-4

I have the picture below. I want to find the the black colored rectangles on the left using opencv. Thanks for help=) enter image description here

1
  • 1
    Please provide more detail. As it currently reads, your question doesn't make much sense. Commented Apr 28, 2021 at 14:29

1 Answer 1

7

Here's a simple approach using thresholding + morphological operations.

  1. Obtain binary image. Load image, convert to grayscale, then adaptive threshold

  2. Fill rectangular contours. Find contours and fill the contours to create filled rectangular blocks.

  3. Perform morph open. We create a rectangular structuring element and morph open to remove the lines

  4. Draw rectangles around largest rectangles Find contours and draw bounding rectangles around rectangles with an area above a certain treshold.


Here's each step visualized:

Obtain binary image

enter image description here

Adaptive treshold

enter image description here

Filled rectangular contours

enter image description here

Perform morph open

enter image description here

Draw rectangles around largest rectangles

enter image description here

In code:

import numpy as np 
import cv2

#load the image
image = cv2.imread("mtF6y.jpg")

# grayscale
result = image.copy()
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)

# adaptive threshold
thresh = cv2.adaptiveThreshold(gray,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,51,9)

# Fill rectangular contours
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(thresh, [c], -1, (255,255,255), -1)

# Morph open
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9,9))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=4)

# Draw rectangles, the 'area_treshold' value was determined empirically
cnts = cv2.findContours(opening, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
area_treshold = 4000
for c in cnts:
    if cv2.contourArea(c) > area_treshold :
      x,y,w,h = cv2.boundingRect(c)
      cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 3)

cv2.imshow('thresh', thresh)
cv2.imshow('opening', opening)
cv2.imshow('image', image)
cv2.waitKey()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.