The comparison operations

Comparing two images with each other (or given values) can be quite useful, especially for producing masks that can be used in various other algorithms, whether it is for tracking some object of interest in an image or performing an operation on an isolated (masked) region of an image. OpenCV provides a handful of functions to perform element-wise comparisons. For instance, the compare function can be used to compare two images with each other. Here's how:

compare(image1, image2, result, CMP_EQ); 

The first two parameters are the first and second image that will participate in the comparison. The result will be saved into a third Mat object, and the last parameter, which must be an entry from the CmpTypes enum, is used to select the type of comparison, which can be any of the following:

  • CMP_EQ: Means that the first image equals the second image
  • CMP_GT: Means that the first image is greater than the second image
  • CMP_GE: Means that the first image is greater than or equal to the second image
  • CMP_LT: Means that the first image is less than the second image
  • CMP_LE: Means that the first image is less than or equal to the second image
  • CMP_NE: Means that the first image is not equal to the second image
Note that we are still talking about element-wise operations, so when we say first image is less than or equal to the second image, what we actually mean is each individual pixel in the first image contains a value less than or equal to its exact corresponding pixel in the second image, and so on.

Note that you can also use the overloaded C++ operators to achieve the same goal as the compare function. Here is how, for each individual comparison type:

result = image1 == image2; // CMP_EQ 
result = image1 > image2; // CMP_GT 
result = image1 >= image2; // CMP_GE 
result = image1 < image2; // CMP_LT 
result = image1 <= image2; // CMP_LE 
result = image1 != image2; // CMP_NE 

The inRange function, which is another useful comparison function in OpenCV, can be used to find the pixels that have a value between a certain lower bound, and higher bound values. You can use any existing image as the boundary value matrices, or you can create them by yourself. Here's an example code that can be used to find the pixel values between 0 and 50 in a grayscale image:

Mat lb = Mat::zeros(image.rows, 
                    image.cols, 
                    image.type()); 
Mat hb = Mat::ones(image.rows, 
                   image.cols, 
                   image.type()) * 50; 
inRange(image, lb, hb, result); 

Note that lb and hb are both Mat objects of the same size and type of source image, except lb is filled with zeroes and hb is filled with the value of 50. This way, when inRange is called, it checks each individual pixel in the source image with their corresponding pixels in lb and hb and sets the corresponding pixel in the result to white if the value fits between the provided boundaries.

The following image depicts the result of the inRange function when it is executed on our example image:

The min and max functions, as it can be easily guessed from their names, are two other comparison functions that can be used to compare two images (element-wise) and find out the minimum or maximum pixel values. Here's an example:

min(image1, image2, result); 

Or you can use max to find the maximum:

max(image1, image2, result); 

Simply put, these two functions compare the pixels of two images of the same size and type and set the corresponding pixel in the result matrix to the minimum or maximum pixel value from the input images.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.70.170