在处理图像时如何规范scipy的convolve2d?

时间:2016-04-21 04:55:30

标签: python opencv numpy scipy

我正在使用scipy的convolve2d:

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

卷积后,所有数值均为10000s,但考虑到我处理图像,我需要它们在0-255的范围内。我如何将其标准化?

1 个答案:

答案 0 :(得分:0)

假设您想在一张图片中进行标准化,您只需使用im_out = im_out / im_out.max() * 255即可。

您还可以规范化内核或原始图像。

以下示例。

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