Ask Your Question
0

How to find text area after dilating the image

asked 2019-04-06 22:03:40 -0600

Vayn gravatar image

updated 2019-04-07 06:16:40 -0600

This is the original image:

original

At first I applied the sobel operator to the image:

gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
sobel = cv.Sobel(gray, cv.CV_8U, 1, 0, 1)  
x = cv.Sobel(gray, cv.CV_16S, 1, 0)
y = cv.Sobel(gray, cv.CV_16S, 0, 1)
absX = cv.convertScaleAbs(x)
absY = cv.convertScaleAbs(y)
sobel = cv.addWeighted(absX, 0.2, absY, 0.8, 0)

sobel

Then I dilated the image:

kernel = cv.getStructuringElement(cv.MORPH_RECT, (24, 14))
dilate = cv.dilate(sobel, kernel, iterations=1)

image description

findContours can't find the rect of text, it detected the irrelevant rect on the left of the image:

image description

edit retag flag offensive close merge delete

Comments

please upload the original image and the whole code you tried

sturkmen gravatar imagesturkmen ( 2019-04-07 00:48:30 -0600 )edit

@sturkmen The third image is almost the original one. I updated the code.

Vayn gravatar imageVayn ( 2019-04-07 06:11:50 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-04-07 09:12:17 -0600

updated 2019-04-07 09:19:21 -0600

see my trial code that inspired by this sample and this post

#!/usr/bin/env python

# Python 2/3 compatibility
from __future__ import print_function

import numpy as np
import cv2 as cv

# built-in modules
import os
import sys

def main():
    try:
        src = sys.argv[1]
    except:
        src = "C:/build/opencv4.0.1/bin/Release/15546357708304171.jpg"

    frame = cv.imread(src)
    gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)

    bin = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY_INV, 3, 9)
    bin = cv.medianBlur(bin, 3)
    bin = cv.medianBlur(bin, 3)
    dilate = cv.dilate(bin, None, iterations=2)
    bin = cv.GaussianBlur(bin, (45,45),0)
    ret, bin = cv.threshold(bin, 0,255, cv.THRESH_BINARY)       
    contours, heirs = cv.findContours( bin, cv.RETR_CCOMP, cv.CHAIN_APPROX_SIMPLE)

    largest_contour=0
    for idx, contour in enumerate(contours):

        area=cv.contourArea(contour)
        if (area >largest_contour) :
            largest_contour=area           
            largest_index=idx

    r = cv.boundingRect(contours[largest_index])
    cv.rectangle(frame,r,(0,0,255),3)
    cv.imshow('frame', frame)
    cv.imshow('dilate', dilate)

    cv.imshow('bin', bin)
    cv.waitKey()


if __name__ == '__main__':
    main()
    cv.destroyAllWindows()
edit flag offensive delete link more

Comments

1

Thank you so much! After spending hours researching, I found I can use canny operator to detect the edges of the text, then dilate the image and extract the right rectangle with contour. However canny edge detection is a CPU intensive algorithm, I'll compare two ways.

Vayn gravatar imageVayn ( 2019-04-07 09:58:29 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-04-06 22:03:40 -0600

Seen: 1,239 times

Last updated: Apr 07 '19