7

I'm using scipy's convolve2d:

for i in range(0, 12):
            R.append(scipy.signal.convolve2d(self.img,  h[i], mode = 'same'))

After convolution all values are in magnitudes of 10000s, but considering I'm working with images, I need them to be in the range of 0-255. How do I normalize it?

  • This may give you sime hints: http://stackoverflow.com/questions/14765891/image-smoothing-in-python Find the maximum and scale it. Please proved image and kernel data for more info. – tfv Apr 21 '16 at 05:16
  • It's a 16,16 kernel, with all values between 0-255. –  Apr 21 '16 at 05:19

1 Answers1

2

Assuming that you want to normalize within one single image, you can simply use im_out = im_out / im_out.max() * 255 .

You could also normalize kernel or original image.

Example below.

import scipy.signal
import numpy as np
import matplotlib.pyplot as plt
from skimage import color
from skimage import io


im = plt.imread('dice.jpg')
gray_img = color.rgb2gray(im)

print im.max()

# make some kind of kernel, there are many ways to do this...
t = 1 - np.abs(np.linspace(-1, 1, 16))
kernel = t.reshape(16, 1) * t.reshape(1, 16)
kernel /= kernel.sum()   # kernel should sum to 1!  :) 

im_out =scipy.signal.convolve2d(gray_img,  kernel, mode = 'same')

im_out = im_out / im_out.max() * 255

print im_out.max()

plt.subplot(2,1,1)
plt.imshow(im)
plt.subplot(2,1,2)
plt.imshow(im_out)
plt.show()

enter image description here

tfv
  • 6,016
  • 4
  • 36
  • 67