变换失败,不过对大数组进行了操作,最后变换出了一条非预期直线

代码如下:

#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 show_image(Mat image)
{
	imshow("test", image);
	waitKey(0);
}

void main()
{
	Mat image;
	Mat result;
	Mat img;
	Mat result_image;
	result_image = imread("C:\\Users\\Administrator\\Desktop\\test.jpg");
	image = imread("C:\\Users\\Administrator\\Desktop\\test.jpg",0);
	threshold(image,result,127,255,THRESH_BINARY_INV);
	Canny(result, img, 50, 150, 3);
	int height = img.rows;
	int width = img.cols;
	int value;
	int (*p)[10000] = new int[180][10000]();
	//memset(value_list, 0, sizeof(value_list));
	int result_list[100][2] = { 0,0 };
	int a = 0;
	for (int i=1;i< height;i++)
	{
		uchar* updata = img.ptr<uchar>(i);
		for (int j=1;j<width;j++)
		{
			if (updata[j]!=0)
			{
				for (int m=1;m<90;m++)
				{
					value = tan(m) * i + j;
					if (value > 0)
					{
						p[m][value] += 1;
					}
				}
			}
		}
	}
	for (int i=0;i<180;i++)
	{
		for (int j=0;j<10000;j++)
		{
			if (p[i][j]>100)
			{
				result_list[a][0] = i;
				result_list[a][1] = j;
				a++;
			}
		}
	}

	for (int i=0;i<a;i++)
	{
		int theta = result_list[i][0];
		int b = result_list[i][1];
		int y1 = tan(theta) * 1 + b;
		int y2 = tan(theta) * 500 + b;
		Point p0 = Point(1,y1);
		Point p1 = Point(500, y2);
		line(img, p0, p1, cv::Scalar(255), 3, 4);
	}
	show_image(img);
}
Logo

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

更多推荐