[OpenCV in C++] Lesson 9 - Common Operations of OpenCV Images (6): Image Morphology - Concept, Function and Operation of Threshold (threshold() function))


The content of the last class (the author still encourages students to learn in order): [OpenCV in C++] Lesson 8 - Common Operations of OpenCV Images (5): Image Morphology - Image Pyramid (Gaussian pyramid, Laplacian pyramid) and upward The use and principle of (down)sampling


1. The concept of threshold (thresh)

  • First of all, as the name implies, " threshold " is a range or limit , so "threshold" is a certain limit value (this value has a certain mathematical meaning, that is, " critical value ", for example, the height of a vehicle height limit pole is a threshold value, It cannot be exceeded; or children under 1.1 meters are free of charge, and children over 1.1 meters will be charged.)
    insert image description here
  • Secondly, the threshold in graphics often refers to a certain pixel value that you want to set.

2. The use of threshold in graphics

        The image thresholding process is used in the field of image separation. According to a certain threshold, the image is separated to obtain the region of interest.
insert image description here

        Of course, based on this idea, it can also be applied to deeper and higher fields, such as medical image analysis and other fields. In short, its application value is very high. Just like the content of the previous chapter.


3. The function and operation of the threshold

3.1 Threshold operations that can be performed in OpenCV

        Next, we use images and function formulas to express its principles and functions as much as possible.
        Note that in the image below, the red solid line is the set threshold!

  • binary mode

    • Mode introduction:
      insert image description here
    • Explanation:
              When the pixel value of the source image is greater than the threshold , these pixels in the processed image take the latest maximum pixel as the result. If the pixel value of the source image is less than or equal to the threshold , the pixel value in the result image is 0.
    • Graphic:
      • Pixel value data before processing:
        insert image description here

      • Processed pixel value data:
        insert image description here

  • binary inversion

    • Mode introduction:
      insert image description here
    • Explanation: Just the opposite of
              pixel binary mode! That is: when the pixel value of the source image is greater than the threshold , the corresponding pixel value of the result image is 0; when the pixel value of the source image is less than or equal to the threshold , the corresponding threshold of the result image takes the MaxValue (maximum value) set in advance.
    • Graphic:
      • Pixel value data before processing:
        insert image description here
      • Processed pixel value data:
        insert image description here
  • Threshold truncation

    • Explanation:
              In a word, set the values ​​of all pixels higher than the threshold to be equal to the threshold, and the remaining pixel values ​​remain unchanged .
    • Graphic:
      • Image data before processing:
        insert image description here
      • Processed image data:
        insert image description here
  • zero threshold

    • Mode introduction:
      insert image description here

    • Explanation: Pixels
              in the source image below or equal to the threshold are set to 0 .

    • Graphic:

      • Image data before processing:
        insert image description here
      • Processed image data:
        insert image description here
  • zero threshold inversion

    • Mode introduction:
      insert image description here
    • Explanation: Reverse the data of the 0 threshold value above, that is, when the pixel value of the source image is greater than the threshold value, the value of the corresponding pixel position of the generated image is 0; otherwise, the original pixel value is retained.
    • Graphic:
      • The data of the image before processing:
        insert image description here
      • The data of the processed image:
        insert image description here

3.2 Operation examples

3.2.1 Threshold() function introduction

  • Function prototype:
double cv::threshold(
						InputArray src,
						OutputArray dst,
						double 	thresh,
						double 	maxval,
						int 	type 
					)	
  • Parameter explanation:
    • src : original image
    • dst: processed image
    • thresh: threshold
    • maxval: The maximum value of the pixel, which is the MaxValue mentioned above
    • type: The method of threshold processing. Here, the threshold modes mentioned above are corresponding to the following integers:

3.2.2 Examples

Reference source code: sample source code

#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
using namespace cv;

int main(void)
{
    
    
    Mat srcImg = imread("/home/aelx-chen/demo.jpg");
    Mat grayImg;
    Mat dstImg;
    cvtColor(srcImg, grayImg, COLOR_BGR2GRAY);

    threshold(grayImg, dstImg, 90, 255, 1);
    /*
        将原图像,设置阈值为90,最大阈值为255,采用二进制方式处理
    */

    imshow("source image",srcImg);
    imshow("gray image",grayImg);
    imshow("destination image",dstImg);
    waitKey(0);
    return 0;
}

3.2.3 Results

insert image description here

Guess you like

Origin blog.csdn.net/weixin_43520503/article/details/129314421