解BUG解了好久,还是没找到原因,过不将图片变小就实现了预期
代码如下:

#include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>        // 核心组件
#include<opencv2/highgui/highgui.hpp>  // GUI
#include<opencv2/imgproc/imgproc.hpp>  // 图像处理

using namespace cv;
using namespace std;

void main()
{
	Mat result;
	Mat image = imread("C:\\Users\\Administrator\\Desktop\\test.jpg",0);
	threshold(image,result,127,255,THRESH_BINARY);
	int a_result[10000][2] = {0,0};
	int b_result[10000][2] = {0,0};
	int a = 0;
	int b = 0;
	int height = result.rows;
	int width = result.cols;
	for (int i=0;i<height;i++)
	{
		uchar* ro = result.ptr<uchar>(i);
		for (int j=0;j<width;j++)
		{
			if (ro[j] == 0)
			{
				a_result[a][0] = i;
				a_result[a][1] = j;
				a++;
			}
			else
			{
				b_result[b][0] = i;
				b_result[b][1] = j;
				b++;
			}
		}
	}
	for (int i = 0; i < b; i++)
	{
		int min_distance = 255;
		for (int j = 0; j < a; j++)
		{
			int distance = sqrt(pow(b_result[i][0] - a_result[j][0], 2) + pow(b_result[i][1] - a_result[j][1], 2));
			if (distance < min_distance)
			{
				min_distance = distance;
			}
		}
		if (min_distance > 255)
		{
			min_distance = 255;
		}
		int row_result = b_result[i][0];
		int col_result = b_result[i][1];
		uchar* i_result = result.ptr<uchar>(row_result);
		i_result[col_result] = min_distance;
		//result.at<uchar>(row_result, col_result) = min_distance;
	}
	cout << result;
	imshow("test", result*5);
	waitKey(0);
}
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐