如何用matlab将图像转换成二进制矩阵?

时间:2016-11-30 03:55:38

标签: matlab image-processing

这是我的代码,所以问题是文件不是我需要的格式,我需要一个矩阵,矩阵的每个像素都有R,G和B的值。我也尝试使用-mat作为格式。

%Read the image
im = imread('my_image.png');

%Write to file
save('my_image.txt','-ascii','im')

我需要一个以下格式的矩阵:三列,每一列分别代表R,G和B,以及MxN行,每一列都是图像的不同像素。值必须是0到255之间的数字,但是以8位格式写入

1 个答案:

答案 0 :(得分:0)

使用dlmwrite将矩阵写为ASCII分隔文件。

%Create a matrix of size 4x5x3 with random integers between 1 and 10
>> a = randi([1 10], 4,5,3)

a(:,:,1) =

 5     6     1     9     7
 2     5     1     9     6
10     1     6     8    10
 8     7     1     2     7


a(:,:,2) =

 9     1     9     6     3
 5     2     9     5     5
 5     2     1     7     1
 9     4     4     7    10


a(:,:,3) =

 2     5     1     6     4
 2     4     8    10     8
 4    10     3     5     7
 2    10     5    10     6


%Write the matrix to file
>> dlmwrite('f1.txt', a);

档案f1.txt的内容:

5,6,1,9,7,9,1,9,6,3,2,5,1,6,4
2,5,1,9,6,5,2,9,5,5,2,4,8,10,8
10,1,6,8,10,5,2,1,7,1,4,10,3,5,7
8,7,1,2,7,9,4,4,7,10,2,10,5,10,6

如果您想要一个不同的排序,例如分别为R,G和B组,您可以这样做:

>> dlmwrite('f2.txt', a(:,:,1))
>> dlmwrite('f2.txt', a(:,:,2),'-append')
>> dlmwrite('f2.txt', a(:,:,3),'-append')

档案f2.txt的内容:

5,6,1,9,7
2,5,1,9,6
10,1,6,8,10
8,7,1,2,7
9,1,9,6,3
5,2,9,5,5
5,2,1,7,1
9,4,4,7,10
2,5,1,6,4
2,4,8,10,8
4,10,3,5,7
2,10,5,10,6

编辑1

正如 Suever 正确地说,除非将其展平为2D,否则无法将3D矩阵存储在ASCII文件中。

>> a1 = reshape(a(:,:,1), [],1)
>> a2 = reshape(a(:,:,2), [],1)
>> a3 = reshape(a(:,:,3), [],1)
>> a4 = horzcat(a1, a2, a3)

a4 =

 5     9     2
 2     5     2
10     5     4
 8     9     2
 6     1     5
 5     2     4
 1     2    10
 7     4    10
 1     9     1
 1     9     8
 6     1     3
 1     4     5
 9     6     6
 9     5    10
 8     7     5
 2     7    10
 7     3     4
 6     5     8
10     1     7
 7    10     6

>> dlmwrite('f3.txt', a4)

档案f3.txt的内容:

5,9,2
2,5,2
10,5,4
8,9,2
6,1,5
5,2,4
1,2,10
7,4,10
1,9,1
1,9,8
6,1,3
1,4,5
9,6,6
9,5,10
8,7,5
2,7,10
7,3,4
6,5,8
10,1,7
7,10,6

编辑2

以8位格式写入数字:

%Convert decimal to 8-bit binary
>> b1 = dec2bin(a1, 8);
>> b2 = dec2bin(a2, 8);
>> b3 = dec2bin(a3, 8);

%Create array of blank characters to serve as delimiter
>> delim = blanks(size(b1,1))';

%Horizontal concatenation
>> b4 = horzcat(b1, delim, b2, delim, b3)

b4 =

00000101 00001001 00000010
00000010 00000101 00000010
00001010 00000101 00000100
00001000 00001001 00000010
00000110 00000001 00000101
00000101 00000010 00000100
00000001 00000010 00001010
00000111 00000100 00001010
00000001 00001001 00000001
00000001 00001001 00001000
00000110 00000001 00000011
00000001 00000100 00000101
00001001 00000110 00000110
00001001 00000101 00001010
00001000 00000111 00000101
00000010 00000111 00001010
00000111 00000011 00000100
00000110 00000101 00001000
00001010 00000001 00000111
00000111 00001010 00000110

%Write to file without the default ',' delimiter
>> dlmwrite('f4.txt', b4, '')

f4.txt的内容:

00000101 00001001 00000010
00000010 00000101 00000010
00001010 00000101 00000100
00001000 00001001 00000010
00000110 00000001 00000101
00000101 00000010 00000100
00000001 00000010 00001010
00000111 00000100 00001010
00000001 00001001 00000001
00000001 00001001 00001000
00000110 00000001 00000011
00000001 00000100 00000101
00001001 00000110 00000110
00001001 00000101 00001010
00001000 00000111 00000101
00000010 00000111 00001010
00000111 00000011 00000100
00000110 00000101 00001000
00001010 00000001 00000111
00000111 00001010 00000110