scipy imsave保存错误的值 [英] scipy imsave saves wrong values

查看:132
本文介绍了scipy imsave保存错误的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写将使用numpy和scipy生成视差图的代码,但是我存储在numpy数组中的图像值与保存在输出图像中的实际值完全不同与misc.imsave.例如,在数组中,没有一个值大于22,但是在图像中,我具有从0到255的所有值.我认为也许imsave正在拉伸这些值,以使最大值显示为图片中有255张图片,但我还有其他使用imsave创建的图片,图片的最大值低于255张.

I'm trying to write code that will produce disparity maps using numpy and scipy, but the values that I store in my numpy array for my images are completely different from the values that are actually showing up in my output images, saved with misc.imsave. For example, in the array, none of the values are greater than 22, but in the image, I have a full range of values from 0 to 255. I thought that perhaps imsave was stretching the values so that the max value showed up as 255 in the image, but I have other images created with imsave that have a max below 255.

这些是我用来创建视差图的函数,给定两个pgm图像已沿x轴移动:

These are the functions I'm using to create my disparity maps, given two pgm images that have been shifted along the x axis:

def disp(i, j, winSize, leftIm, rightIm): #calculate disparity for a given point
    width = leftIm.shape[1]
    height = leftIm.shape[0]
    w = winSize / 2
    minSAD = 9223372036854775807 #max int
    for d in range(23):
        SAD = 0.0 #SAD
        k = i - w
        v = i + w
        m = j - w
        n = j + w
        for p in range(k, v+1): #window - x
            for q in range(m, n+1): #window y
                if(p - d > 0 and p < width and q < height):
                    SAD += abs((int(leftIm[q][p]) - int(rightIm[q][p - d])))
        if(SAD < minSAD):
            minSAD = SAD
            disp = d
    #       print "%d, %d" % (i, j)
    return (disp, SAD)

def dispMap(winSize, leftIm, rightIm):
    width = leftIm.shape[1]
    height = leftIm.shape[0]
    outIm = np.zeros((height, width)) 
    SADstore = np.zeros((height, width))
    w = winSize / 2
    for i in range(w, width-w):
        for j in range(w, height/3-w):
            dispout =  disp(i, j, winSize, leftIm, rightIm)
            outIm[j][i] = 1 * dispout[0] #should normally multiply by 4
            SADstore[j][i] = dispout[1]
    return (outIm, SADstore)

忽略SAD/SADstore返回值,我已确保这些值不会影响我当前的过程.

Ignore the SAD/SADstore return values, I have ensured that these are not effecting my current process.

这是我用来获取输出的代码:

This is the code I'm using to get my output:

disp12 = dispMap(9, view1, view2)
disp12im = disp12[0]
misc.imsave('disp121.pgm', disp12im)

就目前而言,disp12im中的任何内容都不应大于23.但是,如果我加载保存的图像并对值进行相同的for循环操作,则会得到超过23的大量数字.我在做什么错了?

As it current is, nothing in disp12im should be > 23. If I run a for loop to check this on the array, this remains true. However, if I load the saved image and run that same for loop on the values, I get tons of numbers over 23. What am I doing wrong?

推荐答案

当数组的dtypenp.float64(数据类型为disp12im)更改为存储的8位值时,数据将重新缩放在图像中.

The data gets rescaled when the dtype of the array is changed from np.float64 (the data type of disp12im) to the 8 bit values stored in the image.

为避免这种情况,请将图像转换为数据类型np.uint8,然后再将其提供给imsave:

To avoid this, convert your image to data type np.uint8 before giving it to imsave:

misc.imsave('disp121.pgm', disp12im.astype(np.uint8))

例如,我将将此x保存为PGM图像:

For example, I'll save this x as a PGM image:

In [13]: x
Out[13]: 
array([[  1.,   3.,   5.],
       [ 21.,  23.,  25.]])

In [14]: x.dtype
Out[14]: dtype('float64')

保存x不变,然后将其读回:

Save x unaltered, and then read it back:

In [15]: imsave('foo.pgm', x)

In [16]: imread('foo.pgm')
Out[16]: 
array([[  0,  21,  42],
       [212, 234, 255]], dtype=uint8)

这些值已放大到整个8位范围.

The values have been scaled up to the full 8-bit range.

相反,在保存之前将x转换为np.uint8,然后将其读回:

Instead, convert x to np.uint8 before saving, and then read it back:

In [17]: imsave('foo.pgm', x.astype(np.uint8))

In [18]: imread('foo.pgm')
Out[18]: 
array([[ 1,  3,  5],
       [21, 23, 25]], dtype=uint8)

这篇关于scipy imsave保存错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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