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:this is my code
<pre lang="C++">#include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <highgui.h>
#include <stdlib.h>
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
int thresh = 90;
int main(int argc, char* argv[])
{
VideoCapture cap(1);
if (!cap.isOpened()) {
cout << "Cannot open the video cam" << endl;
return -1;
}
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
cout << "Frame size : " << dWidth << " x " << dHeight << endl;
namedWindow("MyVideo", CV_WINDOW_AUTOSIZE);
while (1)
{
Mat frame, canny_output;
bool bSuccess = cap.read(frame);
imshow("MyVideo", frame);
cvtColor(frame, frame, CV_RGB2GRAY);
blur(frame, frame, Size(5, 5));
frame = frame < 128;
vector<vector<Point> > contours;
vector<Vec4i> 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<Point> approxShape;
for (size_t i = 0; i < contours.size(); i++){
approxPolyDP(contours[i], approxShape, arcLength(Mat(contours[i]), true)*0.4, true);
drawContours(drawing, contours, i, Scalar(0, 255, 255), CV_FILLED);
float ctArea = cv::contourArea(contours[i]);
if (ctArea > 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 << roi.width;
cout << "\n";
if (!bSuccess) {
cout << "Cannot read a frame from video stream" << endl;
break;
}
imshow("MyVideo1", drawing);
<b><b></b></b>
if (waitKey(30) == 27) {
cout << "esc key is pressed by user" << 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));