PDA

View Full Version : How to get specific point and draw Rectangle on QImage?



YDYD
16th July 2014, 04:54
Hi all ,

I am currently working on robot vision system,

I would like to track something using Rasp-pi with Webcam.

After i process my image, i would like to box the part i needed ask the pic shown,

How can i do to achieve this? 10518

Would like to rectangle the biggest part.

I did use opencv libs to find contours and get cvpoint, but i cant get exactly what i want.

please advise

Thanks in advance

stampede
16th July 2014, 07:38
I did use opencv libs to find contours and get cvpoint, but i cant get exactly what i want.
Can you show us your code ?

i would like to box the part i needed ask the pic shown,
Do you want to copy a sub-region of image ? For QImages, use QImage::copy() method. For OpenCV, it depends - if you are using C++ interface, use OpenCV::Mat::copyTo method, and for C interface you can set ROI on source image (cvSetImageROI) and copy with cvCopy function.

YDYD
16th July 2014, 08:22
void Dialog::createCam()
{
timer = new QTimer(this);
cam = cvCaptureFromCAM(-1);
if(cam==NULL)
qDebug()<<"Error: No Camera Detect.";

timer->start(33);
connect(timer,SIGNAL(timeout()),this,SLOT(getFrame ()));
connect(timer,SIGNAL(timeout()),this,SLOT(prcFrame ()));
}

void Dialog::getFrame()
{
frame = cvQueryFrame(cam); //get what camera see
image = QImage ((const uchar*)frame->imageData,frame->width,frame->height,QImage::Format_RGB888).rgbSwapped();//rgbSwapped() make color better

qPainter = new QPainter(&image);// all qPainter to draw rectangle on original image according to point get from Filter image
qPainter->begin(this);
qPainter->setPen(QPen(Qt::red,3,Qt::SolidLine));
qPainter->drawRect(rect.x,rect.y,rect.x+rect.width,rect.y+re ct.height);
qPainter->end();
ui->original->setPixmap(QPixmap::fromImage(image));

}

void Dialog::prcFrame()
{
imgHSV= cvCloneImage(frame);
cvCvtColor(frame,imgHSV,CV_BGR2HSV);
imgFilter= cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,1);
cvInRangeS(imgHSV,cvScalar(ui->hueSlide1->value(),ui->satSlide1->value(),ui->lumSlide1->value(),0),cvScalar(ui->hueSlide2->value(),ui->satSlide2->value(),ui->lumSlide2->value(),0),imgFilter);
QImage imgThresdhed = QImage ((const uchar*)imgFilter->imageData,imgFilter->width,imgFilter->height,QImage::Format_Indexed8).rgbSwapped();
ui->filter->setPixmap(QPixmap::fromImage(imgThresdhed)); //convert to QImage and show as Filter image

unsigned char *data_hsv= (unsigned char*)imgHSV->imageData;
int step_hsv = imgHSV->widthStep/sizeof(unsigned char), chanels_hsv=imgHSV->nChannels;
storage=cvCreateMemStorage(0);

cvFindContours(imgFilter, storage, &contour, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE,cvPoint(0,0)); //FindContours

for(;contour;contour = contour->h_next)
{
area1=fabs(cvContourArea(contour,CV_WHOLE_SEQ,1 ));

if(area1<100 || area1>50000 )
{
cvSeqRemove(contour,0);
continue;
}

total =0;
total_S=0;
total_V=0;

for(l = 0;l<contour->total;++l)
{
pt = (CvPoint *)cvGetSeqElem(contour,l);
qDebug()<<"pt"<<pt;
H = data_hsv[step_hsv*pt->y+chanels_hsv*pt->x+0];
qDebug()<<"H"<<H;
S = data_hsv[step_hsv*pt->y+chanels_hsv*pt->x+1];
qDebug()<<"S"<<S;
V = data_hsv[step_hsv*pt->y+chanels_hsv*pt->x+2];
qDebug()<<"V"<<V;
total = H + total;
total_S= S + total_S;
total_V= V + total_V;
}

avg = total / (contour->total);
avg_S=total_S / (contour->total);
avg_V=total_V / (contour->total);

if((avg>=ui->hueSlide1->value())&&(avg<=ui->hueSlide2->value())&&(avg_S>=ui->satSlide1->value())&&(avg_S<=ui->satSlide2->value())&&(avg_V>=ui->lumSlide1->value())&&(avg_V<=ui->lumSlide2->value())) // compare contours size
{
for(i = N-1; i >= 0; --i)
{
if(area1 > maxArea1[i])
{
maxArea1[i] = area1;
contours1[i] = contour;
for(m = (i-1); m >= 0; --m)
{
if(maxArea1[m] < maxArea1[m+1])
{
tmp_area1 = maxArea1[m+1];
tmp_cont = contours1[m+1];
maxArea1[m+1] = maxArea1[m];
contours1[m+1] = contours1[m];
maxArea1[m] = tmp_area1;
contours1[m] = tmp_cont;
}
}
break;
}
}
}
}
rect = ((CvContour*)contours1[0])->rect; //rect point


cvReleaseMemStorage(&storage);
cvReleaseImage(&imgFilter);
cvReleaseImage(&imgHSV);
}

stampede
17th July 2014, 07:36
Can you explain what you mean by "i would like to box the part i needed ask the pic shown," ?
If I'm correct, it looks like you want to draw a bounding box around the contours with the largest area, but I can only guess. Have you verified your sorting procedure ?

YDYD
17th July 2014, 07:40
Can you explain what you mean by "i would like to box the part i needed ask the pic shown," ?
If I'm correct, it looks like you want to draw a bounding box around the contours with the largest area, but I can only guess. Have you verified your sorting procedure ?

Yes,exactly [want to draw a bounding box around the contours with the largest area].

What do you mean by verified my sorting?
Do you mean the sorting of all the function?

stampede
17th July 2014, 08:23
What do you mean by verified my sorting?
It can't be more clear - have you checked that the list is sorted after the sorting code :)