本文介绍了如何在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 "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); // open the video camera no. 0
if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the video cam" << 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 << "Frame size : " << dWidth << " x " << dHeight << endl;
namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
while (1)
{
Mat frame, canny_output;
bool bSuccess = cap.read(frame); // read a new frame from video
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.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 > 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) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
imshow("MyVideo1", drawing); //show the frame in "MyVideo" window
<b><b></b></b>
//show the frame in "MyVideo" window
if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << 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中绘制线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!