Ask Your Question
1

Converting Python to C++

asked 2015-06-18 12:26:04 -0600

jonlwowski012 gravatar image

I have the following opencv code in python and I would like to convert it to c++. I have already converted lines 1 and 2, but I got stuck on lines 3 and 4. Do any of you know how to convert those lines to c++.

contours, hierarchy = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt = contours[0]
leftmost = tuple(cnt[cnt[:,:,0].argmin()][0])
rightmost = tuple(cnt[cnt[:,:,0].argmax()][0])
edge_left_x = leftmost[0]-320
edge_left_y = leftmost[1]-180
edge_right_x = rightmost[0]-320
edge_right_y = rightmost[1]-180
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
5

answered 2015-06-20 04:50:55 -0600

updated 2015-06-20 04:52:48 -0600

inspired from http://opencvpython.blogspot.com.tr/2012/06/contours-3-extraction.html i tried to modify internal pointSetBoundingRect function to get topmost,bottommost,rightmost,leftmost points

example source image and result is

image descriptionimage description

this is my experimental code.

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"

using namespace cv;
using namespace std;


static Rect pointSetBoundingRect( const Mat& points , Mat m)
{
    int npoints = points.checkVector(2);


    int  xmin = 0, ymin = 0, xmax = -1, ymax = -1, i;
    Point ptxmin , ptymin , ptxmax , ptymax;

    if( npoints == 0 )
        return Rect();

    const Point* pts = points.ptr<Point>();
    Point pt = pts[0];

    ptxmin = ptymin = ptxmax = ptymax = pt;
    xmin = xmax = pt.x;
    ymin = ymax = pt.y;

    for( i = 1; i < npoints; i++ )
    {
        pt = pts[i];

        if( xmin > pt.x )
        {
            xmin = pt.x;
            ptxmin = pt;
        }


        if( xmax < pt.x )
        {
            xmax = pt.x;
            ptxmax = pt;
        }

        if( ymin > pt.y )
        {
            ymin = pt.y;
            ptymin = pt;
        }

        if( ymax < pt.y )
        {
            ymax = pt.y;
            ptymax = pt;
        }
    }
    ellipse( m, ptxmin, Size( 3, 3), 0, 0, 360, Scalar( 255, 0, 255 ), 2, 8, 0 );
    ellipse( m, ptxmax, Size( 3, 3), 0, 0, 360, Scalar( 255, 0, 255 ), 2, 8, 0 );
    ellipse( m, ptymin, Size( 3, 3), 0, 0, 360, Scalar( 255, 0, 255 ), 2, 8, 0 );
    ellipse( m, ptymax, Size( 3, 3), 0, 0, 360, Scalar( 255, 0, 255 ), 2, 8, 0 );
    return Rect(xmin, ymin, xmax - xmin + 1, ymax - ymin + 1);
}



int main( int argc, char** argv )
{
    Mat src;
    // the first command-line parameter must be a filename of the binary
    // (black-n-white) image
    if( argc != 2 || !(src=imread(argv[1], 0)).data)
        return -1;

    Mat dst = Mat::zeros(src.rows, src.cols, CV_8UC3);

    src = src > 127;
    namedWindow( "Source", 1 );
    imshow( "Source", src );

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    findContours( src, contours, hierarchy,
                  CV_RETR_CCOMP, CHAIN_APPROX_SIMPLE );


    Rect minRect;

    if (contours.size()>0)
    {

        for( size_t i = 0; i<contours.size() ; i++ )
        {
            Scalar color( rand()&255, rand()&255, rand()&255 );
            drawContours( dst, contours, i, color, FILLED, 8, hierarchy );

            minRect = pointSetBoundingRect( Mat(contours[i]),dst );
            rectangle( dst, minRect, Scalar(rand()&255,rand()&255,rand()&255), 1, 8 );
            imshow( "contours", dst );
            imwrite("extremepoints result.jpg",dst);
            waitKey(0);
        }

    }

    imshow( "contours", dst );
    waitKey(0);
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-06-18 12:26:04 -0600

Seen: 5,401 times

Last updated: Jun 20 '15