简介
有时候需要提取图像颜色交界处的坐标,有个比较巧妙的思路是先做图像增强,通过去噪,连通域等处理,分为黑白二值图,然后再用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;
}
输出的部分坐标点