图像边缘提取

基于边缘颜色变化的边缘提取:

原理是该位置的像素值与周边像素值的R G B存在较大差别,即判定其为边缘像素

#include<opencv2\opencv.hpp>
#include<vector>
#include<iostream>
using namespace std;
using namespace cv;

int main()
{
	IplImage *image = cvLoadImage("D:\\8.jpg");
	if (image == NULL)
	{
		cout << "打开图像失败!";
		return -1;
	}

	struct Label
	{
		int row;
		int column;

	};

	vector<Label> Boundary;
	Label temp;

	CvScalar scalar;
	double thres = 100;
	for (int i = 2; i <= image->height-2; i++)
	{
		for (int j = 2; j < image->width-2; j++)
		{
			
			int R1 = cvGet2D(image, i-1, j-1).val[2]; int G1 = cvGet2D(image, i-1, j-1).val[0]; int B1 = cvGet2D(image, i-1, j-1).val[1];
			int R2 = cvGet2D(image, i-1, j).val[2]; int G2 = cvGet2D(image, i-1, j).val[0]; int B2 = cvGet2D(image, i-1, j).val[1];
			int R3 = cvGet2D(image, i-1, j+1).val[2]; int G3 = cvGet2D(image, i-1, j+1).val[0]; int B3 = cvGet2D(image, i-1, j+1).val[1];
			
			int R4 = cvGet2D(image, i, j-1).val[2]; int G4 = cvGet2D(image, i, j-1).val[0]; int B4 = cvGet2D(image, i, j-1).val[1];
			int R5 = cvGet2D(image, i, j).val[2]; int G5 = cvGet2D(image, i, j).val[0]; int B5 = cvGet2D(image, i, j).val[1];
			int R6 = cvGet2D(image, i, j+1).val[2]; int G6 = cvGet2D(image, i, j+1).val[0]; int B6 = cvGet2D(image, i, j+1).val[1];
			
			int R7 = cvGet2D(image, i+1, j-1).val[2]; int G7 = cvGet2D(image, i+1, j-1).val[0]; int B7 = cvGet2D(image, i+1, j-1).val[1];
			int R8 = cvGet2D(image, i+1, j).val[2]; int G8 = cvGet2D(image, i+1, j).val[0]; int B8 = cvGet2D(image, i+1, j).val[1];
			int R9 = cvGet2D(image, i+1, j+1).val[2]; int G9 = cvGet2D(image, i+1, j+1).val[0]; int B9 = cvGet2D(image, i+1, j+1).val[1];

			bool judge01 = (abs(R5 - R1) >= thres || abs(G5 - G1) >= thres || abs(B5 - B1) >= thres);
			bool judge02 = (abs(R5 - R2) >= thres || abs(G5 - G2) >= thres || abs(B5 - B2) >= thres);
			bool judge03 = (abs(R5 - R3) >= thres || abs(G5 - G3) >= thres || abs(B5 - B3) >= thres);
			bool judge04 = (abs(R5 - R4) >= thres || abs(G5 - G4) >= thres || abs(B5 - B4) >= thres);
			bool judge05 = (abs(R5 - R6) >= thres || abs(G5 - G6) >= thres || abs(B5 - B6) >= thres);
			bool judge06 = (abs(R5 - R7) >= thres || abs(G5 - G7) >= thres || abs(B5 - B7) >= thres);
			bool judge07 = (abs(R5 - R8) >= thres || abs(G5 - G8) >= thres || abs(B5 - B8) >= thres);
			bool judge08 = (abs(R5 - R9) >= thres || abs(G5 - G9) >= thres || abs(B5 - B9) >= thres);

			//只要其中有个不对,则直接为边界点
			bool last = (judge01 == 1 || judge02 == 1 || judge03 == 1 || judge04 == 1 || judge05 == 1 || judge06 == 1 || judge07 == 1 || judge08 == 1);
			if (last )
			{
				temp.row = i;
				temp.column = j;
				Boundary.push_back(temp);
			}
		}
	}


	cv::Mat Img(image, 0);//格式转换

	for (int i = 0; i < Boundary.size(); i++)
	{
		int row = Boundary[i].row;
		int column = Boundary[i].column;

		//设置成绿色
		Img.ptr<uchar>(row)[column * 3] = 0;
		Img.ptr<uchar>(row)[column * 3 + 1] = 255;
		Img.ptr<uchar>(row)[column * 3 + 2] = 0;

	}




	imshow("test", Img);
	
	waitKey(0);
	return 0;
}

  

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

点云实验室lab

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值