CImage对象使用罗伯特算子进行边缘检测

本文介绍了一个使用C++实现的简单边缘检测程序。该程序通过比较像素间的差异来检测图像边缘,并能够加载和显示图像。文章提供了关键代码片段,包括如何处理按钮点击事件以打开图像文件及进行边缘检测。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 在新插入的对话框中添加两个Picture Control和两个Button

2 打开图像代码和显示图像代码和前面两篇博文类似

BEGIN_MESSAGE_MAP(CEdgeDetDlg, CDialogEx)
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_OPEN_BTN, &CEdgeDetDlg::OnClickedOpenBtn)
	ON_BN_CLICKED(IDC_EDGE_DETECTION_BTN, &CEdgeDetDlg::OnClickedEdgeDetectionBtn)
END_MESSAGE_MAP()


// CEdgeDetDlg 消息处理程序

void CEdgeDetDlg::OnClickedOpenBtn()
{
	// TODO: 在此添加控件通知处理程序代码
	CFileDialog dlg(TRUE,_T(".bmp"),_T("*.jpg"),OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
		_T("Bit Map(*.bmp)|*.bmp|JPEG Map(*.jpg)|*.jpg||"));
	if(dlg.DoModal() == IDOK)
	{
		if(!yuantu.IsNull())
		{
			yuantu.Destroy();
		}
		yuantu.Load(dlg.GetPathName());
		Invalidate(FALSE);
	}
}

//边缘检测
void CEdgeDetDlg::OnClickedEdgeDetectionBtn()
{
	if (yuantu.IsNull())
	{
		MessageBox(_T("还未打开图像"),_T("系统提示"),MB_OK);
		return;
	}
	if(!bianyuantu.IsNull())
	{
		bianyuantu.Destroy();
	}

	int i,j,temp;
	int pixel[4];
	int width = yuantu.GetWidth();
	int height = yuantu.GetHeight();
	int widthBytes = yuantu.GetPitch();
	bianyuantu.Create(width,height,yuantu.GetBPP());

	if(yuantu.IsIndexed())
	{
		yuantu.GetColorTable(0,256,colorTable);
		bianyuantu.SetColorTable(0,256,colorTable);
	}
	BYTE *pYuantuData = (BYTE*)yuantu.GetBits();
	BYTE *pBianyuantuData =(BYTE*)bianyuantu.GetBits();

	
	for(j=0;j<height-1;j++)
	{
		for(i=0;i<width-1;i++)
		{
			pixel[0]=pYuantuData[j*widthBytes+i];
			pixel[1]=pYuantuData[j*widthBytes+i+1];
			pixel[2]=pYuantuData[(j+1)*widthBytes+i];
			pixel[3]=pYuantuData[(j+1)*widthBytes+i+1];
			temp=(int)sqrt((double)((pixel[0]-pixel[3])*(pixel[0]-pixel[3])
				+(pixel[1]-pixel[2])*(pixel[1]-pixel[2])));
			pBianyuantuData[j*widthBytes+i]=temp;
		}
	}

	Invalidate(FALSE);
}

void CEdgeDetDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();

		if(!yuantu.IsNull())
		{
			CWnd* pWndYuantu = this->GetDlgItem(IDC_YUANTU);
			CDC* pdc = pWndYuantu->GetDC();
			yuantu.Draw(pdc->GetSafeHdc(), 0, 0);
			pWndYuantu->ReleaseDC(pdc);
		}

		if(!bianyuantu.IsNull())
		{
			CWnd* pWndBianyuantu = this->GetDlgItem(IDC_BIANYUANTU);
			CDC* pdc = pWndBianyuantu->GetDC();
			bianyuantu.Draw(pdc->GetSafeHdc(), 0, 0);
			pWndBianyuantu->ReleaseDC(pdc);
		}
	}
}
测试时用的是灰度图像,实现效果如下:


转载:http://blog.youkuaiyun.com/foreverling/article/details/40116763

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值