Results 1 to 6 of 6

Thread: How to get specific point and draw Rectangle on QImage?

  1. #1
    Join Date
    Jun 2014
    Posts
    33
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question How to get specific point and draw Rectangle on QImage?

    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? snapshot5.png

    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
    Last edited by YDYD; 16th July 2014 at 05:00.

  2. #2
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to get specific point and draw Rectangle on QImage?

    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.

  3. #3
    Join Date
    Jun 2014
    Posts
    33
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Re: How to get specific point and draw Rectangle on QImage?

    Qt Code:
    1. void Dialog::createCam()
    2. {
    3. timer = new QTimer(this);
    4. cam = cvCaptureFromCAM(-1);
    5. if(cam==NULL)
    6. qDebug()<<"Error: No Camera Detect.";
    7.  
    8. timer->start(33);
    9. connect(timer,SIGNAL(timeout()),this,SLOT(getFrame()));
    10. connect(timer,SIGNAL(timeout()),this,SLOT(prcFrame()));
    11. }
    12.  
    13. void Dialog::getFrame()
    14. {
    15. frame = cvQueryFrame(cam); //get what camera see
    16. image = QImage ((const uchar*)frame->imageData,frame->width,frame->height,QImage::Format_RGB888).rgbSwapped();//rgbSwapped() make color better
    17.  
    18. qPainter = new QPainter(&image);// all qPainter to draw rectangle on original image according to point get from Filter image
    19. qPainter->begin(this);
    20. qPainter->setPen(QPen(Qt::red,3,Qt::SolidLine));
    21. qPainter->drawRect(rect.x,rect.y,rect.x+rect.width,rect.y+rect.height);
    22. qPainter->end();
    23. ui->original->setPixmap(QPixmap::fromImage(image));
    24.  
    25. }
    26.  
    27. void Dialog::prcFrame()
    28. {
    29. imgHSV= cvCloneImage(frame);
    30. cvCvtColor(frame,imgHSV,CV_BGR2HSV);
    31. imgFilter= cvCreateImage(cvSize(frame->width,frame->height),IPL_DEPTH_8U,1);
    32. 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);
    33. QImage imgThresdhed = QImage ((const uchar*)imgFilter->imageData,imgFilter->width,imgFilter->height,QImage::Format_Indexed8).rgbSwapped();
    34. ui->filter->setPixmap(QPixmap::fromImage(imgThresdhed)); //convert to QImage and show as Filter image
    35.  
    36. unsigned char *data_hsv= (unsigned char*)imgHSV->imageData;
    37. int step_hsv = imgHSV->widthStep/sizeof(unsigned char), chanels_hsv=imgHSV->nChannels;
    38. storage=cvCreateMemStorage(0);
    39.  
    40. cvFindContours(imgFilter, storage, &contour, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE,cvPoint(0,0)); //FindContours
    41.  
    42. for(;contour;contour = contour->h_next)
    43. {
    44. area1=fabs(cvContourArea(contour,CV_WHOLE_SEQ,1 ));
    45.  
    46. if(area1<100 || area1>50000 )
    47. {
    48. cvSeqRemove(contour,0);
    49. continue;
    50. }
    51.  
    52. total =0;
    53. total_S=0;
    54. total_V=0;
    55.  
    56. for(l = 0;l<contour->total;++l)
    57. {
    58. pt = (CvPoint *)cvGetSeqElem(contour,l);
    59. qDebug()<<"pt"<<pt;
    60. H = data_hsv[step_hsv*pt->y+chanels_hsv*pt->x+0];
    61. qDebug()<<"H"<<H;
    62. S = data_hsv[step_hsv*pt->y+chanels_hsv*pt->x+1];
    63. qDebug()<<"S"<<S;
    64. V = data_hsv[step_hsv*pt->y+chanels_hsv*pt->x+2];
    65. qDebug()<<"V"<<V;
    66. total = H + total;
    67. total_S= S + total_S;
    68. total_V= V + total_V;
    69. }
    70.  
    71. avg = total / (contour->total);
    72. avg_S=total_S / (contour->total);
    73. avg_V=total_V / (contour->total);
    74.  
    75. 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
    76. {
    77. for(i = N-1; i >= 0; --i)
    78. {
    79. if(area1 > maxArea1[i])
    80. {
    81. maxArea1[i] = area1;
    82. contours1[i] = contour;
    83. for(m = (i-1); m >= 0; --m)
    84. {
    85. if(maxArea1[m] < maxArea1[m+1])
    86. {
    87. tmp_area1 = maxArea1[m+1];
    88. tmp_cont = contours1[m+1];
    89. maxArea1[m+1] = maxArea1[m];
    90. contours1[m+1] = contours1[m];
    91. maxArea1[m] = tmp_area1;
    92. contours1[m] = tmp_cont;
    93. }
    94. }
    95. break;
    96. }
    97. }
    98. }
    99. }
    100. rect = ((CvContour*)contours1[0])->rect; //rect point
    101.  
    102.  
    103. cvReleaseMemStorage(&storage);
    104. cvReleaseImage(&imgFilter);
    105. cvReleaseImage(&imgHSV);
    106. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to get specific point and draw Rectangle on QImage?

    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 ?

  5. #5
    Join Date
    Jun 2014
    Posts
    33
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to get specific point and draw Rectangle on QImage?

    Quote Originally Posted by stampede View Post
    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?

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,391
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to get specific point and draw Rectangle on QImage?

    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

Similar Threads

  1. How to draw a rectangle or line on an image
    By Shubham in forum Newbie
    Replies: 6
    Last Post: 18th February 2014, 07:17
  2. Draw rectangle on QImage
    By ^NyAw^ in forum Qt Programming
    Replies: 12
    Last Post: 25th January 2013, 12:33
  3. Replies: 2
    Last Post: 9th November 2012, 17:16
  4. Replies: 1
    Last Post: 3rd November 2009, 22:26
  5. Starting Point of Rectangle in RubberBand implementation
    By cdemirkir in forum Qt Programming
    Replies: 4
    Last Post: 10th July 2007, 15:05

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.