![]() Using Hough on these four edges would probably be a more robust way to go about it. The trick I'm using to find the corners is fairly simple, but fails here because one of the corners is not in the image. In the code below I label the image (identifies connected components), count the number of pixels per connected component, and select the one with the most pixels.įinally, subtracting from this result its erosion leaves us only with the pixels at the edge of the board (here in blue overlaid on the input image): Selecting the largest connected component is a fairly common procedure. The connected components that we see now correspond to the board and the neighboring boards (or whatever the other white things are around the board). Next, I binarize using Otsu threshold selection, and remove holes (that part is not important, the rest would work if there are holes too). Starting with a grey-value image, I apply a small closing (I used a circle with a diameter of 7 pixels, this is suitable for the down-sampled image I used as example, but you might want to increase the size appropriately for the full-size image). The simplest way to detect the board is to recognize that it is light-colored and has a dark-colored background. The four corners give all information about the perspective transformation. The grid itself can be guessed, since it's just an equidistant division of the board, there is no need to try to detect all lines. My aim here is to find the four corners of the board. I'm sure there are ways to accomplish all of these things in Python with OpenCV. I intend this answer to be about the concepts, not about the code. You're working in Python with OpenCV, but I'm going to give you an answer using MATLAB with DIPimage. Identifying corners using contours and harris corner detection:Īnd some examples of the images I have to work with. These are some images gotten from my attempt to obtain corners using contour detection and Harris corner detection. Print(testImageHeight, testImageWidth)''' TestImageWidth, testImageHeight = img.shape #display all the images produced from above processes #resize window because for some reason they are too large.Ĭv2.namedWindow('image', cv2.WINDOW_NORMAL) #iterates through an array of lines gottne from the hough transformĬv2.line(img, (x1,y1),(x2,y2),(0,0,255),2) #applies a hough transformation to extract gridlines from the image Ret, thresh = cv2.threshold(imgray, 127, 255, 0)Ĭv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)Ĭontour = cv2.drawContours(img, contours, -1, (0,255,0), 3)ĭef largest_4_sided_contour(thresh, show_contours=True):Ĭontours = sorted(contours, key = cv2.contourArea, reverse = True)įor cnt in contours: Imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) I also seem to get one error when I tried applying the Hough lines to my contoured image:Įrror: (-215) img.type() = (((0) & ((1 0.01*gridEdges.max()]= Also, yes, I went all out on the bilateral filtering, it seemed to help get rid of unnecessary contours and corners. This is my code (a bit sloppy with the naming and all but I'll try and fix that later). It will also help map grid square to pixel space which is very useful. The goal is to get the corners so I can perform a homography using a 10 X 10 grid to account for skewing in the images. I was wondering if anyone had a way for me to improve the results of my corner detection or if anyone had a completely different suggestion in mind that might help solve my problem. Even after applying all those measures, I still get tons of false corners and sometimes true corners aren't identified. ![]() To help aid this, I've used bilateral filtering, Canny edge detection, morphological dilation, and Harris edge detection as seen in a question with a similar problem as mine. It however, picks up on numerous contours and it leads to the same problem of identifying which are at the corners amongst all the contours identified. I then decided to use contours to detect the edges of the grid. Sometimes the Hough lines don't identify the edge of the grid and it is hard to determine which of the lines drawn belong to the edge of the grid and which of them are grid lines. To determine the corners of the grid, I have tried using Hough lines but to no avail. Images can be skewed and some might be relatively well oriented but we cannot guarantee that all images will be like this. I am trying to detect the corners of a grid within various pictures I have to process.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |