Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

how can I find necessary rectangle? or how can I find the width

using the code I can find contour, then draw a rectangle and write information about the width and height.

#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
int main(int, char**)
{
    VideoCapture cap(0); // open the camera
    if(!cap.isOpened())  // check 
        return -1;
    Mat edges;
    namedWindow("edges",1);
    for(;;)
    {
        Mat frame;
        cap >> frame; 
        cvtColor(frame, edges, COLOR_BGR2GRAY);
        GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
        Canny(edges, edges, 0, 50, 3);
        vector<vector<Point> > contours;
        findContours(edges, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
        for( size_t i = 0; i < contours.size(); i++ )
        {
            Rect minRect = boundingRect(contours[i]);
            if(minRect.width > 50 & minRect.height > 50 )
            {
            rectangle(frame,minRect,Scalar(0,0,255));
putText(frame,format("width = %d , height = %d",minRect.width,minRect.height), Point(minRect.x,minRect.y),
                        FONT_HERSHEY_PLAIN, 1, Scalar(0,255,0));
                }
            }
            imshow("edges", frame);
            if(waitKey(30) >= 0) break;
        }
          return 0;
    }

With this code I can find required mass for simple figure, how can I as the width of the following figures:

http://www.pictureshack.ru/images/29524_3.jpg

http://www.pictureshack.ru/images/24004_12.jpg

thank you very much

how can I find necessary rectangle? or how can I find the width

using the code I can find contour, then draw a rectangle and write information about the width and height.

#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
int main(int, char**)
{
    VideoCapture cap(0); // open the camera
    if(!cap.isOpened())  // check 
        return -1;
    Mat edges;
    namedWindow("edges",1);
    for(;;)
    {
        Mat frame;
        cap >> frame; 
        cvtColor(frame, edges, COLOR_BGR2GRAY);
        GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
        Canny(edges, edges, 0, 50, 3);
        vector<vector<Point> > contours;
        findContours(edges, contours, RETR_LIST, CHAIN_APPROX_SIMPLE);
        for( size_t i = 0; i < contours.size(); i++ )
        {
            Rect minRect = boundingRect(contours[i]);
            if(minRect.width > 50 & minRect.height > 50 )
            {
            rectangle(frame,minRect,Scalar(0,0,255));
putText(frame,format("width = %d , height = %d",minRect.width,minRect.height), Point(minRect.x,minRect.y),
                        FONT_HERSHEY_PLAIN, 1, Scalar(0,255,0));
                }
            }
            imshow("frame", frame);
            imshow("edges", frame);
edges);
            if(waitKey(30) >= 0) break;
        }
          return 0;
    }

With this code I can find required mass for simple figure, how can I as the width of the following figures:

http://www.pictureshack.ru/images/29524_3.jpg

http://www.pictureshack.ru/images/24004_12.jpg

thank you very much