opencv打開圖片用到的ShowImage()函數和ResizeImage()函數

<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爲對話框的類。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章