提取图像中颜色交界处的坐标

简介

有时候需要提取图像颜色交界处的坐标,有个比较巧妙的思路是先做图像增强,通过去噪,连通域等处理,分为黑白二值图,然后再用3*3的均值滤波将图像处理,原来纯白或者纯黑的地方还是纯色,但是交界处就不再是纯白或者纯黑了,利用这点可以提取出图像的交界点的坐标。
如下图为黑白二值图,边界为黑白交界处的直线,若要提取边界的坐标可以通过一个3*3的均值滤波作用,作用后可以得到图2(交界处不再是纯白或者纯黑)。
在这里插入图片描述
在这里插入图片描述

以下图为例计算其黑白交界处的坐标

示例图点击下载
示例图点击下载

#include<iostream>
#include <opencv2\imgproc\imgproc.hpp>
#include<opencv2\opencv.hpp>
#include<opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
//#include <tools.cpp>

using namespace std;
using namespace cv;

int main()
{
	Mat src, srcGray;
	src = cv::imread("C:\\Users\\Administrator\\Desktop\\test.jpg");
	cv::cvtColor(src, srcGray, COLOR_RGB2GRAY);  // 转灰度图 
	//........
	//图像增强,以下图像需要二值化为0,255
	//........

	int i, j,counts=0;
	float means;
	int *Rows = (int*)malloc(srcGray.rows*srcGray.cols*sizeof(int));
	int *Cols = (int*)malloc(srcGray.rows*srcGray.cols*sizeof(int));
	memset(Rows, 0, srcGray.rows*srcGray.cols*sizeof(int));
	memset(Cols, 0, srcGray.rows*srcGray.cols*sizeof(int));

	for (i = 1; i < srcGray.rows - 1; i++) {
		for (j = 1; j < srcGray.cols - 1; j++) {
			// 3*3均值滤波
			means = (srcGray.at<uchar>(i - 1, j - 1) + srcGray.at<uchar>(i - 1, j) + srcGray.at<uchar>(i - 1, j + 1)
				+ srcGray.at<uchar>(i, j - 1) + srcGray.at<uchar>(i, j) + srcGray.at<uchar>(i, j + 1)
				+ srcGray.at<uchar>(i + 1, j - 1) + srcGray.at<uchar>(i + 1, j) + srcGray.at<uchar>(i + 1, j + 1)) / 9.0;

			// 均值滤波后像素值不为0或者255的是交界处
			if (means>0 && means < 255)
			{
				Rows[counts] = i;
				Cols[counts] = j;
				counts += 1;
				printf("(%d,%d),", i,j);
			}
		}
	}
	
	free(Rows);
	free(Cols);
	return 0;
}

输出的部分坐标点
在这里插入图片描述

  • 2
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

BryceRui

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

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

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

打赏作者

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

抵扣说明:

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

余额充值