OpenCV notes (ii) Open folder read Pictures _OPENCV Learn notes

Source: Internet
Author: User

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); }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.