Question

I got some problems with image with grey background with mahotas library:

Example:input image

This is the code:

import mahotas as mh
path ="./imagepath/a.jpg"
fork = mh.imread(path)  
bin = fork[:,:,0]
bfork = bin <230

After that i got that: output image

What have i do for getting a black background and a white sign? I tried in opencv module and was good, but i prefer mahotas.

import cv2
path ="./imagepath/a.jpg"
ow =  int ((oshape[0]/100 )*7 )
oh =  int ((oshape[0]/100 )*7 )
gray = cv2.imread(path,0)
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(ow,oh)) 
graydilate = cv2.erode(gray, element) 
ret,thresh = cv2.threshold(graydilate,127,255,cv2.THRESH_BINARY_INV) 
bin = thresh
Was it helpful?

Solution

You are not doing the same as in the opencv version. If you do, you'll get the wanted results:

fork = mh.imread(path)
bin = fork[:,:,0]
bin = mh.erode(bin)
bin = (bin < 127)

The erosion step was missing and the threshold was different.

from matplotlib import pyplot as plt
plt.imshow(bin)
plt.gray()

Binarized fork

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top