Engine
Engine

Reputation: 5422

how to examin white quads in opencv

I have a small chessboard and I put a geometric objects in the white squares, now I'm trying to extract them from the image using cv::findContours(), here my code and the source and result images:

int main (){
    cv::Mat img = cv::imread("quad.jpg",0);
    cv::Mat image ;
    cv::Mat result(img.size(),CV_8U,cv::Scalar(255));
    std::vector<std::vector<cv::Point>> contours;
    //std::vector<std::vector<cv::Point2f>> hiararchy;
    cv::threshold(img,img,127,255,CV_THRESH_BINARY_INV);
    cv::findContours(img,contours,/*hiararchy,*/CV_RETR_TREE    , CV_CHAIN_APPROX_NONE );
-1, // draw all contours
cv::Scalar(0), // in black
2); // with a thickness of 2
    cv::imshow("result",result);
    //cv::imwrite("con.jpg",result);
    cv::waitKey(0);

    return 0;

source

source

result

result

any idea how to tell the program : care only about the white squares

thanks!

Upvotes: 0

Views: 504

Answers (1)

Zaphod
Zaphod

Reputation: 1927

Use the hough transform to find the longest lines in the image. This will give you the grid of the checkerboard. Next you can check the average pixel colour within each "cell" on the grid to determine whether it's a black or a white square.

Upvotes: 1

Related Questions