Ask Your Question
2

c++ to Python Need help to help :)

asked 2015-09-17 13:09:46 -0600

updated 2015-09-17 13:15:27 -0600

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.

( important note : findContours parameters must be 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;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
6

answered 2015-09-18 02:54:17 -0600

berak gravatar image

updated 2015-09-18 02:57:04 -0600

import cv2
import numpy as np


img = cv2.imread("im/kasten.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = np.asarray(gray < 127, dtype=np.uint8) # need to cast back to uint8

## careful, api change !
## 3.0
dead_img, contours, hierarchy = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
## 2.4
#contours, hierarchy = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

cv2.drawContours(img, contours,-1, (0,0,255), 2)

for i in range(len(contours)):
    print "Contour ", i , "points", contours[i]
    print " - Contour ", i, "area :", cv2.contourArea(contours[i])
    print

cv2.imshow('contours', img)
cv2.waitKey()

image description

edit flag offensive delete link more

Comments

2

thank you so much.

sturkmen gravatar imagesturkmen ( 2015-09-18 03:11:10 -0600 )edit

Question Tools

Stats

Asked: 2015-09-17 13:09:46 -0600

Seen: 1,162 times

Last updated: Sep 18 '15