Пример #1
0
/** This function contains the actions performed for each image*/
void processImage(MSAC &msac, int numVps, cv::Mat &imgGRAY, cv::Mat &outputImg)
{
	cv::Mat imgCanny;

	// Canny
	cv::Canny(imgGRAY, imgCanny, 180, 120, 3);

	// Hough
	vector<vector<cv::Point> > lineSegments;
	vector<cv::Point> aux;
#ifndef USE_PPHT
	vector<Vec2f> lines;
	cv::HoughLines( imgCanny, lines, 1, CV_PI/180, 200);

	for(size_t i=0; i< lines.size(); i++)
	{
		float rho = lines[i][0];
		float theta = lines[i][1];

		double a = cos(theta), b = sin(theta);
		double x0 = a*rho, y0 = b*rho;

		Point pt1, pt2;
		pt1.x = cvRound(x0 + 1000*(-b));
		pt1.y = cvRound(y0 + 1000*(a));
		pt2.x = cvRound(x0 - 1000*(-b));
		pt2.y = cvRound(y0 - 1000*(a));

		aux.clear();
		aux.push_back(pt1);
		aux.push_back(pt2);
		lineSegments.push_back(aux);

		line(outputImg, pt1, pt2, CV_RGB(0, 0, 0), 1, 8);
	
	}
#else
	vector<Vec4i> lines;	
	int houghThreshold = 70;
	if(imgGRAY.cols*imgGRAY.rows < 400*400)
		houghThreshold = 100;		
	
	cv::HoughLinesP(imgCanny, lines, 1, CV_PI/180, houghThreshold, 10,10);

	while(lines.size() > MAX_NUM_LINES)
	{
		lines.clear();
		houghThreshold += 10;
		cv::HoughLinesP(imgCanny, lines, 1, CV_PI/180, houghThreshold, 10, 10);
	}
	for(size_t i=0; i<lines.size(); i++)
	{		
		Point pt1, pt2;
		pt1.x = lines[i][0];
		pt1.y = lines[i][1];
		pt2.x = lines[i][2];
		pt2.y = lines[i][3];
		line(outputImg, pt1, pt2, CV_RGB(0,0,0), 2);
		/*circle(outputImg, pt1, 2, CV_RGB(255,255,255), CV_FILLED);
		circle(outputImg, pt1, 3, CV_RGB(0,0,0),1);
		circle(outputImg, pt2, 2, CV_RGB(255,255,255), CV_FILLED);
		circle(outputImg, pt2, 3, CV_RGB(0,0,0),1);*/

		// Store into vector of pairs of Points for msac
		aux.clear();
		aux.push_back(pt1);
		aux.push_back(pt2);
		lineSegments.push_back(aux);
	}
	
#endif

	// Multiple vanishing points
	std::vector<cv::Mat> vps;			// vector of vps: vps[vpNum], with vpNum=0...numDetectedVps
	std::vector<std::vector<int> > CS;	// index of Consensus Set for all vps: CS[vpNum] is a vector containing indexes of lineSegments belonging to Consensus Set of vp numVp
	std::vector<int> numInliers;

	std::vector<std::vector<std::vector<cv::Point> > > lineSegmentsClusters;
	
	// Call msac function for multiple vanishing point estimation
	msac.multipleVPEstimation(lineSegments, lineSegmentsClusters, numInliers, vps, numVps); 
	for(int v=0; v<vps.size(); v++)
	{
		printf("VP %d (%.3f, %.3f, %.3f)", v, vps[v].at<float>(0,0), vps[v].at<float>(1,0), vps[v].at<float>(2,0));
		fflush(stdout);
		double vpNorm = cv::norm(vps[v]);
		if(fabs(vpNorm - 1) < 0.001)
		{
			printf("(INFINITE)");
			fflush(stdout);
		}
		printf("\n");
	}		
		
	// Draw line segments according to their cluster
	msac.drawCS(outputImg, lineSegmentsClusters, vps);
}
Пример #2
0
vector<vector<vector<Point> > > Nieto::vp(const Mat1b &inBinaryFrameFiltrado, Mat3b &inVanishingPointImage, const Rect &roi, const int maxNumLines, int houghThresholdInicial, int houghStep, double houghMinLineLength, double houghMaxLineGap) {

	double tempoInicio = static_cast<double>(getTickCount());

	// pega somente a regi�o de interesse
	Mat1b mascaraRoi = Mat1b(inBinaryFrameFiltrado.size(), uchar(0));
	mascaraRoi(Rect(roi.x, roi.y, roi.width, roi.height)).setTo(255);
	Mat1b binaryFrameFiltradoMasked;
	cv::bitwise_and(inBinaryFrameFiltrado, mascaraRoi, binaryFrameFiltradoMasked);
	if (display) cvtColor(inBinaryFrameFiltrado, inVanishingPointImage, CV_GRAY2BGR);

	// Hough
	vector<vector<Point> > lineSegments;
	vector<Point> aux;
	vector<Vec4i> lines;
	int houghThreshold = houghThresholdInicial;

	cv::HoughLinesP(binaryFrameFiltradoMasked, lines, 1, CV_PI / 180, houghThreshold, houghMinLineLength, houghMaxLineGap);
	while (lines.size() > maxNumLines) {
		lines.clear();
		houghThreshold += houghStep;
		cv::HoughLinesP(binaryFrameFiltradoMasked, lines, 1, CV_PI / 180, houghThreshold, houghMinLineLength, houghMaxLineGap);
	}
	for (size_t i = 0; i<lines.size(); i++)	{
		Point pt1, pt2;
		pt1.x = lines[i][0];
		pt1.y = lines[i][1];
		pt2.x = lines[i][2];
		pt2.y = lines[i][3];

		// modificado
		int dx = abs(pt2.x - pt1.x);
		int dy = abs(pt2.y - pt1.y);
		if (1.0*dx / 3.0 > dy) {
			if (display) line(inVanishingPointImage, pt1, pt2, CV_RGB(0, 0, 255), 1);
			// continue;
		} else{
			if (display) line(inVanishingPointImage, pt1, pt2, CV_RGB(0, 255, 0), 1);
		}

		// Store into vector of pairs of Points for msac
		aux.clear();
		aux.push_back(pt1);
		aux.push_back(pt2);
		lineSegments.push_back(aux);
	}

	// Multiple vanishing points
	vector<Mat> vps;			// vector of vps: vps[vpNum], with vpNum=0...numDetectedVps
	vector<vector<int> > CS;	// index of Consensus Set for all vps: CS[vpNum] is a vector containing indexes of lineSegments belonging to Consensus Set of vp numVp
	vector<int> numInliers;
	vector<vector<vector<Point> > > lineSegmentsClusters;
	MSAC msac;
	bool msac_verbose = false;
	int numVps = 1;

	msac.init(MODE_NIETO, inBinaryFrameFiltrado.size(), msac_verbose);
	msac.multipleVPEstimation(lineSegments, lineSegmentsClusters, numInliers, vps, numVps); // Call msac function for multiple vanishing point estimation
	msac.drawCS(inVanishingPointImage, lineSegmentsClusters, vps); // Draw line segments according to their cluster

	// sa�das
	this->houghLines = lineSegmentsClusters;
	this->vanishingPoint = vps;

	// calcula o tempo de execu��o
	double tempoFim = static_cast<double>(getTickCount());
	double tempoExecutando = ((tempoFim - tempoInicio) / getTickFrequency()) * 1000;

	// exibe as sa�das definidas (texto e/ou imagem)
	if (verbose) cout << "- nieto.vp: " << tempoExecutando << " ms" << endl;
	if (display) imshow("Vanishing Point", inVanishingPointImage);

	return { lineSegments };
}