-3

Here is my code

#include <opencv/cv.h> 
#include <opencv/highgui.h>
#include<opencv2/opencv.hpp>

#include<iostream>
//#include<vector>

using namespace cv;
using namespace std;

int main()
{
    VideoCapture cap = VideoCapture(0);
    int successes = 0;

    int numBoards = 0;
    int numCornersHor = 6;
    int numCornersVer = 4;

    int numSquares = (numCornersHor - 1) * (numCornersVer - 1);
    Size board_sz = Size(numCornersHor, numCornersVer);



    vector<Point2f> corners;


    for (;;)
    {
        Mat img;
        cap >> img;
        Mat gray;
        cvtColor(img, gray, CV_RGB2GRAY);
        if (img.empty()) break; // end of video stream
        imshow("this is you, smile! :)", gray);
        if (waitKey(1) == 27) break; // stop capturing by pressing ESC 


    bool found = findChessboardCorners(gray, board_sz, corners, CALIB_CB_ADAPTIVE_THRESH);
    if (found == 1)
    {
        cout << corners.size()<<"\n";

        cornerSubPix(gray, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1));
        drawChessboardCorners(gray, board_sz, corners, found);
    }

    }

    cap.release();
        waitKey();
        return 0;
    }

The code is capturing frames from a webcam. If a chessboard is detected, the total number of found corners is printed out (I did it because I was not getting the same output as in the tutorial code and I wanted to find where the bug is).

The output:

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
dhc
  • 3
  • 7
  • OUTPUT SAYS THAT 11 TIME CORNERS ARE DETECTED BUT I dont know whay this much big value is coming – dhc Sep 29 '16 at 12:05
  • 1
    what's with the capslock? – slawekwin Sep 29 '16 at 12:18
  • Sorry about that! @slawekwin – dhc Sep 29 '16 at 12:20
  • 1
    http://docs.opencv.org/3.1.0/d9/d0c/group__calib3d.html#ga93efa9b0aa890de240ca32b11253dd4a *"Note The function requires white space (like a square-thick border, the wider the better) around the board to make the detection more robust in various environments. Otherwise, if there is no border and the background is dark, the outer black squares cannot be segmented properly and so the square grouping and ordering algorithm fails."* ... *"For example, a regular chessboard has 8 x 8 squares and 7 x 7 internal corners"* (so 11 isn't big, it's too small, for whole chessboard you should have 49) – Ped7g Sep 29 '16 at 14:51
  • @Ped7g I meant that while capturing continuous frames from webcam I could detect the corners 11 times. No of corners in with size (6,4)(which i wrote in code) I should get 24 corners but I am getting "1844..."(garbage value) – dhc Sep 30 '16 at 03:59
  • sounds weird, try to feed it by static image of perfect chessboard with white borders to debug the rest of code, then try again with live images... If it works well with perfect imagine, and live images produce 1k+ corners, then you have to toy around with picture quality. Of course while the picture is moving/blur/etc, you may get temporarily weird results (although I have no particular experience with opencv, I'm just guessing this from my other experience with CV and OCR technology), but once the live image is good enough, the results should mostly follow the perfect image input. – Ped7g Sep 30 '16 at 09:39
  • can you uplaod a image with your chessboard so wi can test whats wrong? – PSchn Oct 01 '16 at 09:15
  • After having some suggestion, I changed the code a bit and also changed the way I was capturing, but still, no change in the output! :( – dhc Oct 03 '16 at 03:56

2 Answers2

1

First you should follow some ground rules:

  1. Do not use loose papers -> print/glue the chessboard on a flat plate
  2. Print it with a big white border to improve detection
  3. The chessboard has to be completly inside the image (not as in your example)
  4. Take several images with different positions of your chessboard

Second, you cant draw your contours into a 8-bit grayscale image, use an 8-bit color image instead. And if i count correctly (count inner corners) your chessboard has the size (8,6).

PSchn
  • 718
  • 4
  • 14
1

I have the same problem, the number of corners is HUGE. After some search i found this solution Here. For some reason findChessboardCorners function resizes the corners vector. I tried the solution above, it worked well with the output corners, but i still have assertion failed problem with cornerSubPix function.

Community
  • 1
  • 1
Fadwa
  • 1,717
  • 5
  • 26
  • 43
  • 2
    Thanks but I solved the problem by inserting correct number of squares(very silly mistake though) and after that cornerSubPix function is also working perfectly ok for me! Thanks again! :) – dhc Dec 14 '16 at 04:52