gpt4 book ai didi

c++ - OpenCV c++ HoughLines 转换不起作用

转载 作者:行者123 更新时间:2023-11-28 06:22:07 27 4
gpt4 key购买 nike

我在 visual studio express 2012 中使用 opencv 2.4.10 在 32 位操作系统上的 windows 7 上使用 c++ 进行编码,我正在创建一个程序,该程序将从相机中抓取帧,检测边缘,然后进行一些测量基于这些边缘的像素位置和长度,但是在成功进行精明的边缘检测之后,houghlines 或 findcontours 似乎不起作用。下面是我的一些代码和相关的错误,这是怎么回事?

Mat src, srcEdge, ROImat, templ;
double lowThreshold = 105, highThreshold = lowThreshold*2.2;
int roi_xstart = 140, roi_ystart = 180, roi_width = 360, roi_height = 140, counter = 0, badpartcounter = 0;
Rect ROIrect = cvRect(roi_xstart, roi_ystart, roi_width, roi_height);
Rect matchrect = cvRect(roi_xstart - 10, roi_ystart - 10, roi_width + 20, roi_height + 20);

int main ( int argc, const char** argv )
{
int input;
if (goodpart == true)
{
while (looper)
{
cout<<"1. Capture Image\n";
cout<<"2. Detect Edges of image/create template if first image capture/read\n";
cout<<"3. Compare Template with Captured/Read Image\n";
cout<<"4. Reset counter to reset template\n";
cout<<"5. Read Image\n";
cout<<"6. HoughLines\n";
cout<<"7. Find Contours of img\n";
cout<<"8. Exit program\n";
cin >> input;
switch (input)
{
case 1:
src = FrameCapture();
break;
case 2:
srcEdge = CannyEdge ( src, templ);
cout << "counter = " << counter << endl;
break;
case 3:
MatchingMethod ( srcEdge, templ, goodpart, badpartcounter);
break;
case 4:
counter = 0;
break;
case 5:
src = ReadImage();
imshow (window1, src);
waitKey(0);
destroyWindow(window1);
break;
case 6:
HoughLineTransform (srcEdge);
break;
case 7:
ContourFinding (srcEdge);
break;
case 8:
looper = false;
break;
default:
cout << "incorrect input"<< endl;
}
destroyWindow(window1);
}
}else
{
looper = false;
}
return 0;
}

这是我创建的函数

//blur image, detect edges, result will be binary (black/white) image of edges detected
Mat CannyEdge (Mat& src, Mat& templ)
{
//declare matrices to be used in edge detection
Mat dst;
Mat src_gray;
Mat detected_edges;
//dst is same size as src, all zeros, 8bit 1 channel
dst.zeros(src.rows, src.cols, CV_8UC1);
//convert src from 8 bit 3 channel to 8 bit 1 channel grayimage, output is to src_gray
cvtColor( src, src_gray, CV_RGB2GRAY);
//applies normal blur to image to reduce image noise using a kernel of size 3, takes 8bit 1 channel grayimage of src_gray, blurs it and outputs it to detected_edges
blur( src_gray, detected_edges, Size(3,3) );
//applies canny algorithm for edge detection, takes input of detected edges and outputs back to same matrix
Canny ( detected_edges, detected_edges, lowThreshold, highThreshold, 3 );
//copies image to dst with the mask output from the canny edge detection function, so every pixel that doesn't fit the mask drops to 0, leaving the edges
src_gray.copyTo(dst, detected_edges);
//displays image of edges
threshold (dst, dst, 100, 255, 0);
namedWindow(window1, CV_WINDOW_AUTOSIZE);
imshow ( window1, dst);
waitKey(0);
destroyWindow(window1);
//if this is the first image taken, it creates a template from the specified region of interest to compare with the next images taken
if (counter == 0)
{
Mat ROImat (dst, ROIrect);
threshold (ROImat, ROImat, 100, 255, 0);
imshow ( window2, ROImat);
waitKey(0);
destroyWindow(window2);
ROImat.copyTo (templ);
}
counter++;
return dst;
}

//find countours
void ContourFinding (Mat srcEdge)
{

vector<vector<Point> > contouroutput;
vector<Vec4i> hierarchy;
Point ROIstart;
ROIstart.x = roi_xstart, ROIstart.y = roi_ystart;
Mat contourinput (srcEdge, ROIrect);
Mat contourimage = Mat::zeros(contourinput.size(), CV_8UC3);
findContours(contourinput, contouroutput, hierarchy, 0, 1, ROIstart);
int idx = 0;
for( ; idx >= 0; idx = hierarchy[idx][0] )
{
Scalar color( rand()&255, rand()&255, rand()&255 );
drawContours( contourimage, contouroutput, idx, color, CV_FILLED, 8, hierarchy, 2, ROIstart);
}

namedWindow( "Components", 1 );
imshow( "Components", contourinput);
waitKey(0);

}

//Hough Line Transformation function
void HoughLineTransform (Mat srcEdge)
{
Mat cdst;
Mat hdst (srcEdge, matchrect);
vector<Vec4i> lines;
HoughLinesP( hdst, lines, 1, CV_PI/180, 150, 30, 10 );
for( size_t i = 0; i < lines.size(); i++ )
{
line( cdst, Point(lines[i][0], lines[i][1]),
Point(lines[i][2], lines[i][3]), Scalar(0,0,255), 3, 8 );
}
}

这是轮廓抛出的错误

Unhandled exception at 0x54CD1600 (opencv_core2410.dll) in Template Matching.exe: 0xC0000005: Access violation reading location 0x00389738.

然后当我去反汇编的时候

00BD4B8E E8 C1 3B 00 00       call        cv::findContours (0BD8754h)  
00BD4B93 83 C4 1C add esp,1Ch

第二行是“下一个要执行的命令”

然后是休夫

Unhandled exception at 0x008952D3 in Template Matching.exe: 0xC0000005: Access violation reading location 0x0036E004.

在反汇编中

008952CD 89 A5 50 FE FF FF    mov         dword ptr [ebp-1B0h],esp  
008952D3 8B 08 mov ecx,dword ptr [eax]

最后一行是下一条要执行的语句我不熟悉指针堆栈、内存分配或汇编代码

最佳答案

所以这是一个非常简单的修复,在 Debug模式下我包含了所有额外的库,即

opencv_core2410.lib

opencv_core2410d.lib

对于所有模块,删除发布库并仅保留 opencv_core2410d.lib 等,一切正常。当我最初设置项目属性时,我同时配置了调试和 Release模式,因此包括了每个库。

关于c++ - OpenCV c++ HoughLines 转换不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29157538/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com