Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Opencv : How to correctly apply morphologyEx operation ?

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: https://ibb.co/jZC8Ko
Output : https://ibb.co/kPWeQT

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

Example 1 :
Input: https://ibb.co/ixCHeo
Output: https://ibb.co/dnwXC8

Example 2 :

Input: https://ibb.co/mBuVzo
Output: https://ibb.co/ce9xeo

Example 3 :

Input: https://ibb.co/gLhps8
Output: https://ibb.co/ekbFX8

Opencv : How to correctly apply morphologyEx operation ?

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: https://ibb.co/jZC8Ko Input Image
Output : https://ibb.co/kPWeQTOutput

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

Example 1 :
Input: https://ibb.co/ixCHeo Input
Output: https://ibb.co/dnwXC8output

Example 2 :

Input: https://ibb.co/mBuVzo Input
Output: https://ibb.co/ce9xeooutput

Example 3 :

Input: https://ibb.co/gLhps8 Input
Output: https://ibb.co/ekbFX8Output

Opencv : How to correctly apply morphologyEx operation ?

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: Input Image Input:
image description

Output : Output
image description

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

Example 1 :
Input: Input
image description

Output: output
image description

Example 2 :

Input: Input

image description
>
Output: output
image description

Example 3 :

Input: Input
Output: Output

image description
Output:
image description