hi there,
i think this is a simple question but i need an answer to help [someone.](http://answers.opencv.org/question/70629/detect-spaces-and-fill-with-rectangle/)
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 :**

**desired result image :**

**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
#include
#include
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> contours;
vector 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;
}
↧