27

I m trying to obtain a threshold of the calibration chessboard. I cant detect directly the chessboard corners as there is some dust as i observe a micro chessboard. I try several methods and HoughLinesP seems to be the easiest approach. But the results are not good, how to improve my results?

import numpy as np
import cv2

img = cv2.imread('lines.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
print img.shape[1]
print img.shape
minLineLength=100
lines = cv2.HoughLinesP(image=edges,rho=0.02,theta=np.pi/500, threshold=10,lines=np.array([]), minLineLength=minLineLength,maxLineGap=100)

a,b,c = lines.shape
for i in range(a):
    cv2.line(img, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 3, cv2.LINE_AA)
    cv2.imwrite('houghlines5.jpg',img)

As you can see on figure below, i cant obtain my chessboard, the lines are plotted in a lot of directions... (the original picture : https://s22.postimg.org/iq2b91xq9/droite_Image_00000.jpg)

enter image description here

user3601754
  • 3,792
  • 11
  • 43
  • 77
  • Use thresholding, or try to reduce the small points (erode, dilate). – linusg Sep 28 '16 at 15:56
  • I already try but not coupled with hough lines. Perhaps it could help to obtain the lines? thanks for your hellp ;) – user3601754 Sep 28 '16 at 15:58
  • I really would thresholding give another try! – linusg Sep 28 '16 at 15:59
  • Can you upload the original image? I also agree with giving thresholding a try combined with morphological analysis. I can give this a try myself if I had the original image. – rayryeng Sep 28 '16 at 15:59
  • Also, is it specifically required to use the Hough Transform? If you remove some of the noise, perform a rather large closing then find the corners (Harris for example), that should solve your problem. – rayryeng Sep 28 '16 at 16:01
  • Ok i do that i add it in the question ;) – user3601754 Sep 28 '16 at 16:02
  • I try to remove the noise with median filter but it is not sufficient or to labellize the square to isolate it. But the result is not very good i mean the reprojection error is sometimes important (RMS > 1) – user3601754 Sep 28 '16 at 16:06
  • if you are not required to implement your own solution you can try with an already implemented method http://docs.opencv.org/2.4/doc/tutorials/calib3d/camera_calibration_square_chess/camera_calibration_square_chess.html – GpG Sep 28 '16 at 16:18
  • Yep i know ;) but the picture is too noisy it doesnt work that is why i do this solution ;) – user3601754 Sep 28 '16 at 16:24

3 Answers3

56

You are using too small value for rho.

Try the below code:-

import numpy as np
import cv2

gray = cv2.imread('lines.jpg')
edges = cv2.Canny(gray,50,150,apertureSize = 3)
cv2.imwrite('edges-50-150.jpg',edges)
minLineLength=100
lines = cv2.HoughLinesP(image=edges,rho=1,theta=np.pi/180, threshold=100,lines=np.array([]), minLineLength=minLineLength,maxLineGap=80)

a,b,c = lines.shape
for i in range(a):
    cv2.line(gray, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 3, cv2.LINE_AA)
    cv2.imwrite('houghlines5.jpg',gray)

Note, the change in rho value, pi value and maxLineGap to reduce outliers.

Input Image Input Image

Edges Image Edges Image

Output Image Output Image

Miscellaneous - Tips for Beginners

  1. A lot of Computer Vision algorithms assume certain assumptions, well, in how the input should be. When building Proof-of-Concept, always try to view intermediate inputs you generate before applying such algorithms.

  2. For quick hack, if an algorithm accepts some parameters, use a for loop on possible values of these parameters and see how the results varies. Link to an answer on how to quickly generate these possible values.

  3. To really understand the algorithm, read on wiki or even better sources where if necessary. And then again/still do the above hack(point 2). It will further clear your understanding.

saurabheights
  • 3,967
  • 2
  • 31
  • 50
  • how to save the image without the red lines ? I would like to have the image without all the lines at all. Thanks – lucians Sep 18 '17 at 13:35
  • 2
    @Link - I am not sure what you meant by image without the lines. If you just need to output the original image, just use `cv2.imwrite('imagename.jpg',gray)`, before drawing hough lines, i.e. before running command `cv2.line` and use `imwrite` outside the loop. – saurabheights Sep 18 '17 at 17:05
  • 1
    How to get contours of each box?? Means I want to detect the boxes in image with there co-ordinates. – Parikshit Chalke May 22 '19 at 07:21
  • @ParikshitChalke There are many ways to do it, but simplest would be to use information from these edges and try fitting a chess edges(8x8 squares) over the input image. By fitting, I mean use some loss, such as SSD. Furthermore, please post a new question for different problems. – saurabheights May 22 '19 at 13:36
2

I would rather write this as a comment but unfortunately I can't. You should change the minLineLength and minLineGap. Or what if its just sqaures that you have to find, I would get all the lines and check the angles between them to get lines only along squares. I have worked with HoughLineP before and it is pretty much based on the above two arguments. Additionally, try using Bilateral filtering. I really helps when the sharpening using median filter doesn't help.

Bilateral Filter

Rick M.
  • 3,045
  • 1
  • 21
  • 39
1

in images processing they are some roles you have to go through such as filters before you go for edges detection, in your condition the dust is just a noise that you have to remove by filter, use gausse or blure after that use thresholding and then use canny for edges and in opencv they are cornere detection you can use, or you can just go for key point after threshholding if i'm not wrong.. try to do those steps and see the resulte

shadow
  • 767
  • 3
  • 8
  • 20