Threshold Processing of Opencv Learning

Keywords: Windows

_threshold can be regarded as the simplest method of image segmentation. This method is based on the gray difference between the object and the background in the image, and this segmentation belongs to the pixel level.

Fixed Threshold Operations - threshold() Functions - Single Channel Array

double threshold(inoutArray,outputArray,double thresh,double maxval,int type)
* First parameter, input image, single channel, 8 or 32 bit floating point type Mat.
* The second parameter, output image.
* The third parameter is the specific value of the threshold.
* The fourth parameter is the maximum value of threshold type when the fifth parameter threshold type takes CV_THRESH_BINARY or CV_THRESH_BINARY_INV threshold type.
* Fifth parameter, threshold type, int type
(1) 0, CV_THRESH_BINARY: Binary Thresholding
(2) 1, CV_THRESH_BINARY_INV: Reverse binary thresholding and inversion
(3) 2, CV_THRESH_TRUNC: Truncation Thresholding
(4) 3, CV_THRESH_TOZERO_INV: Exceeding the threshold is set to 0
(5) 4, CV_THRESH_TOZERO: Set below the threshold to 0

Adaptive threshold operation-adaptiveThreshold() function-matrix

void adaptiveThreshold(inputArray,outputArray,double maxVal,int adaptiveMethod,int thresholdType,int blockSize,double C)
* The first parameter, input image, 8-bit single-channel floating-point image.
* The second parameter, output image.
* The third parameter assigns a non-zero value to the pixel that satisfies the condition.
* The fourth parameter specifies the adaptive threshold algorithm to be used, ADAPTIVE_THRESH_MEAN_C or ADAPTIVE_THRESH_GAUSSIAN_C.
* The fifth parameter, threshold type, THRESH_BINARY, one of THRESH_BINARY_INV.
* The sixth parameter is used to calculate the neighborhood size of a pixel whose threshold size is 3, 5, 7, etc.
* The seventh parameter, the constant value after subtracting the average or weighted average, is usually positive, but in a few cases it can also be zero or negative.

#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<iostream>

using namespace cv;
using namespace std;

#define WINDOW_NAME "[Procedure Window]"

//Global variable declaration
Mat g_srcImage,g_dstImage,g_grayImage;
int g_nThresholdType=0;//0 is binary threshold, 1 is anti-binary threshold, 2 is truncation threshold, 3 is threshold to 0, 4 is anti-threshold to 0, 5 is uniform adaptive threshold, 6 is Gauss adaptive threshold.
int g_nThresholdValue=100;

//Global function declaration section
void on_Threshold(int ,void *);
//Principal function
int main()
{
    //Load the source image
    g_srcImage=imread("/Users/new/Desktop/1.jpg");
    if(!g_srcImage.data){printf("Read the source image srcImage error~!\n");return false;}
    //Grayscale image
    cvtColor(g_srcImage, g_grayImage, COLOR_RGB2GRAY);
    //Create windows and display source images
    namedWindow(WINDOW_NAME,WINDOW_AUTOSIZE);

    //Slide bar controls threshold and mode
    createTrackbar("mode:", WINDOW_NAME, &g_nThresholdType, 6,on_Threshold);
    createTrackbar("parameters:", WINDOW_NAME, &g_nThresholdValue, 255,on_Threshold);

    //Initialize custom threshold callback functions
    on_Threshold(0,0);

    //Poll for user cases and exit if ESC key is pressed
    while(1)
    {
        int key;
        key= waitKey(20);
        if((char)key==27){break;}
    }
}

//Customized threshold callback function
void on_Threshold(int,void *)
{
    switch(g_nThresholdType)
    {
        case 5:
            adaptiveThreshold(g_grayImage, g_dstImage, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 3, 5);
            break;
        case 6:
            adaptiveThreshold(g_grayImage, g_dstImage, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 3, 5);
            break;
        default:
            threshold(g_grayImage, g_dstImage, g_nThresholdValue, 255, g_nThresholdType);
    }
    imshow(WINDOW_NAME,g_dstImage);
}

Original picture:

Fixed threshold-binary threshold:

Fixed threshold-anti-binary threshold:

Fixed threshold-truncation threshold:

Fixed threshold-inverse threshold is reduced to 0:

Fixed Threshold-Threshold to 0:

Adaptive threshold-uniformity:

Adaptive Threshold-Gauss:

Posted by Goose87 on Fri, 21 Jun 2019 17:08:48 -0700