I have an image from which I need to detect the number. I have developed a CNN model for the same. But because my image is of a different color format (different from the mnist inputs which are white in black background), I need to process my image appropriately.
This is the input image. I need to convert it into white in black ground so I can pass it through my model to detect the number 3
.
I have tried to remove the border from the image and then invert the color to generate image of the required format.
My code so far :
import cv2
import matplotlib.pyplot as plt
img_name = 'input.png'
image = cv2.imread(img_name, cv2.IMREAD_GRAYSCALE)
gray = cv2.resize(image, (256,256))
result = gray[40:216, 40:216]
So the above code removes the border after resizing the image. This is the image obtained now when I use cv2.imwrite()
Then I tried to invert the colors as follows :
ans = cv2.bitwise_not(result)
ans1 = (ans//145)*255 # so the only values present are 0 and 255
This is the final image I have. As 3
is typed out very thick, my model tends to predict it as 8. Any help on how to process the image in a better way would be really helpful. Thanks a lot in advance.
Edit 1 :
I would like to remove the borders properly from images. In some cases the image is also being cropped away or the borders are still present when I try it using my code.
More images are present here
from Processing image as input for mnist model
No comments:
Post a Comment