Scipy imsave saves invalid values

I am trying to write code that will create mismatch 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 actually appear on my output images are stored with misc.immave. For example, in the array, none of the values โ€‹โ€‹is greater than 22, but in the image I have a full range of values โ€‹โ€‹from 0 to 255. I thought that perhaps imsave stretches the values โ€‹โ€‹so that the maximum value is displayed as 255 in the image, but I have There are other images created with imsave that have a maximum below 255.

These are the functions that I use to create my mismatch maps, given the two pgm images that were 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) 

Ignore the returned SAD / SADstore values, I made sure that they do not affect my current process.

This is the code I use to get my output:

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

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

+5
source share
1 answer

Data is scaled when the dtype array is changed from np.float64 (data type disp12im ) to 8-bit values โ€‹โ€‹stored in the image.

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

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

For example, I will 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') 

Save x unchanged and then read it:

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

Values โ€‹โ€‹have been increased to the full 8-bit range.

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

 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) 
+3
source

Source: https://habr.com/ru/post/1235230/


All Articles