The source of this design is to calculate the area of the tomato leaves in the photo, which involves the gray-scale transformation, the extraction of the blade contour and the calculation of the pixel points in the contour. The basic process is to read images-> image preprocessing-> grayscale transformation-> image analysis-> edge detection-> smooth processing-> Fill-> median filter-> calculate pixels. Originally, image processing was implemented by compiling C ++ classes. However, during the implementation process, it was found that the compilation of this class is beyond my own capabilities, especially when it comes to the color palette, which is a headache. Later, we found OpenCV, which is a cross-platform computer vision library. It implements image processing and computer vision algorithms, and almost writes all the algorithms we need, simply call the function.
First, install OpenCV and add its library functions to the directory of VC6.0. The specific method is Tool-> option-> directoties. Add the SRC file in OpenCV to the source file, add binfiles under the Library file, add various Include files under the Include file, and add lib files during compilation. The specific path is project-> setting-> link, the added link library is cv. lib cvaux. lib highgui. lib cvcam. lib cxcore. lib.
Okay, the following is coding. This program is written in MFC. The interface is relatively simple. There is only one image display window, one image loading button, and one calculated area button, one drop-down option is used to select the filter threshold value and three display data controls. First, add a picture control with the ID defined as IDC_ShowImg to display the image. Add a button to load the image. MFC is message-based and event-driven. To implement a function, you must have corresponding messages and functions. The advantage of using the picture control is that it is not limited to adding only bitmaps. Other images such as jpg can also be used. There is another problem: which part of the widget is loaded, so another function is required to control the placement of the image and resize the image, therefore, you must add a void ResizeImage (IplImage * img) function in the dlg class to scale the image to the maximum side of 256. The code is as follows:
Int w = img-> width;
Int h = img-> height;
// Locate the vertex in length and width
Int max = (w> h )? W: h;
// Calculate the proportional factor required to scale the image to the TheImage area.
Float scale = (float) max/256.0f );
// The width and height of the scaled Image
Int nw = (int) (w/scale );
Int nh = (int) (h/scale );
// To save the scaled image to the center of TheImage, the coordinates must be calculated.
Int tlx = (nw> nh )? 0 :( int) (256-nw)/2;
Int tly = (nw> nh )? (Int) (256-nh)/2: 0;
// Set the ROI of TheImage to store the img image.
CvSetImageROI (TheImage, cvRect (tlx, tly, nw, nh ));
// Scale the image img and add it to TheImage
CvResize (img, TheImage );
// Reset TheImage's ROI to read the next image
CvResetImageROI (TheImage );
The Code includes clearing the image area to load the next image. Then load the display function to display the loaded image. In the dlg class, add the void function ShowImage (IplImage * img, uint id), where img points to the image to be displayed, ID is the ID of the control to be displayed. In this example, IDC_ShowImg is used. Then, write the following code to obtain the size of the display area and display it using the device description table:
CDC * pDC = GetDlgItem (ID)-> GetDC (); // obtain the DC of the display space
HDC hDC = pDC-> GetSafeHdc ();
// Obtain hDC for plotting
CRect rect;
GetDlgItem (ID)-> GetClientRect (& rect );
Int rw = rect. right-rect.left;
Int rh = rect. bottom-rect.top;
Int iw = img-> width;
Int ih = img-> height;
Int tx = (int) (rw-iw)/2;
// Display the image in the center
Int ty = (int) (rh-ih)/2;
SetRect (rect, tx, ty, tx + iw, ty + ih );
CvvImage cimg;
Cimg. CopyOf (img); // copy the image
Cimg. DrawToHDC (hDC, & rect );
// Draw the image to the display area
ReleaseDC (pDC );
After completing the preceding preparations, load the image. Use the zoom function to zoom in to the appropriate size, use the display function to display in the appropriate area of the control, and double-click the Load button, the response function OnButton1ReadImg () appears. The Load Code is as follows:
CFileDialog dlg (TRUE, _ T ("* bmp"), NULL, OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY, _ T ("image files (*. bmp ;*. jpg) | *. bmp ;*. jpg | All Files (*. *) | *. * | "), NULL );
// Specify the option Image
Dlg. m_ofn.lpstrTitle = _ T ("Open Image"); // name of the title in the Open file dialog box
If (dlg. DoModal ()! = IDOK)
// Determine whether an image has been obtained
Return;
CString mPath = dlg. GetPathName ();
IplImage * ipl = cvLoadImage (mPath, 1 );
IplImage * pic = cvLoadImage (mPath, 0 );
If (! Ipl)
Return;
If (TheImage)
// Clears the previous image.
CvZero (TheImage );
// CvNamedWindow ("origin", CV_WINDOW_AUTOSIZE );
// CvShowImage ("origin", ipl );
ResizeImage (ipl); // scale the read image so that the maximum length and width are exactly 256, and then copy it to TheImage.
ShowImage (TheImage, IDC_ShowImg); // call the image display function
The preceding figure shows the proper position of the image in the control. The implementation of image algorithms will appear in the next section.