티스토리 뷰

영상처리/OpenCV

21.Line fitting

빠리빵 2019. 7. 8. 22:13

 

 

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core.hpp>
#include <iostream>

using namespace std;
using namespace cv;

#define PI 3.1415926

int main() {
	Mat input = imread("road.jpg", IMREAD_GRAYSCALE);
	Mat contours;
	vector<Vec4i> lines;

	// applying canny algorithm
	Canny(input, contours, 125, 350);

	/************* Probabilistic hough transform *************/
	HoughLinesP(contours,	// InputArray
		lines,				// OutputArray
		1,					// rho
		PI / 180,			// theta (step size)
		60,					// threshold (minimum number of votes)
		100,				// minLength
		20);				// maxGap

	Mat houghLinesP;
	input.copyTo(houghLinesP);

	// Draw the lines
	Scalar color = Scalar(255, 255, 255);
	vector<Vec4i>::const_iterator it = lines.begin();
	while (it != lines.end()) {
		Point pt1((*it)[0], (*it)[1]);
		Point pt2((*it)[2], (*it)[3]);
		line(houghLinesP, pt1, pt2, color);
		++it;
	}

	imwrite("houghLinesP.bmp", houghLinesP);

	/************* Fitting a line *************/
	// black background
	Mat oneline(contours.size(), CV_8U, Scalar(0));
	
	// Select first line and draw white line
	int n = 0;	
	line(oneline,
		Point(lines[n][0], lines[n][1]),
		Point(lines[n][2], lines[n][3]),
		Scalar(255),
		3);	// line width

	imwrite("[BeforeBitwise]online.bmp", oneline);

	// contours AND white line
	bitwise_and(contours, oneline, oneline);	// src1, src2, dst

	// insert into a vector<Point>
	vector<Point> points;

	// Iterate over the pixels to obtain all point positions
	for (int y = 0; y < oneline.rows; y++) {
		// row y
		uchar* rowPtr = oneline.ptr<uchar>(y);

		for (int x = 0; x < oneline.cols; x++) {
			// column x
			if (rowPtr[x]) {	// if on a contour
				points.push_back(Point(x, y));
			}
		}
	}

	Vec4f line;
	fitLine(points, line,
		DIST_L2,		// distance type
		0,				// not used with L2 distance
		0.01, 0.01);	// accuracy

	int x0 = line[2];	// a point on the line
	int y0 = line[3];
	int x1 = x0 + 150 * line[0];
	int y1 = y0 + 150 * line[1];
	// draw the line
	Mat result;
	input.copyTo(result);
	cv::line(result, Point(x0, y0), Point(x1, y1), 
		0, 2);	// color and thickness


	/************* Image writer *************/
	imwrite("input.bmp", input);
	imwrite("contours.bmp", contours);
	imwrite("[AfterBitwise]online.bmp", oneline);
	imwrite("Fitted line.bmp", result);
}

'영상처리 > OpenCV' 카테고리의 다른 글

20. Hough Transform  (0) 2019.07.06
19. Canny operator  (0) 2019.06.12
18. Bilateral filter  (0) 2019.06.02
17. Maximally stable external regions  (0) 2019.06.02
16. watershed transformation  (0) 2019.05.28
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함