Ask Your Question
1

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

asked 2015-12-08 01:44:37 -0600

MValeriy gravatar image

updated 2015-12-08 03:46:18 -0600

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", 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/295...

http://www.pictureshack.ru/images/240...

thank you very much

edit retag flag offensive close merge delete

Comments

I am not even sure if the provided code has anything to do to the images you are supplying... Is this your own code?

StevenPuttemans gravatar imageStevenPuttemans ( 2015-12-08 03:16:11 -0600 )edit

no , Code prompted me to this forum, I'm just trying to use it to solve my problem

MValeriy gravatar imageMValeriy ( 2015-12-08 03:47:51 -0600 )edit
1

You need to stop opening topics about the same problem, open a single one, explain it better and add your image. Then wait for people to help you out.

StevenPuttemans gravatar imageStevenPuttemans ( 2015-12-08 04:19:43 -0600 )edit

yes of course, there is still a lot of questions, but unfortunately I can not find a solution, but time is running out - study comes to an end. I published the existing questions in one, but because it did not receive more than one response considered that the question is not clear, so I wanted to write a few simple little question. Sorry

MValeriy gravatar imageMValeriy ( 2015-12-08 04:42:45 -0600 )edit

delete or close the question?

MValeriy gravatar imageMValeriy ( 2015-12-08 04:43:40 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2015-12-08 19:46:52 -0600

updated 2015-12-14 05:04:25 -0600

it is not a complete answer but just a clue . with cv::approxPolyDP you can find some points to analyze

example:

#include "highgui.hpp"
#include "imgproc.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    Mat src_gray = imread(argv[1],0);

    Mat bw_img = src_gray > 127;
    cvtColor( src_gray, src_gray, COLOR_GRAY2BGR);

    vector<vector<Point> > contours;

    findContours( bw_img, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE );

    Point minX( src_gray.cols, 0 );
    Point maxX( 0, 0 );
    for( size_t i=0; i<contours.size(); i++)
    {
        approxPolyDP(Mat(contours[i]), contours[i], 4, true);

        for( size_t j=0; j<contours[i].size(); j++)
        {
            if( contours[i][j].x > 3 & contours[i][j].x < minX.x - 3 )
            {
               minX = contours[i][j];
            }

            if( contours[i][j].x < src_gray.cols - 3 & contours[i][j].x > maxX.x  )
            {
               maxX = contours[i][j];
            }

            cout << "Point(x,y)=" << contours[i][j].x << "," << contours[i][j].y << endl;
            circle( src_gray, contours[i][j], 3, Scalar(0, 0, 255), FILLED, LINE_AA );
        }
    }

    circle( src_gray, minX, 15, Scalar(0, 255, 0), 1, LINE_AA );
    circle( src_gray, maxX, 15, Scalar(0, 255, 0), 1, LINE_AA );

    imshow( "Result", src_gray );
    waitKey(0);
    return(0);
}

output images :

image description image description

edit flag offensive delete link more

Comments

Thank you so much

MValeriy gravatar imageMValeriy ( 2015-12-09 07:45:08 -0600 )edit

Unfortunately I have a question again! Can I like something out of this set of points to find the coordinates of the point with the smallest X? see Figure: http://www.pictureshack.ru/images/322...

MValeriy gravatar imageMValeriy ( 2015-12-14 04:20:30 -0600 )edit

In your code, I have limited J, and thus exclude points at the beginning and the end.
for example: if (contours[i][j].x>1 & contours[i][j].x<948)

MValeriy gravatar imageMValeriy ( 2015-12-14 04:22:05 -0600 )edit

can I use minMaxLoc() to do it? http://docs.opencv.org/modules/core/d...

MValeriy gravatar imageMValeriy ( 2015-12-14 04:35:43 -0600 )edit

take a look at updated code

sturkmen gravatar imagesturkmen ( 2015-12-14 05:05:52 -0600 )edit

thank you very much, your help is invaluable for me

MValeriy gravatar imageMValeriy ( 2015-12-14 05:27:44 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-12-08 01:44:37 -0600

Seen: 599 times

Last updated: Dec 14 '15