Horatius420
Horatius420

Reputation: 1

Detecting the biggest rectangle with thicker lines on grid paper with OpenCV

We are making a game where you can use grid paper to make your own level.

The first step was detecting the boundaries of the level, so I made a thicker black border around the level for the edges. This is the image: https://i.sstatic.net/HK3kL.jpg

I first edited the image to gray, applied GaussianBlur and adaptiveThreshold. This is the result: https://i.sstatic.net/3bQ2R.jpg

Normal Houghlines isn't really an option I think as it detects so many lines and loses the thicker lines. findContour I haven't got a usable outcomes as there are so many shapes in a gridpaper.

So I ended up with HoughLinesP which resulted in this:https://i.sstatic.net/JfkVW.jpg

So it detects one part of the rectangle correctly and that is it. I would have another idea which would rely on colors which isn't ideal (people would need a specific color) but I'm at a loss on what a good approach for this would be.

Here is my code I currently have for HoughLinesP

import cv2 as cv
import numpy as np

# from openCV.main import contours

image = cv.imread("grey-grid-blackborder.jpg")
cv.imshow("Image", image)

gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv.imshow("Gray", gray)

blur = cv.GaussianBlur(gray, (5, 5), 0)
cv.imshow("blur", blur)

thresh = cv.adaptiveThreshold(blur, 255, 1, 1, 11, 2)
cv.imshow("thresh", thresh)

grid = image

minLineLength = 1
maxLineGap = 0
# lines = cv.HoughLinesP(thresh, 1, np.pi / 180, 100, minLineLength, maxLineGap)
lines = cv.HoughLinesP(thresh, 1, np.pi / 180, 100, minLineLength=minLineLength, maxLineGap=maxLineGap)
for x1, y1, x2, y2 in lines[0]:
    cv.line(grid, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv.imshow("grid", grid)

cv.waitKey(0)

Upvotes: 0

Views: 216

Answers (0)

Related Questions