I tried to detect all the rectangles in image using threshold, canny edge and applied contour detection but it was not able to detect all the rectangles. Finally, I thought of detect the same using hough
transforms but when I tried to detect lines in image, I'm getting all the lines. I need to detect only rectangular boxes in the image. Can someone help me? I'm new to opencv.
Input image
Code:
import cv2
import matplotlib.pyplot as plt
import numpy as npimg = cv2.imread("demo-hand-written.png",-1)
#img = cv2.resize(img,(1280,720))
edges = cv2.Canny(img,180,200)
kernel = np.ones((2,2),np.uint8)
d = cv2.dilate(edges,kernel,iterations = 2)
e = cv2.erode(img,kernel,iterations = 2)
#ret, th = cv2.threshold(img, 220, 255, cv2.THRESH_BINARY_INV)lines = cv2.HoughLinesP(edges,1,np.pi/180,30, maxLineGap=20,minLineLength=30)
for line in lines:#print(line)x1,y1,x2,y2 = line[0]cv2.line(img,(x1,y1),(x2,y2),(0,255,0),3)
cv2.imshow("image",img)
cv2.waitKey(0)
cv2.destroyAllWindows()