mfc中鼠标响应事件截取roi区域

首先对需要的控件创建鼠标响应函数,在项目—类导向——消息中,找到onlbuttondown,onmove和onlbuttonup,添加至可用后双击就可以出现编辑函数。在函数中进行编辑
在头文件中添加:

CImage t_img;
	BOOL move_flag;
    BOOL down_flag;
    BOOL up_flag;
	int show_width;
int show_height;
double rect_width;
double rect_height;
double img_width;
double img_height;
double xscale;
double yscale;
CRect show_rect;
CRect pic_rect;
CPoint sel_pos;
CPoint prevPoint;
//CvPoint pointROI1;
CPoint startPoint;
//CvPoint pointROI4;
CPoint movePoint;
CPoint endPoint;
CvRect roi_rect;
int roi_width;
int roi_height;

在.cpp中添加显影函数如下:

void CBasicDemoDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if( !down_flag )
	{
		AfxMessageBox(_T("请先输入源图像,再进行ROI裁剪"));
	}
	else{
		GetCursorPos(&sel_pos);
		GetDlgItem(IDC_DISPLAY_STATIC)->GetWindowRect(&pic_rect);
		if(pic_rect.PtInRect(sel_pos))    //如果鼠标在源图像显示范围内
		{
			//::SetCursor(hCur);
			ClipCursor(&pic_rect);//限制鼠标活动区域

			rect_width = pic_rect.right - pic_rect.left;
			rect_height = pic_rect.bottom - pic_rect.top;
			img_width = t_img.GetWidth();
			img_height = t_img.GetHeight();
			xscale = img_width/rect_width;
			yscale = img_height/rect_height;

			startPoint.x = (sel_pos.x - pic_rect.left)*xscale;
			startPoint.y = (sel_pos.y - pic_rect.top)*yscale;

			
		}
		move_flag = true;
	}
	CDialog::OnLButtonDown(nFlags, point);
}


void CBasicDemoDlg::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if(move_flag){
		GetCursorPos(&sel_pos);
		GetDlgItem(IDC_DISPLAY_STATIC) ->GetWindowRect( &pic_rect );
		if(pic_rect.PtInRect(sel_pos))
		{
			/*CClientDC dc(this);
			dc.SetROP2(R2_NOT);*/ 

			rect_width = pic_rect.right - pic_rect.left;
			rect_height = pic_rect.bottom - pic_rect.top;

			endPoint.x=(sel_pos.x-pic_rect.left)*xscale;
			endPoint.y=(sel_pos.y-pic_rect.top)*yscale;

			/*dc.Rectangle(m_ptStart.x, m_ptStart.y, m_ptPrev.x, m_ptPrev.y);
			dc.MoveTo(m_ptStart);
			dc.Rectangle(m_ptStart.x, m_ptStart.y, point.x, point.y);
			m_ptPrev = point;*/
		}
			move_flag = false;
	}
	up_flag = true;
	CDialog::OnMouseMove(nFlags, point);
}


void CBasicDemoDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	if(up_flag)
	{
		GetCursorPos(&sel_pos);
		GetDlgItem(IDC_DISPLAY_STATIC) ->GetWindowRect( &pic_rect );
		if(pic_rect.PtInRect(sel_pos)){			//////////////////判断鼠标在范围内
			ClipCursor(NULL);
			
			rect_width = pic_rect.right - pic_rect.left;
			rect_height = pic_rect.bottom - pic_rect.top;

			endPoint.x=(sel_pos.x-pic_rect.left)*xscale;
			endPoint.y=(sel_pos.y-pic_rect.top)*yscale;
		}
	}
	up_flag = false;

	ShowROIImage(startPoint, endPoint);
	CDialog::OnLButtonUp(nFlags, point);
}


void CBasicDemoDlg::ShowROIImage(CPoint startPoint, CPoint endPoint)
{
	
	roi_rect.x = MIN(startPoint.x, endPoint.x);
	roi_rect.y = MIN(startPoint.y, endPoint.y);
	roi_rect.width = abs(endPoint.x - startPoint.x);
	roi_rect.height = abs(endPoint.y - startPoint.y);

	if(roi_rect.width == 0 || roi_rect.height == 0)
	{
		AfxMessageBox(_T("请重新划定ROI区域"));
	}
	else{		
        IplImage imgTmp = src;//mat类型的src,是输入的图片
        IplImage *input = cvCloneImage(&imgTmp);
		IplImage *dst = cvCreateImage(cvSize(roi_rect.width, roi_rect.height), input->depth, input->nChannels);
		cvSetImageROI(input, roi_rect);//设置roi区域
		cvCopy(input, dst, 0);//提取roi区域
	
		  
		CImage coutput;
        moutput=cvarrToMat(dst);  
        Mat Img = moutput.clone(); 
	    MatToCImage(Img,coutput);
	   CWnd *pWnd = GetDlgItem(IDC_DISPLAY_STATIC2);
	   CDC *pDC = pWnd->GetDC();
	   SetStretchBltMode(pDC->m_hDC,STRETCH_HALFTONE); 
	   CRect ROI_rect1;
	   CRect ROI_rect;
	   pWnd->GetClientRect(&ROI_rect);
	   int height = coutput.GetHeight();//得到图片高度
	   int width = coutput.GetWidth();//得到图片宽度
	   if(width<=ROI_rect.Width() && height<=ROI_rect.Width()) //如果图片的尺寸小于图片控件尺寸,则不缩放显示
	{
		ROI_rect1 = CRect(ROI_rect.TopLeft(), CSize(width,height));
		coutput.StretchBlt(pDC->m_hDC,ROI_rect1,SRCCOPY); //将图片画到Picture控件表示的矩形区域
	}
	else//如果图片的尺寸大于图片控件的尺寸
	{
		float xScale=(float)ROI_rect.Width()/(float)width;//求X方向的缩放因子
		float yScale=(float)ROI_rect.Height()/(float)height;//求Y方向的缩放因子
		//为了图片在图片控件上显示不失真,采用X和Y方向上较小的缩放因子作为缩放因子,此时图片不失真,但是可能不会铺满整个图片控件区域
		//全局变量
		ScaleIndex=(xScale<=yScale?xScale:yScale);
		//rect1 = CRect(rect.TopLeft(), CSize((int)width*xScale,(int)height*xScale));//此时图片失真,但是会铺满整个图片控件区域
		ROI_rect1 = CRect(ROI_rect.TopLeft(), CSize((int)width*ScaleIndex,(int)height*ScaleIndex));
		coutput.StretchBlt(pDC->m_hDC,ROI_rect1,SRCCOPY); //将图片画到Picture控件表示的矩形区域
		
	   resize(moutput, resizeMoutput,Size(),ScaleIndex, ScaleIndex, INTER_LINEAR );

	}
	ReleaseDC(pDC);//释放picture控件的Dc
	
	}
}

鼠标左键按住拉动抬起就会在右图显示截取区域
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43491924/article/details/89476559