Search code examples
pngscipymorphing

Python scipy dilation creates different colors


My image (png) is a black/white letter (The letter is white, the background is black). When I apply binary_dilation, I get an image which is white inside and the background is blue.

from scipy.ndimage.morphology import binary_dilation
from scipy.misc import imread, imsave

template = imread("temp.png")/255.0
imsave("Result.png",binary_dilation(template))

Why?


Solution

  • Beware of the color channels --- if "temp.png" has it, then template.shape == (nx, ny, 3) or with alpha template.shape == (nx, ny, 4). Binary dilation treats the last dimension as the third spatial dimension, rather than as a color channel, which is not what you usually want. You can do binary_dilation(template[:,:,0]) to enforce a 2-D image operation.