Opencv : How to correctly apply morphologyEx operation ?

asked 2018-06-24 09:37:01 -0600

Ahmed Osama gravatar image

updated 2018-06-24 10:08:07 -0600

I am having a problem regarding the kernel size for morphologyEx. I have some captcha images and I want to do the same operation on them and get the same final result.

code :

 image = cv2.imread("Image.jpg")
 gray = cv2.cvtColor(image , cv2.COLOR_BGR2GRAY)


ret, thresh1 = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

k1 = np.ones((3,3))
k2 = np.ones((5,5))
bottom_image = cv2.morphologyEx(thresh1, cv2.MORPH_CLOSE, k1)
bottom_image =  255-bottom_image
bottom_image = remove_boxes(bottom_image , True)


ret,thresh2 = cv2.threshold(bottom_image,127,255,cv2.THRESH_BINARY_INV)
opening = cv2.morphologyEx(thresh2, cv2.MORPH_OPEN, k1)


#closing =  cv2.morphologyEx(opening, cv2.MORPH_CLOSE, k)
# cv2.imshow('opening', opening)

dilate = cv2.morphologyEx(opening, cv2.MORPH_DILATE, k2)
dilate = cv2.bitwise_not(dilate)
# cv2.imshow('dilation', dilate)


bottom_image = cv2.morphologyEx(bottom_image, cv2.MORPH_CLOSE, k1)

The perfect result would be

Input:
image description
Output :
image description

But the problem appears when I apply it to other images with the same structure output is distorted.

Example 1 :
Input:
image description
Output:
image description

Example 2 :

Input:
image description
> Output:
image description

Example 3 :

Input:
image description
Output:
image description

edit retag flag offensive close merge delete

Comments

can you put your images here, not on an external bin ?

(there is an "upload image" button in the edit menu)

berak gravatar imageberak ( 2018-06-24 09:42:53 -0600 )edit
1

I have uploaded the images here

Ahmed Osama gravatar imageAhmed Osama ( 2018-06-24 10:08:41 -0600 )edit

@Ahmed Osama That's not an issue. Close it please. I have got same results than yours because results are rights. Read and try tutorials

LBerger gravatar imageLBerger ( 2018-06-25 04:20:15 -0600 )edit

I can close it. but I would like to understand what is wrong with the results. I read the tutorial and something is missing. why the results is no the same

Ahmed Osama gravatar imageAhmed Osama ( 2018-06-25 04:25:05 -0600 )edit

"why the results is no the same" same? you ve'got three different input images and three ouputs = results are not the same because input are not the same. There is no imshow("output",) in your program. What is your output? try to write a good example

LBerger gravatar imageLBerger ( 2018-06-25 04:30:57 -0600 )edit
2

Closed the issue. the output I expect is to be as the first image. I am new to morphologex and I want to achieve my goal. sorry for any inconvenience. any advice you think might help is appreciated

Ahmed Osama gravatar imageAhmed Osama ( 2018-06-25 04:36:11 -0600 )edit

why the results is no the same

because you use a fixed size kernel, and the font type/size/strength changes between your images.

I want to do the same operation on them and get the same final result.

that might be an illusion.

berak gravatar imageberak ( 2018-06-25 04:41:38 -0600 )edit

so, how I can make a dynamic kernel with size related to font size and strength. is this possible and how

Ahmed Osama gravatar imageAhmed Osama ( 2018-06-25 04:51:36 -0600 )edit

i don't really think, you can automate this.

what you coould try though: make a diff image between before and after your morpology, if that has too many white pixels, your kernel was too large. (you have to experiment here, there's no silver bullet)

berak gravatar imageberak ( 2018-06-25 05:32:57 -0600 )edit