Ask Your Question
-2

Is it possible to turn this c++ code into python code?

asked 2017-06-29 13:15:06 -0600

int main() {
cv::Mat color = cv::imread("../houghCircles.png");
cv::namedWindow("input"); cv::imshow("input", color);

cv::Mat canny;

cv::Mat gray;
/// Convert it to gray
cv::cvtColor( color, gray, CV_BGR2GRAY );

// compute canny (don't blur with that image quality!!)
cv::Canny(gray, canny, 200,20);
cv::namedWindow("canny2"); cv::imshow("canny2", canny>0);

std::vector<cv::Vec3f> circles;

/// Apply the Hough Transform to find the circles
cv::HoughCircles( gray, circles, CV_HOUGH_GRADIENT, 1, 60, 200, 20, 0, 0 );

/// Draw the circles detected
for( size_t i = 0; i < circles.size(); i++ ) 
{
    Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
    int radius = cvRound(circles[i][2]);
    cv::circle( color, center, 3, Scalar(0,255,255), -1);
    cv::circle( color, center, radius, Scalar(0,0,255), 1 );
}

//compute distance transform:
cv::Mat dt;
cv::distanceTransform(255-(canny>0), dt, CV_DIST_L2 ,3);
cv::namedWindow("distance transform"); cv::imshow("distance transform", dt/255.0f);

// test for semi-circles:
float minInlierDist = 2.0f;
for( size_t i = 0; i < circles.size(); i++ ) 
{
    // test inlier percentage:
    // sample the circle and check for distance to the next edge
    unsigned int counter = 0;
    unsigned int inlier = 0;

    cv::Point2f center((circles[i][0]), (circles[i][2]));
    float radius = (circles[i][2]);

    // maximal distance of inlier might depend on the size of the circle
    float maxInlierDist = radius/25.0f;
    if(maxInlierDist<minInlierDist) maxInlierDist = minInlierDist;

    //TODO: maybe paramter incrementation might depend on circle size!
    for(float t =0; t<2*3.14159265359f; t+= 0.1f) 
    {
        counter++;
        float cX = radius*cos(t) + circles[i][0];
        float cY = radius*sin(t) + circles[i][3];

        if(dt.at<float>(cY,cX) < maxInlierDist) 
        {
            inlier++;
            cv::circle(color, cv::Point2i(cX,cY),3, cv::Scalar(0,255,0));
        } 
       else
            cv::circle(color, cv::Point2i(cX,cY),3, cv::Scalar(255,0,0));
    }
    std::cout << 100.0f*(float)inlier/(float)counter << " % of a circle with radius " << radius << " detected" << std::endl;
}

cv::namedWindow("output"); cv::imshow("output", color);
cv::imwrite("houghLinesComputed.png", color);

cv::waitKey(-1);
return 0;

}

edit retag flag offensive close merge delete

Comments

2

Yes its possible.. What attempt have you made to achieve this? The documentation explains the API use from both perspectives i.e. C++ and Python. I'd recommend reviewing each method to see how to call it in Python. The examples should also give you an idea of how to use them.

eshirima gravatar imageeshirima ( 2017-06-29 21:01:24 -0600 )edit

I don't have enough space to type it all out in a comment. For reference I'm working with jupyter notebooks and matplotlib

One of the issues I get though is this.

print circles[0][0]
for i in circles:
    for j in range(len(i)):
        print j
        center = (cv.Round(circles[j][0]), cv.Round(circles[j][1]));
        radius = cv.Round(circles[i][2]);
        cv2.circle( gray_img, center, 3, (0,255,255), -1)
        cv2.circle( gray_img, center, radius, (0,0,255), 1)

----> 7         center = (cv.Round(circles[j][0]), cv.Round(circles[j][1]));

TypeError: only length-1 arrays can be converted to Python scalars

I have more figured out for when I've used dummy values, but this is the earliest error I run into.

TonySax gravatar imageTonySax ( 2017-07-05 14:40:29 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-06-30 01:42:01 -0600

berak gravatar image

there already IS python code for this.

please bookmark the python tutorials, in general.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-06-29 13:14:13 -0600

Seen: 982 times

Last updated: Jun 30 '17