Image Processing in Python PDF
Document Details
![ProdigiousGyrolite3737](https://quizgecko.com/images/avatars/avatar-12.webp)
Uploaded by ProdigiousGyrolite3737
Rebeca Gonzalez
Tags
Related
- Week 2 - Unit I - Image Fundementals - II.pptx.pdf
- Week 2 - Unit I - Image Fundementals - II PDF
- Medical Image Analysis Lecture Notes PDF
- Lab Report No. 7 on Siamese Networks - Kyiv Polytechnic Institute - 2024 PDF
- Chapter 6 - Cloud Application Development in Python PDF
- Sections of Proof of Textbook, Highlighted PDF
Summary
This document discusses various image processing techniques in Python, covering topics such as filtering, contrast enhancement, transformations, and morphology. It explains concepts like Gaussian smoothing, edge detection, histogram equalization, and adaptive equalization. The document also includes code examples and visual representations of the methods.
Full Transcript
Jump into filtering IMAGE PROCESSING IN PYTHON Rebeca Gonzalez Data Engineer Filters Enhancing an image Emphasize or remove features Smoothing Sharpening Edge detection Neighborhoods Certain image processing operations involve...
Jump into filtering IMAGE PROCESSING IN PYTHON Rebeca Gonzalez Data Engineer Filters Enhancing an image Emphasize or remove features Smoothing Sharpening Edge detection Neighborhoods Certain image processing operations involve processing an image in sections, called blocks or neighborhoods, rather than processing the entire image at once. Neighborhoods filtering, histogram equalization for contrast enhancement, morphological functions Edge detection With filtering we can detect edges. This technique can be used to find the boundaries of objects within images. As well as segment and extract information like how many coins are in an image. Most of the shape information of an image is enclosed in edges. Edge detection Edge detection Edge detection works by detecting discontinuities in brightness Edge detection Edge detection Sobel # Import module and function from skimage.filters import sobel # Apply edge detection filter edge_sobel = sobel(image_coins) # Show original and resulting image to compare plot_comparison(image_coins, edge_sobel, "Edge with Sobel") Edge detection Sobel Comparing plots def plot_comparison(original, filtered, title_filtered): fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(8, 6), sharex=True, sharey=True) ax1.imshow(original, cmap=plt.cm.gray) ax1.set_title('original’) ax1.axis('off’) ax2.imshow(filtered, cmap=plt.cm.gray) ax2.set_title(title_filtered) ax2.axis('off') Gaussian smoothing A Gaussian filter is used to smoothen images by reducing noise and detail, utilizing a kernel (a small block or image patch) that follows the shape of a Gaussian (bell curve) distribution. This technique is typically used to blur an image or to reduce noise original Gaussian smoothing original Blurred with gaussian Gaussian smoothing Gaussian smoothing # Import the module and function from skimage.filters import gaussian # Apply edge detection filter gaussian_image = gaussian(original_image, channel_axis=-1) # Show original and resulting image to compare plot_comparison(original_image, gaussian_image, "Gaussian filtered image") Gaussian smoothing Gaussian smoothing Gaussian smoothing (be careful!) Questions? Contrast enhancement IMAGE PROCESSING IN PYTHON Rebeca Gonzalez Data engineer Contrast enhancement Image contrast refers to the difference in luminance or color that makes an object (or its representation within an image) distinguishable. In practical terms, higher contrast helps to clearly delineate objects while lower contrast can make an image look more flat and details harder to discern. Contrast enhancement Contrast Histograms for contrast enhancement The contrast of an image can be seen as the measure of its dynamic range, or the "spread" of its histogram. Contrast Contrast Enhance contrast Contrast stretching stretching the range of intensity values it contains to span a desired range of values Histogram before and after Contrast Stretching Enhance contrast Contrast stretching stretching the range of intensity values it contains to span a desired range of values Histogram equalization This method improves the contrast in an image by redistributing the image's intensity levels so that the histogram of the output image is approximately uniform. Histogram before and after Equalization Contrast Enhancement Comparison Types of Histogram Equalization Histogram equalization (standard) Adaptive histogram equalization Contrast Limited Adaptive Histogram Equalization (CLAHE) Histogram equalization Histogram equalization Histogram equalization from skimage import exposure # Obtain the equalized image image_eq = exposure.equalize_hist(image) # Show original and result show_image(image, 'Original’) show_image(image_eq, 'Histogram equalized') Histogram equalization Adaptive Equalization Contrastive Limited Adaptive Histogram Equalization (CLAHE) CLAHE calculates multiple histograms based on different sections of the image to adjust lightness values, enhancing contrast without over-amplifying noise. This method often yields results similar to standard histogram equalization but with improved detail preservation in varying lighting conditions. Contrastive Limited Adaptive Equalization The adaptive method is not that intense, so it looks more natural. This is because it is not taking the global histogram of the entire image, but operates on small regions called tiles or neighborhoods. CLAHE in scikit-image from skimage import exposure # Apply adaptive Equalization image_adapteq = exposure.equalize_adapthist(image, clip_limit=0.03) # Show original and result show_image(image, 'Original') show_image(image_adapteq, 'Adaptive equalized') CLAHE in scikit-image CLAHE in scikit-image Questions? Transformations IMAGE PROCESSING IN PYTHON Rebeca Gonzalez Data Engineer Why transform images? Preparing images for classification Machine Learning models Optimization and compression of images Save images with same proportion Rotating Rotating Rotating clockwise from skimage.transform import rotate # Rotate the image 90 degrees clockwise image_rotated = rotate(image, -90) show_image(image, 'Original') show_image(image_rotated, 'Rotated 90 degrees clockwise') Rotating anticlockwise from skimage.transform import rotate # Rotate an image 90 degrees anticlockwise image_rotated = rotate(image, 90) show_image(image, 'Original') show_image(image_rotated, 'Rotated 90 degrees anticlockwise') Rescaling Rescaling Downgrading from skimage.transform import rescale # Rescale the image to be 4 times smaller image_rescaled = rescale(image, 1/4, anti_aliasing=True, channel_axis=-1) show_image(image, 'Original image') show_image(image_rescaled, 'Rescaled image') Rescaling Aliasing in digital images Aliasing in digital images creates a rippling or wavy effect due to insufficient pixel resolution, causing the image to display improperly rendered patterns. This phenomenon occurs when the digital representation fails to accurately capture finer details. Aliasing in digital images The first one has the anti_aliasing to True so what we see is softer. While the one without it is pixelated. Resizing Same purpose as rescale, but allows to specify an output image shape instead of a scaling factor. Resizing from skimage.transform import resize # Height and width to resize height = 400 width = 500 # Resize image image_resized = resize(image, (height, width), anti_aliasing=True) # Show the original and resulting images show_image(image, 'Original image') show_image(image_resized, 'Resized image') Resizing Resizing proportionally (rescaling) from skimage.transform import resize # Set proportional height so its 4 times its size height = image.shape / 4 width = image.shape / 4 # Resize image image_resized = resize(image, (height, width), anti_aliasing=True) show_image(image_resized, 'Resized image') Resizing proportionally Questions? Morphology IMAGE PROCESSING IN PYTHON Rebeca Gonzalez Data Engineer Binary images Binary regions produced by simple thresholding can be distorted by noise and texture Morphological filtering Morphological filtering operations try to remove these imperfections by accounting for the form and structure of the objects in the image. Better for binary images Can extend for grayscale Morphological operations Dilation Erosion Dilation adds pixels to the boundaries of objects in an image, while erosion removes pixels on object boundaries. Structuring element (aka kernel) Structuring element (aka kernel) symmetric symmetric symmetric asymmetric Mechanism of Erosion (shrinking effect) image kernel eroded image In erosion, the center pixel in the output will be set to 1 only if all the pixels under the structuring element (kernel) that correspond to 1s are also 1. Mechanism of Erosion (shrinking effect) image kernel eroded image In erosion, the center pixel in the output will be set to 1 only if all the pixels under the structuring element (kernel) that correspond to 1s are also 1. Mechanism of Erosion (shrinking effect) image kernel eroded image In erosion, the center pixel in the output will be set to 1 only if all the pixels under the structuring element (kernel) that correspond to 1s are also 1. Mechanism of Dilation (expanding effect) image kernel eroded image In dilation, the center pixel in the output will be set to 1 if any of the pixels under the structuring element (kernel) that correspond to 1s are also 1. Mechanism of Dilation (expanding effect) image kernel eroded image In dilation, the center pixel in the output will be set to 1 if any of the pixels under the structuring element (kernel) that correspond to 1s are also 1. Mechanism of Dilation (expanding effect) image kernel eroded image In dilation, the center pixel in the output will be set to 1 if any of the pixels under the structuring element (kernel) that correspond to 1s are also 1. Shapes in scikit-image Erosion in scikit-image from skimage import morphology # Set structuring element to the rectangular-shaped selem = rectangle(12,6) # Obtain the erosed image with binary erosion eroded_image = morphology.binary_erosion(image_horse, selem=selem) Erosion in scikit-image # Show result plot_comparison(image_horse, eroded_image, 'Erosion') Binary erosion with default selem # Binary erosion with default selem eroded_image = morphology.binary_erosion(image_horse) Dilation in scikit-image from skimage import morphology # Obtain dilated image, using binary dilation dilated_image = morphology.binary_dilation(image_horse) # See results plot_comparison(image_horse, dilated_image, ‘Dilation’) Dilation in scikit-image More examples: dilation and erosion Original More examples: dilation and erosion More examples: dilation and erosion Questions? Let's practice! IMAGE PROCESSING IN PYTHON