本文介绍了如何在opencv中绘制线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有检测对象边缘的代码。使用这个我可以围绕矩形对象绘制线条。但现在我想要画线穿过矩形。我是怎么做到的简单地说。我想要测量矩形的宽度。

这是我的输出图像(

i had code for the detect object edge. using this one i can draw line around rectangle objects. but now i want draw line cross the rectangle. how i do this. simply say. i want measure width of the rectangle.
this is my output image(

https://drive.google.com/file/d/0B_sXHz69wLw0RURrUWRzMVk1d2c/view?usp=sharing
this is my code

<pre lang="C++">#include &quot;stdafx.h&quot;
#include &quot;opencv2/highgui/highgui.hpp&quot;
#include &lt;iostream&gt;
#include &lt;highgui.h&gt;
#include &lt;stdlib.h&gt;
#include &quot;opencv2/opencv.hpp&quot;

using namespace cv;
using namespace std;

int thresh = 90;

int main(int argc, char* argv[])
{
	VideoCapture cap(1); // open the video camera no. 0

	if (!cap.isOpened())  // if not success, exit program
	{
		cout &lt;&lt; &quot;Cannot open the video cam&quot; &lt;&lt; endl;
		return -1;
	}

	double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
	double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video

	

	cout &lt;&lt; &quot;Frame size : &quot; &lt;&lt; dWidth &lt;&lt; &quot; x &quot; &lt;&lt; dHeight &lt;&lt; endl;

	namedWindow(&quot;MyVideo&quot;, CV_WINDOW_AUTOSIZE); //create a window called &quot;MyVideo&quot;
	
	
	while (1)
	{
		
		Mat frame, canny_output;
		
		bool bSuccess = cap.read(frame); // read a new frame from video
	
		imshow(&quot;MyVideo&quot;, frame);

		cvtColor(frame, frame, CV_RGB2GRAY);
		blur(frame, frame, Size(5, 5));
		frame = frame &lt; 128;
		vector&lt;vector&lt;Point&gt; &gt; contours;
		vector&lt;Vec4i&gt; hierarchy;
		Canny(frame, canny_output, thresh, thresh * 2, 3);
		cv::dilate(canny_output, canny_output, cv::Mat(), cv::Point(-1, -1));
		findContours(canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
		Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3);

		int biggestContourIdx = -1;
		float biggestContourArea = 0;
		vector&lt;Point&gt; approxShape;

		for (size_t i = 0; i &lt; contours.size(); i++){
			//approxPolyDP(contours[i], approxShape, arcLength(Mat(contours[i]), true)*0.04, true); tiiba eka 

			approxPolyDP(contours[i], approxShape, arcLength(Mat(contours[i]), true)*0.4, true);
		drawContours(drawing, contours, i, Scalar(0, 255, 255), CV_FILLED);   // fill BLUE
			//cv::Scalar color = cv::Scalar(0, 0, 0);
			//	drawContours( drawing, contours, i, color, 1, 8, hierarchy, 0, cv::Point() );

			float ctArea = cv::contourArea(contours[i]);
			if (ctArea &gt; biggestContourArea + 10)
			{
				biggestContourArea = ctArea;
				biggestContourIdx = i;
			}
		}

		cv::RotatedRect boundingBox = cv::minAreaRect(contours[biggestContourIdx]);
		cv::Point2f corners[4];
		boundingBox.points(corners);
		cv::line(drawing, corners[0], corners[1], cv::Scalar(255, 0, 0));
		cv::line(drawing, corners[1], corners[2], cv::Scalar(255, 0, 0));
		cv::line(drawing, corners[2], corners[3], cv::Scalar(255, 0, 0));
		cv::line(drawing, corners[3], corners[0], cv::Scalar(255, 0, 0));	
		Rect roi;
		roi.width = boundingBox.size.width;
		cout &lt;&lt; roi.width;
		cout &lt;&lt; &quot;\n&quot;;
		
		if (!bSuccess) //if not success, break loop
		{
			cout &lt;&lt; &quot;Cannot read a frame from video stream&quot; &lt;&lt; endl;
			break;
		}

		imshow(&quot;MyVideo1&quot;, drawing); //show the frame in &quot;MyVideo&quot; window

<b><b></b></b>
	 //show the frame in &quot;MyVideo&quot; window

		if (waitKey(30) == 27) //wait for &#39;esc&#39; key press for 30ms. If &#39;esc&#39; key is pressed, break loop
		{
			cout &lt;&lt; &quot;esc key is pressed by user&quot; &lt;&lt; endl;
			break;
		}
	}
	return 0;

}</pre>







什么我试过了:



i尝试链接到角落。但没有成功

那样



cv :: line(绘图,(corner [0] + corner [1])/ 2, corner [1],cv :: Scalar(255,0,0));


)

What I have tried:

i tried link to corners together. but not successfull
like that

cv::line(drawing, (corners[0]+ corners[1])/2, corners[1], cv::Scalar(255, 0, 0));

推荐答案


这篇关于如何在opencv中绘制线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 05:58