Draw rectangle issue (detection and tracking)
I'm tracking rectangles and the algorithm is working fine, until I try
draw a rectangle around the edges of the detected retangle and it crash.
Without drawing works normally. What's wrong about my code?
#include <cv.h>
#include <highgui.h>
//x1 = r1[0].x x3 = r2[0].x
//x2 = r1[1].x x4 = r2[1].x
//y1 = r1[0].y y3 = r2[0].y
//y2 = r1[1].y y4 = r2[1].y
int main(int argc, char** argv)
{
CvCapture *cam = cvCreateCameraCapture(0);
IplImage* img = cvQueryFrame(cam);
IplImage* aux1 = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);
CvMemStorage* storage = cvCreateMemStorage(0);
CvMemStorage* storage1 = cvCreateMemStorage(0);
CvSeq* lines = 0;
CvSeq* seq= cvCreateSeq(0, sizeof(CvSeq), sizeof(CvPoint), storage1);
CvRect rect;
int i,j,d;
while(1)
{
img = cvQueryFrame(cam);
if(!img) break;
cvCvtColor(img,aux1,CV_RGB2GRAY);
cvCanny( aux1, aux1, 100, 100, 3 );
lines=cvHoughLines2(aux1, storage, CV_HOUGH_PROBABILISTIC, 1,
CV_PI/2, 50, 30, 70);
for( i = 0; i < lines->total; i++ )
{
CvPoint* r1 = (CvPoint*)cvGetSeqElem(lines,i);
for( j=i+1; j < lines->total; j++ )
{
CvPoint* r2 = (CvPoint*)cvGetSeqElem(lines,j);
d =
(r1[0].x-r1[1].x)*(r2[1].x-r2[1].x)-(r1[0].y-r1[1].y)*(r2[0].x-r2[1].x);
if (d != 0)
{
CvPoint corner =
cvPoint(((r1[0].x*r1[1].y-r1[0].y*r1[1].x)*(r2[0].x-r2[1].x)-(r1[0].x-r1[1].x)*(r2[0].x*r2[1].x
-
r2[1].x*r2[1].x))/d,((r1[0].x*r1[1].y-r1[0].y*r1[1].x)*(r2[1].x-r2[1].x)-(r1[0].y-r1[1].y)*(r2[0].x*r2[1].x
- r2[1].x*r2[1].x))/d);
cvSeqPush(seq,&corner);
}
}
for(; seq!=0; seq=seq->h_next)
{
rect=cvBoundingRect(seq, 0);
cvRectangle(img,cvPoint(rect.x,
rect.y),cvPoint(rect.x+rect.width,
rect.y+rect.height),cvScalar(0,0,255,0), 2, 6, 0);
}
}
cvShowImage("imagem", img);
if( cvWaitKey(1) == 27 ) break;
}
cvReleaseCapture(&cam);
cvReleaseMemStorage(&storage);
cvReleaseMemStorage(&storage1);
}
It seems to work until a detection occurs, then crashes and closes. Draw
rectangle is only needed for viewing, if I could be sure that the
rectangles are stored properly in rect would already be enough.
Thanks for the help and sorry about the english.
No comments:
Post a Comment