Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contour object detection and extraction in OpenCV Python

I am trying to detect three rectangles from the contour. I have already extracted the entire contour from the whole image. I am attaching the image of extracted contour below. I would like to figure out a way to further extract three rectangular kind panels from the whole contour.

Extracted Contour Image:

enter image description here

like image 794
Nisarg Dave Avatar asked Sep 03 '19 18:09

Nisarg Dave


People also ask

What is contour detection in OpenCV?

Contour Detection using OpenCV (Python/C++) Using contour detection, we can detect the borders of objects, and localize them easily in an image. It is often the first step for many interesting applications, such as image-foreground extraction, simple-image segmentation, detection and recognition.


1 Answers

Here's a simple approach:

  • Convert image to grayscale
  • Threshold to obtain binary image
  • Perform morphological operations to smooth image
  • Find contours and extract ROI

After converting to grayscale, we threshold to obtain a binary image

image = cv2.imread('1.png')
original = image.copy()

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 120, 255, cv2.THRESH_BINARY)[1]

Next we create a kernel and perform morphological operations to smooth the image. This step "breaks" the joints connecting the three rectangles by eroding the image

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25,25))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=3)

From here we find contours and extract the ROI with numpy slicing. The bounding boxes for the desired rectangles are drawn on the original image

cnts = cv2.findContours(opening, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

image_number = 0
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 3)
    ROI = original[y:y+h, x:x+w]
    cv2.imwrite("ROI_{}.png".format(image_number), ROI)
    image_number += 1

Here's each individual saved ROI

Full code

import cv2

image = cv2.imread('1.png')
original = image.copy()

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 120, 255, cv2.THRESH_BINARY)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25,25))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=3)

cnts = cv2.findContours(opening, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]

image_number = 0
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 3)
    ROI = original[y:y+h, x:x+w]
    cv2.imwrite("ROI_{}.png".format(image_number), ROI)
    image_number += 1

cv2.imshow('opening', opening)
cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey()
like image 54
nathancy Avatar answered Sep 20 '22 12:09

nathancy