Ask Your Question

Revision history [back]

c++ to Python Need help to help :)

hi there,

i think this is a simple question but i need an answer to help someone.

i need a python code that equivalent to my c++ code below.

( code can be different except findContours( bw, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );)

thanks in advance.

input image :

image description

desired result image :

image description

desired output :

Contour 0 points [80, 160; 120, 160] - Contour 0 area : 0

Contour 1 points [161, 140; 161, 159; 160, 160; 140, 160; 161, 160] - Contour 1 area : 0.5

Contour 2 points [40, 140; 40, 160; 61, 160; 41, 160; 40, 159] - Contour 2 area : 0.5

Contour 3 points [161, 80; 161, 120] - Contour 3 area : 0

Contour 4 points [40, 80; 40, 120] - Contour 4 area : 0

Contour 5 points [50, 50; 50, 150; 150, 150; 150, 50] - Contour 5 area : 10000

Contour 6 points [140, 40; 160, 40; 161, 41; 161, 60; 161, 40] - Contour 6 area : 0.5

Contour 7 points [80, 40; 119, 40] - Contour 7 area : 0

Contour 8 points [40, 40; 40, 60; 40, 41; 41, 40; 60, 40] - Contour 8 area : 0.5

c++ code:

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

/// Global Variables
Point pt0;
Scalar color = Scalar(0,0,255);

int main( int argc, char** argv )
{
    char* filename = argc >= 2 ? argv[1] : (char*)"test.png";
    Mat img = imread(filename);
    if (img.empty())
        return -1;

    Mat bw;

    cvtColor( img, bw, COLOR_BGR2GRAY );
    bw = bw < 127;

    // Find contours
    vector<vector<Point> > contours;
    vector<Point> contour;
    findContours( bw, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );

    for ( size_t i = 0; i < contours.size(); i++)
    {
        drawContours(img,contours,i,color);
        cout << "Contour " << i << " points " << endl << contours[i];
        cout << " - Contour " << i << " area : " << contourArea(contours[i]) << endl << endl;
    }

    imshow("result", img);
    waitKey(0);
    return 0;
}

c++ to Python Need help to help :)

hi there,

i think this is a simple question but i need an answer to help someone.

i need a python code that equivalent to my c++ code below.

( code (code can be different except findContours( bw, contours, CV_RETR_EXTERNAL, findContours(bw,contours,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );)

thanks in advance.

input image :

image description

desired result image :

image description

desired output :

Contour 0 points [80, 160; 120, 160] - Contour 0 area : 0

Contour 1 points [161, 140; 161, 159; 160, 160; 140, 160; 161, 160] - Contour 1 area : 0.5

Contour 2 points [40, 140; 40, 160; 61, 160; 41, 160; 40, 159] - Contour 2 area : 0.5

Contour 3 points [161, 80; 161, 120] - Contour 3 area : 0

Contour 4 points [40, 80; 40, 120] - Contour 4 area : 0

Contour 5 points [50, 50; 50, 150; 150, 150; 150, 50] - Contour 5 area : 10000

Contour 6 points [140, 40; 160, 40; 161, 41; 161, 60; 161, 40] - Contour 6 area : 0.5

Contour 7 points [80, 40; 119, 40] - Contour 7 area : 0

Contour 8 points [40, 40; 40, 60; 40, 41; 41, 40; 60, 40] - Contour 8 area : 0.5

c++ code:

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

/// Global Variables
Point pt0;
Scalar color = Scalar(0,0,255);

int main( int argc, char** argv )
{
    char* filename = argc >= 2 ? argv[1] : (char*)"test.png";
    Mat img = imread(filename);
    if (img.empty())
        return -1;

    Mat bw;

    cvtColor( img, bw, COLOR_BGR2GRAY );
    bw = bw < 127;

    // Find contours
    vector<vector<Point> > contours;
    vector<Point> contour;
    findContours( bw, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );

    for ( size_t i = 0; i < contours.size(); i++)
    {
        drawContours(img,contours,i,color);
        cout << "Contour " << i << " points " << endl << contours[i];
        cout << " - Contour " << i << " area : " << contourArea(contours[i]) << endl << endl;
    }

    imshow("result", img);
    waitKey(0);
    return 0;
}

c++ to Python Need help to help :)

hi there,

i think this is a simple question but i need an answer to help someone.

i need a python code that equivalent to my c++ code below.

(code can ( important note : findContours parameters must be different except findContours(bw,contours,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE)

thanks in advance.

input image :

image description

desired result image :

image description

desired output :

Contour 0 points [80, 160; 120, 160] - Contour 0 area : 0

Contour 1 points [161, 140; 161, 159; 160, 160; 140, 160; 161, 160] - Contour 1 area : 0.5

Contour 2 points [40, 140; 40, 160; 61, 160; 41, 160; 40, 159] - Contour 2 area : 0.5

Contour 3 points [161, 80; 161, 120] - Contour 3 area : 0

Contour 4 points [40, 80; 40, 120] - Contour 4 area : 0

Contour 5 points [50, 50; 50, 150; 150, 150; 150, 50] - Contour 5 area : 10000

Contour 6 points [140, 40; 160, 40; 161, 41; 161, 60; 161, 40] - Contour 6 area : 0.5

Contour 7 points [80, 40; 119, 40] - Contour 7 area : 0

Contour 8 points [40, 40; 40, 60; 40, 41; 41, 40; 60, 40] - Contour 8 area : 0.5

c++ code:

#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

/// Global Variables
Point pt0;
Scalar color = Scalar(0,0,255);

int main( int argc, char** argv )
{
    char* filename = argc >= 2 ? argv[1] : (char*)"test.png";
    Mat img = imread(filename);
    if (img.empty())
        return -1;

    Mat bw;

    cvtColor( img, bw, COLOR_BGR2GRAY );
    bw = bw < 127;

    // Find contours
    vector<vector<Point> > contours;
    vector<Point> contour;
    findContours( bw, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );

    for ( size_t i = 0; i < contours.size(); i++)
    {
        drawContours(img,contours,i,color);
        cout << "Contour " << i << " points " << endl << contours[i];
        cout << " - Contour " << i << " area : " << contourArea(contours[i]) << endl << endl;
    }

    imshow("result", img);
    waitKey(0);
    return 0;
}