Image Processing in Python: Image Restoration, Noise, Segmentation - PDF
Document Details
![ProdigiousGyrolite3737](https://quizgecko.com/images/avatars/avatar-12.webp)
Uploaded by ProdigiousGyrolite3737
Tags
Summary
This document introduces image processing concepts in Python, focusing on image restoration, dealing with noise, and image segmentation techniques. The document also explains contour detection using computer vision tools and methods. It serves as a beginner-friendly tutorial for learning image manipulation.
Full Transcript
Image restoration IMAGE PROCESSING IN PYTHON Rebeca Gonzalez Data Engineer Restore an image Image reconstruction Fixing damaged images Text removing Logo removing Object removing Image reconstruction Inpainting Reconstructing lost parts of images...
Image restoration IMAGE PROCESSING IN PYTHON Rebeca Gonzalez Data Engineer Restore an image Image reconstruction Fixing damaged images Text removing Logo removing Object removing Image reconstruction Inpainting Reconstructing lost parts of images Looking at the non-damaged regions Image reconstruction A mask image is simply an image where some of the pixel intensity values are zero and others are non-zero (damaged). Image reconstruction in scikit-image from skimage.restoration import inpaint # Obtain the mask mask = get_mask(defect_image) # Apply inpainting to the damaged image using the mask restored_image = inpaint.inpaint_biharmonic(defect_image, mask, channel_axis=-1) # Show the resulting image show_image(restored_image) Image reconstruction in scikit-image # Show the defect and resulting images show_image(defect_image, 'Image to restore’) show_image(restored_image, 'Image restored') Masks In the right image we see the damaged areas (white pixels) of the image as a mask. Masks def get_mask(image): ''' Creates mask with three defect regions ''' mask = np.zeros(image.shape[:-1]) mask[101:106, 0:240] = 1 mask[152:154, 0:60] = 1 mask[153:155, 60:100] = 1 mask[154:156, 100:120] = 1 mask[155:156, 120:140] = 1 mask[212:217, 0:150] = 1 mask[217:222, 150:256] = 1 return mask Questions? Noise IMAGE PROCESSING IN PYTHON Rebeca Gonzalez Data Engineer Noise (in digital images) refers to random variations of brightness or color information, typically manifesting as grainy or pixelated disturbances. ✓ low light conditions ✓ high ISO settings ✓ sensor limitations. Noise (in digital images) In this image, we can see a variation of brightness and color that does not correspond to reality Apply noise in scikit-image # Import the module and function from skimage.util import random_noise # Add noise to the image noisy_image = random_noise(dog_image) # Show original and resulting image show_image(dog_image) show_image(noisy_image, 'Noisy image') Apply noise in scikit-image This type of noise is known as "salt and pepper" Reducing noise Noisy Image Denoised Image Denoising types Total variation (TV) Bilateral Wavelet denoising Non-local means denoising Denoising types Total variation (TV) Bilateral Wavelet denoising Non-local means denoising Denoising Using total variation filter denoising from skimage.restoration import denoise_tv_chambolle # Apply total variation filter denoising denoised_image = denoise_tv_chambolle(noisy_image, weight=0.1, channel_axis=-1) # Show denoised image show_image(noisy_image, 'Noisy image') show_image(denoised_image, 'Denoised image') Denoising Total variation filter Denoising Bilateral filter from skimage.restoration import denoise_bilateral # Apply bilateral filter denoising denoised_image = denoise_bilateral(noisy_image, channel_axis=-1) # Show original and resulting images show_image(noisy_image, 'Noisy image') show_image(denoised_image, 'Denoised image') Denoising Bilateral filter Denoising Total variation filter Bilateral filter The resulting image (bilateral filter) is less smooth than the one from the total variation filter and preserves the edges a lot more. Questions? Superpixels & segmentation IMAGE PROCESSING IN PYTHON Rebeca Gonzalez Data Engineer Segmentation The goal is to partition images into regions, or segments, to simplify and/or change the representation into something more meaningful and easier to analyze. Segmentation before a tumor is analyzed in a before recognizing a face, it has to also be computed tomography, it has to be picked out from its background detected and somehow isolated (segmentation / detection). from the rest of the image. Image representation A single pixel, standing alone by itself, is not a natural representation. Superpixels We can explore more logical meanings in an image that's formed by bigger regions or grouped pixels. A superpixel is a group of connected pixels with similar colors or gray levels. These carry more meaning than their simple pixel grid counterparts. Benefits of superpixels More meaningful regions Computational efficiency Segmentation Supervised the kind of thresholding in which we specify the threshold value ourselves. Unsupervised no prior knowledge is required. These algorithms try to subdivide images into meaningful regions automatically. e.g. Otsu Unsupervised segmentation Simple Linear Iterative Clustering (SLIC) It segments the image using a machine learning algorithm called K-Means clustering. It takes in all the pixel values of the image and tries to separate them into a predefined number of sub-regions. Unsupervised segmentation (SLIC) # Import the modules from skimage.segmentation import slic from skimage.color import label2rgb # Obtain the segments segments = slic(image) # Put segments on top of original image to compare segmented_image = label2rgb(segments, image, kind='avg') show_image(image) show_image(segmented_image, "Segmented image") Unsupervised segmentation (SLIC) Original image segments segmented image More segments # Import the modules from skimage.segmentation import slic from skimage.color import label2rgb # Obtain the segmentation with 300 regions segments = slic(image, n_segments= 300) # Put segments on top of original image to compare segmented_image = label2rgb(segments, image, kind='avg') show_image(segmented_image) More segments More segments 100 segments 300 segments Questions? Finding contours IMAGE PROCESSING IN PYTHON Rebeca Gonzalez Data Engineer Finding contours A contour is a closed shape of points or line Total points in domino tokens: 29. segments, representing the boundaries of objects. ✓ Measure size ✓ Classify shapes ✓ Determine the number of objects Binary images We can obtain a binary image applying thresholding or using edge detection Find contours using scikit-image PREPARING THE IMAGE Transform the image to 2D grayscale. # Make the image grayscale image = color.rgb2gray(image) Find contours using scikit-image PREPARING THE IMAGE Binarize the image # Obtain the thresh value thresh = threshold_otsu(image) # Apply thresholding thresholded_image = image > thresh Find contours using scikit-image And then use find_contours() # Import the measure module from skimage import measure # Find contours at a constant value of 0.8 contours = measure.find_contours(thresholded_image, 0.8) Constant level value The level value varies between 0 and 1, the closer to 1 the more sensitive the method is to detecting contours, so more complex contours will be detected. We have to find the value that best detects the contours we care for. The steps to spotting contours from skimage import measure from skimage.filters import threshold_otsu # Make the image grayscale image = color.rgb2gray(image) # Obtain the optimal thresh value of the image thresh = threshold_otsu(image) # Apply thresholding and obtain binary image thresholded_image = image > thresh # Find contours at a constant value of 0.8 contours = measure.find_contours(thresholded_image, 0.8) The steps to spotting contours Resulting in A contour's shape Contours: list of (n,2) - ndarrays. A contour's shape Contours: list of (n,2) - ndarrays. A contour's shape Contours: list of (n,2) - ndarrays. A contour's shape Contours: list of (n,2) - ndarrays. A contour's shape Contours: list of (n,2) - ndarrays. A contour's shape Contours: list of (n,2) - ndarrays. Number of dots: 7. Questions? Let's practice! IMAGE PROCESSING IN PYTHON