So I'm trying to find every square on a image, but I'm getting too many detections. I only want to detect the big cubes, and not all the noise around. My problem is also that the cubes can be different colours, but they will always be the same size.
I've written some code, but it doesn't work.
import numpy as np
import cv2
img = cv2.imread('gulRec.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imagem = (255-gray)
ret,thresh = cv2.threshold(imagem,120,200,1)
contours,h = cv2.findContours(thresh,1,2)
for cnt in contours:
approx = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
print (len(approx))
if len(approx)==4:
cv2.drawContours(img,[cnt],0,(0,0,255),-1)
cv2.imshow('thresh',thresh)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()






