我是靠谱客的博主 彩色小刺猬,最近开发中收集的这篇文章主要介绍python 二次平滑_Python中的图像平滑,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

I wanted to try to write a simple function to smooth an inputted image. I was trying to do this using the Image and numpy libraries. I was thinking that using a convolution mask would be an approach to this problem and I know numpy has a convolve function build in.

How can I use the numpy.convolve to smooth an image?

解决方案

Nice question! tcaswell post here is a great suggestion, but you will not learn much this way because scipy is doing all the work for you! Since your question said you wanted to try and write the function, I will show you a bit more crude and basic kind of way to do it all manually in the hope that you will better understand the maths behind convolution etc, and then you can improve it with your own ideas and efforts!

Note: You will get different results with different shapes/sizes of kernels, a Gaussian is the usual way but you can try out some other ones for fun (cosine, triangle, etc!). I just made up this one on the spot, I think it's a kind of pyramid shaped one.

import scipy.signal

import numpy as np

import matplotlib.pyplot as plt

im = plt.imread('example.jpg')

im /= 255. # normalise to 0-1, it's easier to work in float space

# make some kind of kernel, there are many ways to do this...

t = 1 - np.abs(np.linspace(-1, 1, 21))

kernel = t.reshape(21, 1) * t.reshape(1, 21)

kernel /= kernel.sum() # kernel should sum to 1! :)

# convolve 2d the kernel with each channel

r = scipy.signal.convolve2d(im[:,:,0], kernel, mode='same')

g = scipy.signal.convolve2d(im[:,:,1], kernel, mode='same')

b = scipy.signal.convolve2d(im[:,:,2], kernel, mode='same')

# stack the channels back into a 8-bit colour depth image and plot it

im_out = np.dstack([r, g, b])

im_out = (im_out * 255).astype(np.uint8)

plt.subplot(2,1,1)

plt.imshow(im)

plt.subplot(2,1,2)

plt.imshow(im_out)

plt.show()

最后

以上就是彩色小刺猬为你收集整理的python 二次平滑_Python中的图像平滑的全部内容,希望文章能够帮你解决python 二次平滑_Python中的图像平滑所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(58)

评论列表共有 0 条评论

立即
投稿
返回
顶部