Difference in zeros values after saving image and then reading again

asked 2014-03-14 05:22:51 -0500

updated 2014-03-14 05:23:47 -0500

berak gravatar image

Here's my code snippet:

        non_zeros = np.transpose(np.nonzero(self.host))
        print 'Before imwrite and imread'
        print non_zeros
        cv2.imwrite(final_img_name ,self.host)
        out = cv2.imread(final_img_name, 0)
        non_zeros = np.transpose(np.nonzero(out))
        print 'After imwrite and imread'
        print non_zeros

The output is as follow:

Before imwrite and imread
[[ 32 328]
 [141 175]]
After imwrite and imread
[[ 32 328]
 [ 32 329]
 [ 33 328]
 [ 35 331]
 [ 35 332]
 [ 36 331]
 [ 39 332]
 [136 171]
 [136 172]
 [137 168]
 [137 169]
 [138 172]
 [138 173]
 [138 174]
 [138 175]
 [139 170]
 [139 171]
 [140 170]
 [140 171]
 [140 175]
 [141 174]
 [141 175]
 [142 168]
 [142 169]
 [142 170]
 [142 174]
 [142 175]
 [143 171]
 [143 172]]

Why non-zeros elements get added ? The values of non-zeros elements change as well (Like 10 become 1,2,3,...).

I'm working in certain application where there's not scope for any tolerance.

What's wrong ? What should I do ?

edit retag flag offensive close merge delete

Comments

2

are you using a lossy format for saving, like jpeg ?

berak gravatar imageberak ( 2014-03-14 05:24:22 -0500 )edit

Exactly what berak suggested. Standard imsave uses compression. You should specify png image format with a 0% compression rate and then your images will stay identical. What you experience are compression artefacts.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-03-14 06:57:15 -0500 )edit