Image display error after changing dtype of image matrix
<p>I'm using opencv + python to process fundus(retinal images). There is a problem that im facing while converting a float64 image to uint8 image.</p>

<p><strong>Following is the python code:</strong></p>

<pre><code>import cv2
import matplotlib.pyplot as plt
import numpy as np
from tkFileDialog import askopenfilename

filename = askopenfilename()
a = cv2.imread(filename)
height, width, channel = a.shape
b, Ago, Aro = cv2.split(a)
mr = np.average(Aro)
sr = np.std(Aro)

Ar = Aro - np.mean(Aro)
Ar = Ar - mr - sr

Ag = Ago - np.mean(Ago)
Ag = Ag - mr - sr

#Values of elements in Ar
#    Ar = [[-179.17305527, -169.17305527, -176.17305527, ..., -177.17305527, -177.17305527, -177.17305527],
#           [-178.17305527, -169.17305527, -172.17305527, ..., -177.17305527, -177.17305527, -177.17305527],
#           [-179.17305527, -178.17305527, -179.17305527, ..., -177.17305527, -177.17305527, -177.17305527],
#           ...,
#           [-177.17305527, -177.17305527, -177.17305527, ..., -177.17305527, -177.17305527, -177.17305527],
#           [-177.17305527, -177.17305527, -177.17305527, ..., -177.17305527, -177.17305527, -177.17305527],
#           [-177.17305527, -177.17305527, -177.17305527, ..., -177.17305527, -177.17305527, -177.17305527]]

Mr = np.mean(Ar)
SDr = np.std(Ar)

print "MR = ", Mr, "SDr = ", SDr

Mg = np.mean(Ag)
SDg = np.std(Ag)
Thg = np.mean(Ag) + 2 * np.std(Ag) + 50 + 12

Thr = 50 - 12 - np.std(Ar)
print "Thr = ", Thr
Dd = np.zeros((height, width))
Dc = Dd
for i in range(height):
    for j in range(width):

        if Ar[i][j] &gt; Thr:
            Dd[i][j] = 255
        else:
            Dd[i][j] = 0

TDd = np.uint8(Dd)
TDd2 = Dd

for i in range(height):
    for j in range(width):
        if Ag[i][j] &gt; Thg:
            Dc[i][j] = 1
        else:
            Dc[i][j] = 0

#CALCULATING RATIO
ratio = 500.0 / Dd.shape[1]
dim = (500, int(Dd.shape[0] * ratio))
#
# #RESIZING TO-BE-DISPLAYED IMAGES
resized_TDd = cv2.resize(TDd, dim, interpolation=cv2.INTER_AREA)
resized_TDd2 = cv2.resize(TDd2, dim, interpolation=cv2.INTER_AREA)
resized_original = cv2.resize(Aro, dim, interpolation=cv2.INTER_AREA)

cv2.imshow('TDd', resized_TDd)
cv2.imshow('TDd2', resized_TDd2)
cv2.imshow('Aro', resized_original)
cv2.waitKey(0)
</code></pre>

<p><code>Ar[][]</code> has -ve as well as +ve values and <code>Thr</code> has a -ve value. <strong><code>Dd</code> is the image which i want to display.</strong> The problem is that <code>TDd</code> displays a bizarre image <em>(255s and 0s are being assigned to appropriate pixels, i checked</em> but the image being displayed is weird and not similar to <code>TDd</code></p>

<p><strong>Original image</strong></p>

<p><a href="https://i.stack.imgur.com/vxtIU.jpg" rel="nofollow"><img src="https://i.stack.imgur.com/vxtIU.jpg" alt="enter image description here"></a></p>

<p><strong>Red channel image:</strong></p>

<p><a href="https://i.stack.imgur.com/ZWqCL.png" rel="nofollow"><img src="https://i.stack.imgur.com/ZWqCL.png" alt="enter image description here"></a></p>

<p><strong>TDd (uint8 of Dd):</strong></p>

<p><a href="https://i.stack.imgur.com/HOCd9.png" rel="nofollow"><img src="https://i.stack.imgur.com/HOCd9.png" alt="enter image description here"></a></p>

<p><strong>TDd2 (same as Dd)</strong></p>

<p><a href="https://i.stack.imgur.com/iv7al.png" rel="nofollow"><img src="https://i.stack.imgur.com/iv7al.png" alt="enter image description here"></a></p>

<p><strong>Dd2 (declared uint8 dtype while initializing)</strong></p>

<p><a href="https://i.stack.imgur.com/ZKHwT.png" rel="nofollow"><img src="https://i.stack.imgur.com/ZKHwT.png" alt="enter image description here"></a></p>

<p>Why are the <code>TDd</code> and <code>TDd2</code> images different? 
<strong>Since the difference between the gray values of the pixels (as far as i understand and know) in these 2 images is only 255, 0 <em>(in TDd)</em> and 255.0, 0.0 <em>(in TDd2)</em></strong>.</p>

<p>It would be a great great help if someone could tell me.</p>

<p>Usually when Images are represented with np float64 arrays, RGB values are in range 0 to 1. So when converting to uint8 there is a possible precision loss when converting from float64 to uint8.</p>

<p>I would directly create Dd as a:</p>

<pre><code>Dd = np.zeros((height, width), dtype=np.uint8)
</code></pre>

<p>Then it should work.</p>

python opencv numpy