Two value of the image:
compared with edge detection, contour detection can sometimes better reflect the content of the image. In order to detect the image, the image must be binary, the two value of the image is to set the gray value of the pixel on the image to 0 or 255, so that the entire image will show a significant black and white effect. In Digital image processing, the binary image occupies a very important position, the two value of the image reduces the amount of data in the image so as to highlight the contour of the target .
The following is an introduction to the key function--cvthreshold () for binary image in OpenCV.
function function: Using canny method to detect image edge
Function Prototypes:
void Cvthreshold (
Const cvarr* SRC, the first parameter represents an input image and must be a single-channel grayscale.
cvarr* DST, the second parameter represents the output edge image, which is a single-channel black-and-white image.
Double threshold, the third parameter represents a threshold value
Double Max_value, the fourth parameter represents the maximum value
int Threshold_type The fifth parameter represents the method of operation.
);
The definition of an arithmetic method can be found in the imgproc\types_c.h of OpenCV.
Enum
{
Cv_thresh_binary =0, value = value > Threshold? max_value:0
CV_THRESH_BINARY_INV =1, value = value > Threshold? 0:max_value
Cv_thresh_trunc =2, value = value > Threshold? Threshold:value
Cv_thresh_tozero =3, value = value > Threshold? value:0
CV_THRESH_TOZERO_INV =4, value = value > Threshold? 0:value
Cv_thresh_mask = 7,
Cv_thresh_otsu =8 use OTSU algorithm to choose the optimal threshold value;
Combine the flag with one of the above cv_thresh_* values
};
#include"stdafx.h"#include"iostream"using namespacestd; #include"opencv2/opencv.hpp"Iplimage*pgrayimage =Null;iplimage*pbinaryimage =NULL;Const Char*pimagepath ="e:/c_vc_code/text_photo/girl001.jpg";Const Char*pgraywindowstitle ="Original";Const Char*pbinarywindowstitle ="Two value graph";Const Char*pwindowstoolbartitle ="Valve Value";voidOnCallback (intPOS) { //Change to binary imageCvthreshold (Pgrayimage, Pbinaryimage, POS,255, cv_thresh_binary); Cvshowimage (pbinarywindowstitle,pbinaryimage);}intMain () {//load srcouse image from File//iplimage *pimage = Cvloadimage (Pimagepath, cv_load_image_unchanged); //load Gray image from Srcouce file imagePgrayimage =cvloadimage (Pimagepath, Cv_load_image_grayscale); //Cvcvtcolor (pimage,pgrayimage,cv_bgr2gray);Pbinaryimage= Cvcreateimage (Cvgetsize (pgrayimage), ipl_depth_8u,1); //Create window and show orial imageCvnamedwindow (pgraywindowstitle,cv_window_autosize); Cvnamedwindow (pbinarywindowstitle,cv_window_autosize); //creat Slide Bar intpos =1; Cvcreatetrackbar (Pwindowstoolbartitle, Pbinarywindowstitle,&pos, -, OnCallback); OnCallback (0); Cvshowimage (Pgraywindowstitle,pgrayimage); Cvshowimage (Pbinarywindowstitle,pbinaryimage); Cvwaitkey (0); Cvdestroywindow (Pbinarywindowstitle); Cvdestroywindow (Pgraywindowstitle); Cvreleaseimage (&pgrayimage); Cvreleaseimage (&pbinaryimage); return 0;}
Two value of OPENCV image