HoughLines는 ret_line에 값을 넣기에 빈 그림에 따로 정보를 넣어 줘야 한다. 참고로 canny에 그릴 수 있지만 본인의 roi를 설정하여 그릴 수 있다. 베스트는 roi에서 검출만 하고 이를 bitwise하여 빈 화면에 넣는 것. 그리고 add_weight를 하여 원래 화면과 합치는 것 까지 하면 완벽할듯 하다.
#include <opencv4/opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat img = imread("line.jpeg");
Mat img_gray;
cvtColor(img, img_gray, COLOR_BGR2GRAY);
Mat img_canny;
Canny(img_gray, img_canny, 150, 255);
vector<Vec2f> lines;
HoughLines(img_canny, lines, 1, CV_PI / 180, 200);
Mat img_hough;
img.copyTo(img_hough);
Mat img_lane;
threshold(img_canny, img_lane, 150, 255, THRESH_MASK);
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(img_hough, pt1, pt2, Scalar(0,0,255), 2, 8);
line(img_lane, pt1, pt2, Scalar::all(255), 1, 8);
}
imshow("img_canny", img_hough);
imshow("img_lane", img_lane);
waitKey(0);
return 0;
return 0;
}