I have a grayscale image (called input) and I'm trying to add 1px padding (made up of 0 only) around the image in order to apply a filter:
padded = np.zeros((input.shape[0]+2, input.shape[1]+2), dtype=int)
padded[1:-1, 1:-1] = input[:,:]
but the result is less contrasted than the original. Why? This is the original image:
and this is the bordered image:
If instead of using the zeros function I use the once function and multiply the matrix by 255, I get an even different image. I don't understand why as I just copy one image into another.
From the plt.imshow
docs:
X : array-like or PIL image
The image data. Supported array shapes are:
- (M, N): an image with scalar data. The values are mapped to colors using normalization and a colormap. . . .
Normalization means that the colors are "spread out" so that the minimum is black and max is white. So those np.zeros
are making the minimum 0
(and the border black) and making everything else lighter to compensate. This should work to fix it (I think)
padded = np.pad(input,
pad_width = 1,
mode = 'minimum') # or mode = "maximum' for a white border
If you absolutely need a border of 0
, you can do:
padded = np.pad(input - input.min(),
pad_width = 1,
mode = 'constant')
That changes the range of your original picture to start a 0
, so the normalization should be the same.