【问题标题】:Convert a Matrix of doubles to a grayscale .tif image将双精度矩阵转换为灰度 .tif 图像
【发布时间】:2016-08-03 16:40:24
【问题描述】:

我有一个 nCol by nRow 的双精度矩阵,我想将其转换为 nCol by nRow 像素灰度图像。我不想将其缩减为缩放到 256 个通道的图像。我很乐意使用单打而不是双打。我查看了 Mathworks 的 Tiff 类文档,但找不到一个简单的示例。

【问题讨论】:

  • 你的矩阵的值是多少?因为只是二维矩阵已经是灰度的。彩色图像有很多维度。试试imshow(yourmatrix)
  • 这不是我想要的。
  • 那么请展示一些例子。
  • 如果我运行 imshow 然后另存为图像,则分辨率与 nCol 相差 nRow 像素。我不想改变图中的设置并调用saveas,那是低效的。我宁愿直接在Matlab中使用Tiff class,直接写图。

标签: matlab image-processing tiff


【解决方案1】:

我尝试阅读有关 Tiff 类的信息,是的,它的文档记录很差。但我发现这是经过反复试验:

假设我们有矩阵

data = rand(100,200);

让我们将其保存为 Tiff:

t  = Tiff('new.tif','w')                                   %create object of Tiff class
setTag(t,Tiff.TagID.ImageLength,size(data,1))              %define image dimentions
setTag(t,Tiff.TagID.ImageWidth,size(data,2))

setTag(t,'Photometric', Tiff.Photometric.MinIsBlack)       %define the color type of image

%specifies how image data components are stored on disk
setTag(t,'PlanarConfiguration',Tiff.PlanarConfiguration.Chunky);

setTag(t,'BitsPerSample',64);                              %because 1 double = 8byte = 64bits

%Specify how to interpret each pixel sample (IEEEFP works with input doubles)
setTag(t,'SampleFormat',Tiff.SampleFormat.IEEEFP);         
t.write(data)
t.close

现在让我们测试一下:

datac = imread('new.tif')
whos datac
Name         Size              Bytes  Class     Attributes
datac      100x200            160000  double  

还有:

datac(:,1)

ans =

0.4921
0.6908
0.1544
0.4433
...

【讨论】:

  • 谢谢你 - 实际上我已经做了类似的事情,但是当我试图在 Windows 中打开 .tif 时,我收到一个错误,指出文件已损坏。但是,我可以在 Matlab 中打开图像。知道为什么 Windows 会看到损坏的文件吗?
  • 首先我在考虑“RowsPerStrip”属性,因为它的值不正确。但是修复这个值并没有帮助!我认为问题在于 Windows 无法识别双打的 tiff!此外,由于 tiff 始终是 3d 矩阵,因此创建 2d tiff 可能是不正确的......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多