The threshold OpenCV

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )


First, the threshold value

It must be on a single channel view ( in particular grayscale ) thresholding operation

1. The fixed threshold: Threshold ()

prototype

double threshold(
	InputArray src,
	OutputArray dst,
	double thresh,
	double maxval,
	int type
);

parameter

  • src : input image
  • dst : Output image
  • Thresh : threshold number (0-255) values of type double
  • maxval: maximum value (0-255) threshold value, and only in THRESH_BINARY valid THRESH_BINARY_INV
  • type: the threshold type (respectively 0,1,2,3,4).
    And generally used THRESH_BINARY THRESH_BINARY_INV the binary image is converted to grayscale (black and white)
    Here Insert Picture Description
    Here Insert Picture Description

Threshold and the threshold type slider

#include<opencv2/opencv.hpp>
using namespace cv;

#define WINDOW_NAME "ThresholdWindow"

int g_thresholdValue=100;
int g_thresholdType=0;
Mat g_srcImage,g_dstImage;

void onChange(int,void*)
{
	threshold(g_srcImage,g_dstImage,g_thresholdValue,255,g_thresholdType);
	imshow(WINDOW_NAME,g_dstImage);
}

int main()
{
	g_srcImage=imread("M569_989_066.jpg",0);
	namedWindow(WINDOW_NAME,WINDOW_NORMAL);
	createTrackbar("ThresholdType",WINDOW_NAME,&g_thresholdType,4,onChange);
	createTrackbar("ThresholdValue",WINDOW_NAME,&g_thresholdValue,255,onChange);
	onChange(0,0);
	waitKey();
	return 0;
}

Here Insert Picture Description

The threshold value of about 180, four, identifying best.

2. Adaptive Threshold: adaptiveThreshold ()

prototype

void adaptiveThreshold(
	InputArray src,
	OutputArray dst,
	double maxval,
	int adaptiveMethod,
	int thresholdType,
	int blockSize,
	double c
);

parameter

  • src : input image
  • dst : Output image
  • maxval: maximum threshold value
  • type: Threshold Type
  • adaptiveMethod:
  • thresholdType:
  • blockSize:
  • c:

3.srcImage = srcImage> 180

srcImage is grayscale

Mat srcImage=imread("M569_989_066.jpg",0);
srcImage=srcImage>180;
imshow("Effect",srcImage);
//效果是二值图,和二进制阈值类型的一样。

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/sandalphon4869/article/details/94609807