【发布时间】:2016-02-16 05:36:28
【问题描述】:
我是 openCv 和一般图像处理的新手。我需要从这样的相机输入中实时绘制线条及其位置:
我已经有了来自精明边缘检测的图像,但是在应用霍夫线并尝试使用以下代码将其绘制到该图像时,我发现:
int main(int argc, char* argv[]){
Mat input;
Mat HSV;
Mat threshold;
Mat CannyThresh;
Mat HL;
//video capture object to acquire webcam feed
cv::VideoCapture capture;
capture.open(0);
capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
//start an infinite loop where webcam feed is copied to cameraFeed matrix
//all operations will be performed within this loop
while (true){
capture.read(input);
cvtColor(input, HSV, COLOR_BGR2HSV); //hsv
inRange(HSV, Scalar(H_MIN, S_MIN, V_MIN), Scalar(H_MAX, S_MAX, V_MAX), threshold);//thershold
MorphOps(threshold);//morph operations on threshold image
Canny(threshold, CannyThresh, 100, 50); //canny edge detection
std::vector<Vec4i> lines;
HoughLines(CannyThresh, lines, 1, CV_PI/180, 150, 0, 0 );
for( size_t i = 0; i < lines.size(); i++ )
{
float rho = lines[i][0], theta = lines[i][1];
Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
pt1.x = cvRound(x0 + 1000*(-b));
pt1.y = cvRound(y0 + 1000*(a));
pt2.x = cvRound(x0 - 1000*(-b));
pt2.y = cvRound(y0 - 1000*(a));
line( input, pt1, pt2, Scalar(0,0,255), 3, CV_AA);
}
imshow("camera", input);
waitKey(30);
}
return 0;
}
1- 我不能说我真的理解那个代码,但是你能告诉我为什么它不能工作吗?。
2- 如果我设法让它工作,我怎样才能得到水平线的 Y 坐标?我需要知道另一个物体是否在这个物体的内部、下方或上方。所以我需要这张图片上两条水平线在 Y 轴上的位置(检测到的粗线),这样我就可以确定另一个对象在这个“矩形”中的位置。
编辑 #1
我复制了完整的代码。正如您在第二张图片中看到的那样,调试器不会抛出任何错误。但在程序的控制台中显示OpenCV Error:Assertion failed (channels() == CV_MAT_CN(dtype)) in cv::Mat::copyTo, file C:\builds\master_packSlave-Win32-vc12-shared\opencv\modules\core\src\copy.cpp, line 281。调用堆栈中的最后一个调用也是这样的: > KernelBase.dll!_RaiseException@16() Unknown,我开始认为是 opencv 问题而不是代码问题,可能是那个 dll 的问题。
编辑#2
我换行了
std::vector<Vec4i> lines; // this line causes exception
为
std::vector<Vec2f> lines;
现在它进入了 for 循环。但它现在给出了另一个运行时错误(另一个分段错误。我认为它与这些值有关:
我认为他们可能会超出范围,有什么想法吗?
【问题讨论】:
-
使用调试器得到错误信息
-
用调试器信息编辑。
-
您可能在发行版中使用调试库,反之亦然。或使用其他编译器或体系结构(x86 与 x64)编译的库。
标签: c++ opencv image-processing real-time hough-transform