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:
Output :
But the problem appears when I apply it to other images with the same structure output is distorted.
Example 1 :
Input:
Output:
Example 2 :
Input:
>
Output:
Example 3 :
Input:
Output:
can you put your images here, not on an external bin ?
(there is an "upload image" button in the edit menu)
I have uploaded the images here
@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
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
"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
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
because you use a fixed size kernel, and the font type/size/strength changes between your images.
that might be an illusion.
so, how I can make a dynamic kernel with size related to font size and strength. is this possible and how
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)