24,854
社区成员
发帖
与我相关
我的任务
分享
#include<opencv2/opencv.hpp>
#include<iostream>
#define CV_PI 3.1415926535897932384626433832795
using namespace cv;
//hough line
int main()
{
Mat srcImage = imread("C:\\Users\\zhouchong\\Desktop\\1.jpg");
if (srcImage.empty())
{
std::cout << "load wrong";
return 0;
}
Mat midImage, dstImage;
Canny(srcImage, midImage, 50, 200, 3);
cvtColor(midImage, dstImage, CV_GRAY2BGR);
std::vector<Vec2f>lines;
HoughLines(midImage, lines, 1, CV_PI / 180, 150, 0, 0);
for (size_t i = 0; i < lines.size(); i++)
{
/*cout << lines[i][0] << endl << lines[i][1] << endl;
Vec2f tem = lines.at(i);
float rho0 = tem[0];
float theta = tem[1];*/
Point pt1, pt2;
double a = cos(lines[i][0]), b = sin(lines[i][1]);
double x0 = a * lines[i][0], y0 = b * lines[i][0];
pt1.x = cvRound(x0 + 1000 * (-b));
pt1.y = cvRound(y0 + 1000 * a);
pt2.x = cvRound(x0 - 1000 * (-b));
pt2.y = cvRound(y0 - 1000 * a);
line(dstImage, pt1, pt2, Scalar(55, 100, 195), 1, LINE_AA);
}
imshow("source pic", srcImage);
imshow("canny result", midImage);
imshow("result pic", dstImage);
waitKey(0);
return 0;
}
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
//hough line
int main()
{
Mat srcImage = imread("1.jpg");
if (srcImage.empty())
{
std::cout << "load wrong";
return 0;
}
Mat midImage, dstImage;
Canny(srcImage, midImage, 50, 200, 3);
cvtColor(midImage, dstImage, CV_GRAY2BGR);
std::vector<Vec2f>lines;
HoughLines(midImage, lines, 1, CV_PI / 180, 150, 0, 0);
for (size_t i = 0; i < lines.size(); i++)
{
float rho = lines[i][0], theta = lines[i][1];
Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a * rho, y0 = b * rho;
pt1.x = cvRound(x0 + 1000 * (-b));
pt1.y = cvRound(y0 + 1000 * a);
pt2.x = cvRound(x0 - 1000 * (-b));
pt2.y = cvRound(y0 - 1000 * a);
line(dstImage, pt1, pt2, Scalar(55, 100, 195), 1, LINE_AA);
}
imshow("source pic", srcImage);
imshow("canny result", midImage);
imshow("result pic", dstImage);
waitKey(0);
return 0;
}