OpenCV notes are records of MFC under the image processing project, encountered problems, leave this blog for record.
This article is mainly about the need to open a dialog box, manually selected pictures, displayed on the MFC picture control. For the code that uses absolute paths to read pictures in a program, here is a straightforward explanation for not doing too much.
OpenCV use absolute path to read pictures:
void Cmydlg::onbnclickedbtnloadimg () {//TODO: Add control Notification handler code iplimage *SRCIMG here; srcimg = Cvloadimage ("c:\\users\\administrator\\desktop\\1.jpg");//Read picture with absolute path//srcimg = Cvloadimage (". \\1.jpg"); Indicates that the IF (!) is read from the current folder. Srcimg->imagedata) {AfxMessageBox ("cvloadimage () Err");//Use multibyte Character set//afxmessagebox (_t ("Cvloadimage () Err"));//Use Unicode character Set} if (Srcimg->nchannels!= 1)//If not grayscale, convert to grayscale map {iplimage *grayimg = Cvcreateimage (Cvgetsize (srcimg), IP
l_depth_8u, 1);
Cvcvtcolor (srcimg, grayimg, Cv_bgr2gray);
else {cvreleaseimage (&grayimg);
grayimg = srcimg; /* Picture control displays the image with the ID of idc_showsrcimg///Get the device environment for idc_static controls for image manipulation cdc* pdcsrc = Getdlgit
EM (idc_showsrcimg)->GETDC ()///Get the display space of the DC,MFC class is generally the beginning of the C HDC hdcsrc = PDCSRC->GETSAFEHDC ();//Get HDC handle for drawing operation
Cvvimage cimgsrc; Cimgsrc.
Copyof (GRAYIMG);
Displays the image to the IDC_SHOWIMGSRC control CRect rectsrc;
GetDlgItem (idc_showsrcimg)->getclientrect (&RECTSRC); Cimgsrc. DRAWTOHDC (HDCSRC, &RECTSRC);
Release of ReleaseDC (PDCSRC); }
OpenCV Pop-up dialog box, select the picture to display:
void Cmydlg::onbnclickedbtnopenimg () {//TODO: Add control notification handler code here iplimage* srcimg = NULL; CFileDialog Dlg (TRUE, _t ("*.bmp;*.jpg;*.tif"), NULL, Ofn_filemustexist | Ofn_pathmustexist |
Ofn_hidereadonly, "Image files All Files (*.*) |*.*| |", NULL);//Option picture's convention; Dlg.m_ofn.lpstrTitle = _t ("Open Picture");//Open File dialog box title name; (*.bmp; *.jpg) |*.bmp;
*.jpg | if (dlg.
DoModal () = = IDOK)//judge whether to get pictures; {if (DLG). Getfileext ()!= "BMP" && dlg. Getfileext ()!= "JPG" &&dlg. Getfileext ()!= "tif") {AfxMessageBox () ("Select the correct picture format.")
"), MB_OK);
Return } CString MPath = dlg.
GetPathName ();//Get Picture path;
srcimg = Cvloadimage (MPath);//read pictures, cache into a local variable IPL; if (!
SRCIMG)//judge whether the successful loading of the picture;
Return /* Picture control displays the image with the ID of idc_showsrcimg///Get the device environment for idc_static controls for image manipulation cdc* pdcsrc = Getdlgi
TEM (IDC_SHOWSRCIMG)->GETDC ()///Get the display space of the DC,MFC class is generally the beginning of the C HDC hdcsrc = PDCSRC->GETSAFEHDC ();//Get HDC handle for drawing operation
Cvvimage cimgsrc; Cimgsrc.
Copyof (SRCIMG); The imageDisplay to the IDC_SHOWIMGSRC control on the CRect rectsrc;
GetDlgItem (idc_showsrcimg)->getclientrect (&RECTSRC); Cimgsrc.
DRAWTOHDC (HDCSRC, &RECTSRC);
Release of ReleaseDC (PDCSRC); }