声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 3985|回复: 4

[编程技巧] matlab中如何保存(imwrite)uint8类型的二值化图片?

[复制链接]
发表于 2011-1-20 10:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
a=[1,1 ,1,1,1,1,1,1,1,1,1;0 ,0, 0,0,0,0,0,0,0,0,0;1,1,1,1,1,1,1,1,1,1,1;
         0,0,0,0,0,0,0,0,0,0,0;1,1,1,1,1,1,1,1,1,1,1;0,0,0,0,0,0,0,0,0,0,0];一张图片如上数组a所示,只有1和0两个值组成,我想把它保存起来,如果直接保存,那么再读入时就全部都是0和255的分布。将其转换成uint8类型(之所以要转换,是因为我要保存的图片很大,double型数据溢出),用imwrite(a,'1.bmp');之后,图像是黑色的,这种保存方式可能是灰度范围取的是0到255,我想请教一下,怎么样将只有0和1两个值的uint8图片保存起来,并且能够正常显示(不会是全黑)?
回复
分享到:

使用道具 举报

发表于 2011-1-20 10:37 | 显示全部楼层
回复 1 # lpp8515 的帖子
这个是你想要的不?
  1. >> a=[1,1 ,1,1,1,1,1,1,1,1,1;0 ,0, 0,0,0,0,0,0,0,0,0;1,1,1,1,1,1,1,1,1,1,1;
  2.          0,0,0,0,0,0,0,0,0,0,0;1,1,1,1,1,1,1,1,1,1,1;0,0,0,0,0,0,0,0,0,0,0];
  3. >> a=uint8(a)

  4. a =

  5.     1    1    1    1    1    1    1    1    1    1    1
  6.     0    0    0    0    0    0    0    0    0    0    0
  7.     1    1    1    1    1    1    1    1    1    1    1
  8.     0    0    0    0    0    0    0    0    0    0    0
  9.     1    1    1    1    1    1    1    1    1    1    1
  10.     0    0    0    0    0    0    0    0    0    0    0

  11. >> imwrite(a,'1.bmp');
  12. >> b=imread('1.bmp')

  13. b =

  14.     1    1    1    1    1    1    1    1    1    1    1
  15.     0    0    0    0    0    0    0    0    0    0    0
  16.     1    1    1    1    1    1    1    1    1    1    1
  17.     0    0    0    0    0    0    0    0    0    0    0
  18.     1    1    1    1    1    1    1    1    1    1    1
  19.     0    0    0    0    0    0    0    0    0    0    0
复制代码
 楼主| 发表于 2011-1-20 11:28 | 显示全部楼层
回复 2 # tenglang 的帖子

我想要的是保存起来的imwrite(a,'1.bmp')这个图,你可以看下这个图是黑色的,并不是黑点白点的二值化分布,a这个矩阵比较小,可以用a1=repmat(a,10,10);将矩阵复制,然后看下imwrite(a1,'1.bmp')这个图,怎么样才能将这个图存成灰度范围为0到1的二值分布呢?
发表于 2011-1-20 11:49 | 显示全部楼层
本帖最后由 tenglang 于 2011-1-20 13:11 编辑

我得到的也不是全黑啊
help中对a的数据类型也有要求. 你直接输入的,没格式化的就是 double类型.

  1. Class Support
  2. The input array A can be of class logical, uint8, uint16, or double. Indexed images (X) can be of class uint8, uint16, or double; the associated colormap, map, must be of class double. Input values must be full (non-sparse).

  3. The class of the image written to the file depends on the format specified. For most formats, if the input array is of class uint8, imwrite outputs the data as 8-bit values. If the input array is of class uint16 and the format supports 16-bit data (JPEG, PNG, and TIFF), imwrite outputs the data as 16-bit values. If the format does not support 16-bit values, imwrite issues an error. Several formats, such as JPEG and PNG, support a parameter that lets you specify the bit depth of the output data.

  4. If the input array is of class double, and the image is a grayscale or RGB color image, imwrite assumes the dynamic range is [0,1] and automatically scales the data by 255 before writing it to the file as 8-bit values.

  5. If the input array is of class double, and the image is an indexed image, imwrite converts the indices to zero-based indices by subtracting 1 from each element, and then writes the data as uint8.

  6. If the input array is of class logical, imwrite assumes the data is a binary image and writes it to the file with a bit depth of 1, if the format allows it. BMP, PNG, or TIFF formats accept binary images as input arrays.
复制代码
你可以这样:
  1. a1=logical(a2);
  2. imwrite(a1,'4.bmp');
复制代码
图直接上来就变了,不知道为什么.

2.zip

1.11 KB, 下载次数: 3

 楼主| 发表于 2011-1-20 14:56 | 显示全部楼层
回复 4 # tenglang 的帖子

logical是将数值型转换成逻辑型,图是出来了,但为什么是这样就不知道了
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-9-21 16:43 , Processed in 0.057770 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表