【发布时间】:2016-10-15 21:52:55
【问题描述】:
我正在使用 opencv + python 来处理眼底(视网膜图像)。在将 float64 图像转换为 uint8 图像时,我遇到了一个问题。
以下是python代码:
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] > 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] > 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)
Ar[][] 有 -ve 和 +ve 值,Thr 有 -ve 值。 Dd 是我要显示的图像。 问题是TDd 显示了一个奇怪的图像(255s 和 0s 被分配给适当的像素,我检查了但是显示的图像很奇怪,与TDd不相似
原图
红色通道图像:
TDd(Dd 的 uint8):
TDd2(与 Dd 相同)
Dd2(初始化时声明 uint8 dtype)
为什么TDd 和TDd2 图像不同?
由于这两张图像中像素的灰度值之间的差异(据我所知)只有 255, 0 (TDd) 和 255.0, 0.0 (在 TDd2).
如果有人能告诉我,那将是一个很大的帮助。
【问题讨论】: