我需要对我正在进行的项目提供一些反馈。基本上我需要能够从来自摄像机的视频流中跟踪矩形形状。我正在使用OpenCV库来实现C ++。 首先,我应用了颜色检测(我需要一次只跟踪1种颜色)。之后,我应用Canny边缘检测以获得滤波图像的轮廓。在这一点上,我应该能够确定是否存在矩形,它在(x,y)平面中的位置(知道它的中心或顶点的位置不会是毕竟这么大的差异)和它对这样一个平面的定位。 所有这一切都应该在"实时"在某种意义上,由于信号来自相机,我需要能够跟踪流中显示的这些形状特征。
这是通过Canny边缘检测算法检测到的颜色选择(左)及其轮廓后输入的阈值版本的图片
这是任何人需要它的代码:
#include <sstream>
#include <string>
#include <iostream>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#define HIGH_CANNY_THRESH 255
#define CANNY_KERNEL_SIZE 3
#define FRAME_WIDTH 640
#define FRAME_HEIGHT 480
#define DISPLAY_IMAGES true
using namespace std;
using namespace cv;
void createTrackbarsForHSVSel();
void morphOps(Mat &thresh);
int LOW_H = 0;
int HIGH_H = 255;
int LOW_S = 0;
int HIGH_S = 255;
int LOW_V = 0;
int HIGH_V = 255;
int LOW_THRESHOLD = 0;
int HIGH_THRESHOLD = 100;
int CORNER_THRESH = 200;
int MAX_CORNER_THRESH = 255;
int main(int argc, char* argv[])
{
Mat src, hsvSpace, threshold, edges;
vector<vector<Point> > contours; // Vectors for the contours storage
vector<Vec4i> hierarchy;
createTrackbarsForHSVSel(); // create trackbars for the HSV palette
createTrackbar("Min Threshold", "Trackbars", &LOW_THRESHOLD , HIGH_THRESHOLD);
createTrackbar("Max Threshold", "Trackbars", &HIGH_THRESHOLD, HIGH_THRESHOLD);
VideoCapture capture;
capture.open(0);
printf("Starting to capture from camera0:\nisOpened = %d\n", capture.isOpened());
capture.set(CV_CAP_PROP_FRAME_WIDTH,FRAME_WIDTH);
capture.set(CV_CAP_PROP_FRAME_HEIGHT,FRAME_HEIGHT);
while(1) // loop exectues as long as the user doesn't press ESC, q or Q
{
capture.read(src); // read from camera
cvtColor(src, hsvSpace, CV_BGR2HSV); // RGB to HSV color space transformation
// create a binary such that 1s are between Scalar(min_, min_, min_) and Scalar(max_, max_, max_)
inRange(hsvSpace, Scalar(LOW_H, LOW_S, LOW_V), Scalar(HIGH_H, HIGH_S, HIGH_V), threshold);
morphOps(threshold); // morphological operations: they allow to close the 'hole' and delete the 'dots'
// threshold now contains the binary that only displays one colour (if the trackbars are set correctly)
// Apply Gaussian blurring and Canny edge algorithm for the edge detection
GaussianBlur(threshold, threshold, Size(3,3), 0, 0); // Kernel = 3x3, Sigmas are calculated automatically (see 'getGaussianKernel()')
Canny(threshold, edges, LOW_THRESHOLD, HIGH_THRESHOLD);
/*
Algorithm that approximates the edges of the figure to a rectangle.
After that it needs to be able to calculate the rectangle position and orientation
(will something like RotatedRect be useful?)
*/
#if DISPLAY_IMAGES == true
// Show images
imshow("Camera feed", src);
imshow("Thresholded", threshold);
imshow("Edges", edges);
#endif
if((char)waitKey(30) == 'q')
break;
}
return 0;
}
void createTrackbarsForHSVSel()
{
namedWindow("Trackbars", CV_WINDOW_AUTOSIZE);
createTrackbar("Low hue", "Trackbars", &LOW_H , HIGH_H );
createTrackbar("High hue", "Trackbars", &HIGH_H, HIGH_H );
createTrackbar("Low sat", "Trackbars", &LOW_S , HIGH_S );
createTrackbar("High sat", "Trackbars", &HIGH_S, HIGH_S );
createTrackbar("Low val", "Trackbars", &LOW_V , HIGH_V );
createTrackbar("High val", "Trackbars", &HIGH_V, HIGH_V );
return;
}
void morphOps(Mat &thresh)
{
// create structuring element that will be used to "dilate" and "erode" image.
// the element chosen here is a 3px by 3px rectangle.
// As a rule of thumb you want to dilate with larger element to make sure the object is nicely visible
erode (thresh,thresh,getStructuringElement( MORPH_RECT, Size(3,3)));
dilate(thresh,thresh,getStructuringElement( MORPH_RECT, Size(3,3)));
dilate(thresh,thresh,getStructuringElement( MORPH_RECT, Size(3,3)));
erode (thresh,thresh,getStructuringElement( MORPH_RECT, Size(3,3)));
return ;
}
感谢您的帮助!
答案 0 :(得分:0)
如果在样本上显示的阈值处理后定义了矩形,那么这是一个非常基本的任务。
(x,y), (width, height), theta
。在生气之前,请记得查看this之前的角度很奇怪。应该实时工作没问题。如果你对某些东西进行无效编码而它没有,那么就每隔2或3帧进行一次处理。