Click here to Skip to main content
15,916,398 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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(
C++
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>

)

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));
Posted
Updated 26-May-16 4:33am

1 solution

Drawing a diagonal in a rectangle is simply a matter of drawing from the point [top,left] to the point [bottom, right]. Or [bottom,left] to [top,right].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900