4

I am following up from this post: How to extract only characters from image?

This solution works perfectly for me (with some tweaking) for its desired purpose. However, I am attempting to take it 1 step further by saving each of the characters. So in the example of this post, I wish to save the characters K, N, and M as their own individual images. I attempted iterating through the nested if loop with a cv2.imwrite function with the rect object although the final output is 7 images containing the overall image with just an additional rectangle to highlight the next contour each time.

example image:

nathancy
  • 42,661
  • 14
  • 115
  • 137
Jack Timber
  • 45
  • 1
  • 1
  • 5

1 Answers1

2

Here's a simple approach:

  1. Obtain binary image. Load image, grayscale, Otsu's threshold

  2. Extract ROIs. Find contours and sort from left-to-right to ensure we have the contours in the correct order with imutils.contours.sort_contours. We filter using contour area then extract and save each ROI using Numpy slicing.


Input

enter image description here

Binary image

enter image description here

Detected characters highlighted in green

enter image description here

Extracted ROIs

enter image description here

Code

import cv2
from imutils import contours

# Load image, grayscale, Otsu's threshold
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY_INV)[1]

# Find contours, sort from left-to-right, then crop
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts, _ = contours.sort_contours(cnts, method="left-to-right")

# Filter using contour area and extract ROI
ROI_number = 0
for c in cnts:
    area = cv2.contourArea(c)
    if area > 10:
        x,y,w,h = cv2.boundingRect(c)
        ROI = image[y:y+h, x:x+w]
        cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)
        cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 2)
        ROI_number += 1

cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey()
nathancy
  • 42,661
  • 14
  • 115
  • 137
  • Hi nathancy, I know my this comment will be removed by the moderators. But before it gets can you please suggest me some resources to learn opencv, I see your answer nowadays in most of OCV question. And I am a beginner can you please help me out with this. – Himanshu Poddar Mar 04 '20 at 09:25