Ziy*_* He 5 opencv tiff scikit-image
我知道opencv有一个BGR命令,但在我的实验中,不仅顺序而且值也完全混乱
import cv2 as cv
import tifffile as tiff
import skimage.io
img_path = r"C:\test\pics\t100r50s16_1_19.tif"
c = cv.imread(img_path,cv.IMREAD_UNCHANGED)
t = tiff.imread(img_path)
s = skimage.io.imread(img_path)
print("c:", c.shape, "t:", t.shape, "s:", s.shape)
print("c:", c.dtype, "t:", t.dtype, "s:", s.dtype)
print(c[0, 0], c[1023, 0], c[0, 1023], c[1023, 1023])
print(t[0, 0], t[1023, 0], t[0, 1023], t[1023, 1023])
print(s[0, 0], s[1023, 0], s[0, 1023], s[1023, 1023])
print(c.sum())
print(t.sum())
print(s.sum())
Run Code Online (Sandbox Code Playgroud)
输出如下:
c: (1024, 1024, 4) t: (1024, 1024, 4) s: (1024, 1024, 4)
c: uint8 t: uint8 s: uint8
[ 50 63 56 182] [131 137 140 193] [29 28 27 94] [123 130 134 190]
[ 79 88 70 182] [185 181 173 193] [74 77 80 94] [180 174 165 190]
[ 79 88 70 182] [185 181 173 193] [74 77 80 94] [180 174 165 190]
# Here seems that opencv only read the alpha channel right,
# the values of first three channels are much different than other package
539623146
659997127
659997127
Run Code Online (Sandbox Code Playgroud)
我使用的图像可以在这里下载.那么,这是我的问题,如何打开cv处理4通道tiff文件?因为当我测试3通道图像时,一切看起来都不错.
一段时间我没有购买它,如链接文章所示,存在舍入错误或与JPEG解码相关的一些错误.
首先是因为你的图像是整数,特别uint8是没有浮点数的舍入,其次是因为你的TIF图像的压缩不是JPEG - 实际上没有压缩.如果您使用ImageMagick,您可以自己查看并执行以下操作:
identify -verbose a.tif
Run Code Online (Sandbox Code Playgroud)
或者如果你使用tiffinfo那艘船libtiff,像这样:
tiffinfo -v a.tif
Run Code Online (Sandbox Code Playgroud)
所以,我通过使用ImageMagick生成样本图像做了一些实验,如下所示:
# Make 8x8 pixel TIF full of RGBA(64,128,192) with full opacity
convert -depth 8 -size 8x8 xc:"rgba(64,128,192,1)" a.tif
# Make 8x8 pixel TIFF with 4 rows per strip
convert -depth 8 -define tiff:rows-per-strip=4 -size 8x8 xc:"rgba(64,128,192,1)" a.tif
Run Code Online (Sandbox Code Playgroud)
并且OpenCV能够正确读取所有这些,但是当我做了以下操作时它出错了.
# Make 8x8 pixel TIFF with RGB(64,128,192) with 50% opacity
convert -depth 8 -define tiff:rows-per-strip=1 -size 8x8 xc:"rgba(64,128,192,0.5)" a.tif
Run Code Online (Sandbox Code Playgroud)
OpenCV中的值为32,64,96 - 是的,正好值HALF正确值 - 就像OpenCV预先乘以alpha一样.所以我尝试了25%的不透明度,并且值是正确值的1/4.因此,我怀疑OpenCV中有一个错误预测alpha.
如果您查看您的值,您将看到tifffile并skimage读取第一个像素:
[ 79 88 70 182 ]
Run Code Online (Sandbox Code Playgroud)
如果你看一下这个像素的alpha值,它是0.713725(182/255),如果你将这些值乘以它们,你会得到:
[ 50 63 56 182 ]
Run Code Online (Sandbox Code Playgroud)
这正是OpenCV所做的.
作为一种解决方法,我猜你可以将alpha除以正确的比例.
如果参数是OpenCV 故意预先乘以alpha,那么这就引出了为什么它为TIFF文件而不是PNG文件的问题:
# Create 8x8 PNG image full of rgb(64,128,192) with alpha=0.5
convert -depth 8 size 8x8 xc:"rgba(64,128,192,0.5)" a.png
Run Code Online (Sandbox Code Playgroud)
查看OpenCV:
import cv2
c = cv2.imread('a.png',cv2.IMREAD_UNCHANGED)
In [4]: c.shape
Out[4]: (8, 8, 4)
In [5]: c
Out[5]:
array([[[192, 128, 64, 128],
[192, 128, 64, 128],
...
...
Run Code Online (Sandbox Code Playgroud)
如果有人认为TIF文件中的值与OpenCV报告的那样,我只能说我写了rgb(64,128,192),不透明度为50%,我测试了以下各项,发现他们都同意,唯一的例外这个文件正是包含的OpenCV:
| 归档时间: |
|
| 查看次数: |
268 次 |
| 最近记录: |