In this video we will discuss Pixel Transformations we will review: Histograms, Intensity Transformations Thresholding and Simple Segmentation We will use opencv as it has all the functions, the video will only focus on gray scale images Let’s review histograms. A histogram counts the number of occurrences of a pixel, and it's a useful tool for understanding and manipulating images. Consider the following toy image with three intensity values, we can plot the occurrence of each pixel we have one black pixel, 5 gray pixels and two white pixels instead of counting the pixels, we can count the intensity values. Consider the following example as numbers ranging from 0 to 2, where 0 is black, 1 gray and 2 is white The histogram counts the pixel intensities. We represent these intensities as an array, the index of the array is the intensity level r in most images we have 256 levels, representing the count of the different intensity of gray levels consider the following image. We can calculate the histogram as follows. We have the image, the channel we would like to calculate, in this video we use the gray scale, the number of channels Finally we have the range of intensity values in yellow We can plot the histogram as a bar graph The darker portions represent the lower intensities, the brighter regions are mapped to the higher values An Intensity Transformation changes a image one pixel at a time Some image transformations depend on neighbouring pixels An Intensity Transformation T depends on only one single point i,j, in the image array “f”, the image array ”f” is converted to array g The transform also operates on the intensity of gray levels “r” mapping them them to “s” This changes the histogram, lets apply a simple transform to a toy image We apply the following linear transform to the image array f, returning the array g, we apply the transform at the pixel located at the first row first column we get the following result at the same pixel location at array g We apply the transform at the pixel located at the second row first column We get the following result at the same index in array g We repeat the process for the entire array. Let's see how the transform changes the histogram We have the histogram each pixel intensity is a function of “r” or histogram r The transform maps each intensity value at r to s we have the new intensity values s and it’s histogram s, let’s see the relationship between histogram “s” and “r” the value of histogram r has one intensity value at zero(click 1) the value of histogram r has one intensity value at zero when applying the transform to r we see the intensity value of zero is mapped to 1 the histogram s now has one intensity value at one we see the intensity value r =1 is mapped to 3 the histogram r has a value of 5 at r=1 In histogram s the value of 5 is mapped to s=3, and so on. If we plot the histogram after applying the transform, we see the histogram is shifted and scaled Image Negatives is where we reverse the intensity levels of an image Consider the following image, its difficult to make out details Applying the following transform will reverse the intensity levels of the image We can apply the transform as an array operation as follows The new image looks like this; we see the details are much more evident The following plot shows the histogram. We can overly the transform as a function of the intensity The function maps the lower intensity values of 0,1 and to 255, 254 and so on We can plot the value of the histogram on the horizontal axis, we see this flips the histogram A linear transform can be seen as applying Brightness and Contrast Adjustments We can use the following linear model where alpha is Simple contrast control and beta is Simple brightness control We can use the following values, just changing beta we can adjust the brightness Rather than using array operations we use the function “convertScaleAbs” after applying the transformation. The function scales, calculates absolute values, so the intensity values fall in the 0 to 255 value range We see the image is much brighter We see the histogram of the bright image has shifted to the right. A lot of the intensity values have been mapped to the point value of 255 We can adjust alpha too change the contrast. We see the contrast in the darker areas have improved but the lighter areas the image appears washed out Comparing the original image histogram to the histogram of the new image We see the lower values exhibit more spread, but many of the larger values have been mapped to 255 explaining why the image appeared washed out We can use nonlinear functions, but let’s explore some algorithms that optimize contrast Histogram Equalization is an algorithm that uses the image's histogram to adjust contrast Consider, the following image with following histogram the function “equalizeHist” improves contrast, by using the histogram to determine a transform that flattens the histogram, the resulting image has improved contrast A threshold function applies a threshold to every pixel, it can be used in extracting objects from an image this is called segmentation The following function applies a threshold to the input array input image array imput_out and outputs the result the following portion of the code will cycle through each pixel (𝑖,𝑗). If the pixel is greater then that threshold It will set a pixel in the array “image_out” pixel to some value, usually 1 or 255, Otherwise it will set it to another value, usually zero. Let’s do an example
Consider the following array In this case the threshold value is 1 and the min value is 0 and the max value 255 the first loop increments through the rows, for each column in “input_img” we check the condition if the values greater than 1 we set the corresponding element in “image_out” to 255. Otherwise we set it to zeros. We repeat the process for the second row As none of the columns are less than one all the corresponding rows in ”Image_out” is set to zero We repeat the process for the final row. The result is all the values in the new image array are white or black Consider the following image, it’s histogram bimodal We see the camera man corresponds to the first mode i.e intensities under 87, the second mode is the back round By setting the threshold accordingly we can segment the camera man We use the function threshold to perform thresholding, the output new_image is the image after thresholding has been applied Along with the image ld the input is the threshold and the max value is set to 255 The final parameter is the threshold type, in this case THRESH_BINARY this means the output will be 0 or 255. In the output we see the camera has zero values and most of the background has values of 255 We see the histograms intensities are mapped to black and wight after thresholding has been applied Sometimes its difficult to select a threshold, therefore we can use OTSU method to select the value automatically. ret is the threshold value determined, We see the image looks identical to the one using the manual threshold. Check out the lab for more