HoughLinesP vastly different in Python and C++
Hi all,
I’ve spent some time to debug a pole detector i wanted to build on basis of the probabilistic line transform. The results i got from my python code, using Opencv 2.4.x as well as 3.0.0-beta, always looked just so bad. So i made what i hope to be a kind of self-contained example…
Python
#!/usr/bin/python
import cv2
import numpy as np
import sys
src = cv2.imread(sys.argv[1], 0)
dst = cv2.Canny(src, 50, 200, 3)
color_dst = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR)
lines = cv2.HoughLinesP(dst, 1, np.pi/180, 80, 30, 10)
for x1,y1,x2,y2 in lines[0]:
cv2.line(color_dst, (x1,y1), (x2,y2), (0,255,0), 3, 8)
cv2.imwrite("test_py.jpg", color_dst)
C++
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc/types_c.h>
#include <iostream>
using namespace cv;
int main(int argc, char** argv)
{
Mat src, dst, color_dst;
if( argc != 2 || !(src=imread(argv[1], 0)).data)
return -1;
Canny( src, dst, 50, 200, 3 );
cvtColor( dst, color_dst, CV_GRAY2BGR );
std::vector<Vec4i> lines;
HoughLinesP( dst, lines, 1, CV_PI/180, 80, 30, 10 );
for( size_t i = 0; i < lines.size(); i++ )
{
line( color_dst, Point(lines[i][0], lines[i][1]),
Point(lines[i][2], lines[i][3]), Scalar(0,0,255), 3, 8 );
}
imwrite( "test_cpp.jpg", color_dst );
return 0;
}
Results
I’ve taken the example image from the OpenCV docs for HoughLinesP, as this looked to me like a good tasting ground…
Input Image
C++ Result 2.4.9+3.0.0-beta (expected)
Python 2.7.9 OpenCV 2.4.9 (broken?)
Python 2.7.9 OpenCV 3.0.0-beta (totally broken?)
As you can see, in Python with OpenCV 2.4.9 (3.0.0-beta looks worse!), the resulting lines are more, shorter, generally more clutter-y as compared with the C++ version. I’ve also checked the Canny output (visually), and it looks like it’s the same. Did i make any mistake? If not, how can this happen? I thought that python bindings are generated using an automated wrapper generator (swig?), so the code run in the background should be the same, right?
Any hints or tips on this would be greatly appreciated!