scipy.ndimage.zoom结果取决于图像大小 [英] scipy.ndimage.zoom result depends on image size

查看:1695
本文介绍了scipy.ndimage.zoom结果取决于图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到scipy.ndimage.zoom的结果取决于原始图像的大小.在下面的代码示例中,将生成一个棋盘图像,然后使用ndimage.zoom进行缩放.如果一个棋盘格拼贴仅为2x2像素,则缩放系数似乎太大,并且裁剪出了生成的图像.相反,如果图块的尺寸为10x10,则结果看起来不错.

I noticed that the result of scipy.ndimage.zoom depends on the size of the original image. In the following code sample a checkerboard image is generated and then zoomed with ndimage.zoom. If one checkerboard tile is just 2x2 pixels, the zoom factor seems to be too large and the resulting image gets cropped. In contrast, if the tile has dimensions 10x10, the result looks good.

from __future__ import division

import numpy as np
from scipy import ndimage, misc
import wx

y,x = 2,2   # change tile size here
imgdata = np.zeros((y,x),dtype='uint8')
imgdata[y/2:,x/2:] = 255
imgdata[:y/2,:x/2] = 255
imgdata = np.tile(imgdata,(4,4))
imgdata = np.array((imgdata,imgdata,imgdata))
d,y,x = imgdata.shape

zoom = 200.0/y

w, h = int(x*zoom), int(y*zoom)

app = wx.App(None)

zoomed = np.ascontiguousarray(ndimage.interpolation.zoom(imgdata,[1,zoom, zoom],order=0).transpose((1,2,0)), dtype='uint8')
image = wx.ImageFromBuffer(w, h, zoomed)
image.SaveFile('zoomed.png',wx.BITMAP_TYPE_PNG)

02x02磁贴:

10x10瓦片:

据我所知,我一直在使用scipy.misc.imresize,它没有显示此行为,但我想避免对PIL的额外依赖.

Up to know I have been using scipy.misc.imresize which does not show this behaviour but I want to avoid the additional dependency on PIL.

我做错什么了吗,或者这是缩放错误吗?

Am I doing something wrong or is this a bug in zoom?

推荐答案

自您发布问题以来已有一段时间...万一您仍然有兴趣,我也遇到了类似的问题,并使用了以下内容:

i'ts been a while since you posted your question... in case you're still interested, I had a similar problem and used the following:

import skimage
data_new = skimage.transform.resize(data_old, [new_shape_x, new_shape_z], order = 0)

请确保将order设置为0,因为默认值为order = 1,这将导致值之间的一阶样条插值(这会导致图块在其边界处模糊).

Make sure you set order = 0, because the default is order = 1, which will result in a first order spline interpolation between the values (this causes the tiles to blur at their boundaries).

无论如何,我不知道这是否是个好方法,尽管它对我有用.如果是bug,我就无法回答,因为我对编程真的不了解.此外,我还尝试使用scipy.ndimage.interpolation.zoom函数,但是像您的情况一样,图块的边界不在应有的位置.因此,我使用了skimage.

Anyway, I don't know if that's a good way to do it, it worked for me though. I can't answer if it's a bug, because I really don't know enough about programming to answer that. Furthermore I also tried to use the the scipy.ndimage.interpolation.zoom function, but then the boundaries of the tiles weren't where they should be, like in your case. Therefore I used skimage.

如果您对上下文感兴趣:我研究了断裂力学,需要创建平滑变化的随机强度分布.因此,我创建了一个由窦和余弦函数组成的曲面,该曲面在x和z方向上具有一定数量的周期.然后,我获取该曲面的绝对值,然后将其乘以不规则的棋盘状曲面.在棋盘状表面上每个方向上的瓦片数必须与相应的强度变化表面中的周期数/2相匹配.最终表面的计算如下(逐段加法和乘法):

In case you're interested in the context: I worked on fracture-mechanics and needed to create random strength-distributions that are varying smoothly. So I created a surface with a combination of sinus and cosinus funcitons, that had a certain number of periods in x and z direction. Then I took the absolute value of that surface and multiplied it with an irregular chessboard-like surface. The numbers of tiles in each direction on the chessboard-like surface had to match the number of periods/2 in the corresponding strength-variation surface. The final surface was calculated as follows (piecewise addition and multiplication):

strength_surface[i,j] = strength_mean[i,j] + random_grid[i,j] * strength_variation[i,j]

必须调整random_grid的大小以匹配其他曲面的形状.

where random_grid had to be resized to match the shape of the other surfaces.

这篇关于scipy.ndimage.zoom结果取决于图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆