【发布时间】:2018-12-20 17:47:38
【问题描述】:
我正在开发基于微软论文Whiteboard scanning and image enhancement的自动图像增强
在“白平衡和图像增强”部分,他们提供了增强步骤:
首先:他们估计扫描文档或检测到的白板的背景:
1. "将白板区域划分为矩形单元格,单元格大小应与我们预期的大致相同 板上单个字符的大小(在我们的实现中为 15 x 15 像素)。”
然后
2. "将每个单元格中的像素按亮度值排序。由于墨水吸收入射光,因此 白板像素高于笔画像素。因此,单元格内的白板颜色是 最高亮度。在实践中,我们平均前 25 个百分位像素的颜色,以减少 传感器噪声引入的误差"
然后
3. "通过在RGB空间中局部拟合一个平面来过滤单元格的颜色。偶尔会有单元格 完全被笔划覆盖,因此在步骤 2 中计算的单元格颜色不正确。那些颜色是 被局部拟合平面拒绝为异常值,并被其邻居的插值替换。"
我的问题在于第二步和第三步:
他们如何获得亮度值,我应该将输入图像转换为 YUV 色彩空间并从 Y 通道获取亮度值还是只在 RGB 色彩空间上工作?
如何在 RGB 空间中拟合局部平面?
这是我的 python 代码,我试图从输入图像中生成单元格,从 YUV 颜色空间中获取亮度值,以及一个简单的结果,与他们在论文中得到的结果相比似乎不正确。
Python 代码:
import cv2
import numpy as np
## Return List of cells from a given Image
def SubImage(image):
Cells = []
CellRows = []
for i in range(0,rows/CellSize):
subIm = image[i*CellSize:(i+1)*CellSize,:]
CellRows.append(subIm)
for img in CellRows:
for i in range(0,cols/CellSize):
subIm = img[:,i*CellSize:(i+1)*CellSize]
Cells.append(subIm)
return Cells
## Sort luminosity Value
def GetLuminance(Cells):
luminance = []
for cel in Cells:
luminance.append(cel.max())
return luminance
## Estimate the background color of the white board
def UniformBackground(CelImage,img,luminance):
a = 0
for c in range(0,len(CelImage)):
cel = CelImage[c]
for i in range(0,cel.shape[0]):
for j in range(0, cel.shape[1]):
cel[i,j] = min(1,cel[i,j]/ luminance[c])
for i in range(0,rows/CellSize):
for j in range(0,cols/CellSize):
img[i*CellSize:(i+1)*CellSize,j*CellSize:(j+1)*CellSize] = CelImage[a]
a = a + 1
if __name__ == '__main__':
img = cv2.imread('4.png')
CellSize = 15
rows,cols,depth = img.shape
if (rows%CellSize !=0):
rows = rows - rows%CellSize
if (cols%CellSize !=0):
cols = cols - cols%CellSize
yuvImg = cv2.cvtColor(img, cv2.COLOR_BGR2YUV)
# Get cells from Y channel
CellsY = SubImage(yuvImg[:,:,0])
CellsB = SubImage(img[:,:,0])
CellsG = SubImage(img[:,:,1])
CellsR = SubImage(img[:,:,2])
# Get Luminance From Y cells
LuminanceY = GetLuminance(CellsY)
# Uniform Background
UniformBackground(CellsB, img[:,:,0], LuminanceY)
UniformBackground(CellsG, img[:,:,1], LuminanceY)
UniformBackground(CellsR,img[:,:,2], LuminanceY)
#bgrImg = cv2.cvtColor(imgB, cv2.COLOR_GRAY2BGR)
#print imgB
cv2.imwrite('unifrom.jpg',img)
输入白板图像:
输出图片:
预期输出:
【问题讨论】:
-
我猜你的输出图像类型是
float,来自cel[i,j] = min(255,cel[i,j]/ luminance[c])这一行。将其转换为类型int。 -
好的,我现在就试试
-
我像这样转换它 img = np.uint8(img) 仍然是相同的结果,但这不是我的问题
标签: python opencv computer-vision image-enhancement