Outline
Topic Requirements |
Program code |
Result picture |
Homilies Miao DAO |
Title Requirements:
Read a landscape view and convert it into a grayscale image
A, the image of the morphological "hat" operation, and display the results
B. Convert the resulting image to a 8-bit mask
C, copy the gray value to the top hat block, display the results → I understand that the results of question A and B are combined into a picture
Program code:
1 //OpenCVExerciseTesting.cpp: Defines the entry point of the console application. 2 //3 //d:\\work\\work_programming\\source\\image\\lena.jpg4 5 6#include"stdafx.h"7#include <cv.h>8#include 9#include <iostream>Ten using namespaceCV; One using namespacestd; A - //function declaration---->-->--->-->--->-->--->// - the //<--<--<--<--<--<--<--<--<--Function Declaration// - - - int_tmain (intARGC, _tchar*argv[]) + { - Const Char* FileName1 ="d:\\work\\work_programming\\source\\image\\opencvexerciseimage\\ 5th chapter \ \ landscape. jpg"; +Iplimage * Src1 =cvloadimage (fileName1, cv_load_image_unchanged); A assert (SRC1); at -Cvnamedwindow ("Original Image",0); -Cvnamedwindow ("Topic _a",0); -Cvnamedwindow ("Topic _b",0); -Cvnamedwindow ("Topic _c",0); - inIplimage * Img_gray = Cvcreateimage (Cvsize (Src1->width, Src1->height), ipl_depth_8u,1); - to //before using Cvcvtcolor and cvcopy These functions, you should validate the parameters before using them. + if(Src1->nchannels! =3) - { thecout <<"the loaded image must be a color picture"<<Endl; * return 0; $ }Panax Notoginseng - Cvcvtcolor (Src1, Img_gray, Cv_rgb2gray); the +Cvshowimage ("Original Image", Img_gray); A the //---------------------------A: Start--------------------------------// + - $Iplimage * Imgcopy =cvcloneimage (img_gray); $ Cvzero (imgcopy); - -Iplconvkernel * kernel = Cvcreatestructuringelementex (3,3,1,1, cv_shape_rect); theCvmorphologyex (Img_gray, imgcopy, NULL, kernel, cv_mop_tophat,6); - WuyiCvshowimage ("Topic _a", imgcopy); the - //---------------------------A: End--------------------------------// Wu - //---------------------------B: Start--------------------------------// About $ -Iplimage * Imgcopy_b =cvcloneimage (img_gray); - Cvzero (imgcopy_b); - ACvthreshold (Imgcopy, Imgcopy_b, -,255, cv_thresh_binary); + theCvshowimage ("Topic _b", imgcopy_b); - $ //---------------------------B: End--------------------------------// the the //---------------------------C: Start--------------------------------// the the -Iplimage * Imgcopy_c =cvcloneimage (imgcopy); in //Cvzero (Imgcopy_c); the the cvcopy (Imgcopy_b, Imgcopy_c, imgcopy_b); About theCvshowimage ("Topic _c", Imgcopy_c); the the //---------------------------C: End--------------------------------// + -Cvwaitkey (0); the BayiCvreleaseimage (&src1); theCvreleaseimage (&imgcopy); theCvreleaseimage (&imgcopy_b); -Cvreleaseimage (&Imgcopy_c); - theCvdestroywindow ("Original Image"); theCvdestroywindow ("Topic _a"); theCvdestroywindow ("Topic _b"); theCvdestroywindow ("Topic _c"); - the return 0; the } the
Result Picture:
Homilies Miao DAO:
① use Cvcvtcolor, cvcopy and other functions, pay attention to verify the validity of the input picture, otherwise it will error, for example:
1 //before using Cvcvtcolor and cvcopy These functions, you should validate the parameters before using them.2 if(Src1->nchannels! =3)3 {4cout <<"the loaded image must be a color picture"<<Endl;5 return 0;6 }7 8Cvcvtcolor (Src1, Img_gray, Cv_rgb2gray);
② Making a mask image is actually a binary image
③ can use cvcopy for image fusion, using the third parameter
④ hats and Black hats: These actions are used to separate patches that are lighter or darker than the neighboring ones. So, can be used to implement similar Cognex-like Cogblobtool function, OpenCV has similar description of the result of the blob: cvconnectedcomponent
"Exercise 5.9" image mask, hat, cvcopy, image fusion, Cvcvtcolor