opencv打开图片用到的ShowImage()函数和ResizeImage()函数

来源:互联网 发布:一生所爱 网络歌手 编辑:程序博客网 时间:2024/05/21 22:29
<pre name="code" class="cpp">void CmymfcDlg::ResizeImage(IplImage* img) {// 读取图片的宽和高int w = img->width; int h = img->height;   // 找出宽和高中的较大值者 int max = (w > h)? w: h;  // 计算将图片缩放到TheImage区域所需的比例因子 float scale = (float) ( (float) max / 256.0f );  // 缩放后图片的宽和高 int nw = (int)( w/scale );int nh = (int)( h/scale );  // 为了将缩放后的图片存入 TheImage 的正中部位,需计算图片在 TheImage 左上角的期望坐标值 int tlx = (nw > nh)? 0: (int)(256-nw)/2; int tly = (nw > nh)? (int)(256-nh)/2: 0;   // 设置 TheImage 的 ROI 区域,用来存入图片 img cvSetImageROI( TheImage, cvRect( tlx, tly, nw, nh) );   // 对图片 img 进行缩放,并存入到 TheImage 中 cvResize( img, TheImage );   // 重置 TheImage 的 ROI 准备读入下一幅图片 cvResetImageROI( TheImage ); }     void CmymfcDlg::ShowImage( IplImage* img, UINT ID ) // ID 是Picture Control控件的ID号 { // 获得显示控件的 DC CDC* pDC = GetDlgItem( ID ) ->GetDC(); // 获取 HDC(设备句柄) 来进行绘图操作 HDC hDC = pDC ->GetSafeHdc();   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; int ty = (int)(rh - ih)/2; SetRect( rect, tx, ty, tx+iw, ty+ih ); // 复制图片 CvvImage cimg; cimg.CopyOf( img ); // 将图片绘制到显示控件的指定区域内 cimg.DrawToHDC( hDC, &rect );   ReleaseDC( pDC ); }


CmymfeDlg为对话框的类。                                             
0 0