// 8-1.cpp: defines the entry point of the console application. /// * ============================================ ========================================== Name: 8-1 time: 2013.07.02 note: binarization ================================================= =============================================================== */# include "stdafx. H "# include" CV. H "# include" highgui. H "# include" cxcore. H "using namespace STD; // defines two global variables, sequence cvseq * s, * DPS; int _ tmain (INT argc, _ tchar * argv []) {// define two image pointers to the input image and the output image iplimage * SRC, * DST, * dst1; src = cvloadimage ("D: // 34.jpg "); DST = cvcreateimage (cvgetsize (SRC), Src-> depth, 1); dst1 = cvcreateimage (cvgetsize (SRC), Src-> depth, 1); // defines two memories, after storing the processed image cvpoint P; cvmemstorage * storage_ct = cvcreatememstorage (0); cvmemstorage * storage_dp = cvcreatemstorage (0); cvcvtcolor (SRC, DST, cv_bgr2gray ); // convert the image to a grayscale image cvthreshold (DST, DST, 80,180, cv_thresh_binary_inv); // binarization dst1 = cvcloneimage (DST ); // search for the image contour cvfindcontours (DST, storage_ct, & contours, sizeof (cvcontour), cv_retr_list, cv_chain_approx_simple); cvzero (DST); cvdrawcontours (DST, contours, cvscalarall (255), cvscalarall (255), 8, cvpoint (0, 0); // create a display window and an image cvnamedwindow ("src", 0 ); cvnamedwindow ("DST", 0); cvshowimage ("src", dst1); cvshowimage ("DST", DST); cvwaitkey (0); // release cvdestroyallwindows (); cvreleaseimage (& SRC); cvreleaseimage (& DST); Return 0 ;}
Running effect: