Well, OpenCv comes with its function findCheckerboardCorners() in C++ which goes like

```
bool findChessboardCorners(InputArray image, Size patternSize, 
OutputArray corners, 
int flags=CALIB_CB_ADAPTIVE_THRESH+CALIB_CB_NORMALIZE_IMAGE )

```

After using this function for a while, one thing that i understood was that the pattern size must comply with the image to a very good extent, else the algorithm refuses to detect any Chessboard altogether. I was wondering if there were any random image of a chessboard, this function would fail as it is impractical to enter the precise values of the patternSize. Is there a way, the patternSize for this function could be obtained from the image provided. Any help would be appreciated. Thanks.
['https://Stackoverflow.com/questions/37541683', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4633925/']
Short answer: you cannot.

The OpenCV checkerboard detection code assumes that the pattern is uniform (all squares have the same size) and therefore, in order to uniquely locate its position in the image, the following two conditions must be true:

1. The pattern is entirely visible.
2. The pattern has a known numbers of rows and columns.

If either 1 or 2 is violated there is no way to know which corner is, say, the "top left" one.

For a more general case, and in particular if you anticipate that the pattern may be partially occluded, you must use a different algorithm and a non-uniform pattern, upon which corners can be uniquely identified.

There are various way to do that. My favorite pattern is Matsunaga and Kanatani's "2D barcode" one, which uses sequences of square lengths with unique crossratios. See the paper [here](https://www.semanticscholar.org/paper/Optimal-Grid-Pattern-for-Automated-Matching-Using-Matsunaga-Kanatani/8c57a5a20eaf9d8d0f16faa2153a80003ef50cdb). In order to match it, once you have sorted the corners into a grid, you can use a simple majority voting algorithm:

* Precompute the crossratios of all the pattern's consecutive 4-tuples of corners, in both the horizontal and vertical directions.
* Do the above for the detected corners in the grid.
* For every possible horizontal shift
	+ Over every row
		- Accumulate the number of crossratios that agree within a threshold
* Select the horizontal shift with the highest number of agreements.
* Repeat the above for every possible vertical shift, counting crossratios along the columns.
* Repeat the above two steps reversing the order of the crossratios in the vertical and horizontal and vertical direction, separately and jointly, to account for reflections and rotations.

Placing the detected corners in a grid can be achieved in various ways. There is an often-rediscovered [algorithm](https://somelightprojections.blogspot.com/2016/06/checkerboards.html) that uses topological proximity. The idea is to first associate each corner to all the squares within a small window of it, thus building a corner->squares table, and then traverse it as a graph to build a global table of the offsets of each corner from one another.
The doc for [`findChessboardCorners`](http://docs.opencv.org/3.0.0/d9/d0c/group__calib3d.html#ga93efa9b0aa890de240ca32b11253dd4a) says that

> 
> `patternSize` – Number of inner corners per a chessboard row and
>  column
> 
> 
> 

So `patternSize` is not the size of the chessboard inside the image but the number of inner corners. The number of inner corners does not depend from the size of the chessboard inside the image.

For example for the following image <https://github.com/Itseez/opencv/blob/3.1.0/samples/data/chessboard.png>
[![enter image description here](https://i.stack.imgur.com/4Tnr6.png)](https://i.stack.imgur.com/4Tnr6.png)
`patternSize` should be `cv::Size(7,7)`.