title: Detect rectangles in OpenCV (4.2.0) using Python (3.7),
body: <p>I am working on a personal project where I detect rectangles (all the same dimensions) and then place those rectangles inside a list in the same order (top-bottom) and then process the information inside each rectangle using some function. Below is my test image.</p>
  
  <p><a href="https://i.stack.imgur.com/lF9Jj.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lF9Jj.jpg" alt="Test Image"></a></p>
  
  <p>I have managed to detect the rectangle of interest, however I keep getting other rectangles that I don't want. As you can see I only want the three rectangles with the information (6,9,3) into a list.</p>
  
  <p><a href="https://i.stack.imgur.com/pdcd0.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pdcd0.jpg" alt="My output when I run my code"></a></p>
  
  <p>My code </p>
  
  <pre><code>import cv2
  
  width=700
  height=700
  y1=0
  y2=700
  x1=500
  x2=700
  img=cv2.imread('test.jpg') #read image
  img=cv2.resize(img,(width,height)) #resize image
  roi = img[y1:y2, x1:x2] #region of interest i.e where the rectangles will be
  gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY) #convert roi into gray
  Blur=cv2.GaussianBlur(gray,(5,5),1) #apply blur to roi
  Canny=cv2.Canny(Blur,10,50) #apply canny to roi
  
  #Find my contours
  contours =cv2.findContours(Canny,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)[0]
  
  #Loop through my contours to find rectangles and put them in a list, so i can view them individually later.
  cntrRect = []
  for i in contours:
          epsilon = 0.05*cv2.arcLength(i,True)
          approx = cv2.approxPolyDP(i,epsilon,True)
          if len(approx) == 4:
              cv2.drawContours(roi,cntrRect,-1,(0,255,0),2)
              cv2.imshow('Roi Rect ONLY',roi)
              cntrRect.append(approx)
  
  
  
  cv2.waitKey(0)
  cv2.destroyAllWindows()
  </code></pre>