来自Matlab矩阵的图像 [英] images from matrix in matlab

查看:89
本文介绍了来自Matlab矩阵的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一个matlab代码,该代码将加载写入文本文件中的矩阵,然后将其显示为图像.文本文件以随机方式包含从0到2的整数,我想用不同的颜色表示每个整数,例如白色为0,某些颜色为1,其他颜色为2.我将在下面提供Matlab代码:

I have written a matlab code which would load the matrix written in a text file and then I want to show it as an image. The text file contains integers number from 0 to 2 in a random fashion and I want to represent each in a different colors e.g. 0 in white,1 in some color and 2 in a different color. I would provide the matlab code just below :

clc;
clear all;

for i=1:10
 k=num2str(i);
 f = strcat('test_file_num_',k,'.txt');
 a{i} = fileread(f);

 [m,n] = size(a{i});

 a{i} = reshape(a{i},12,10);
 a{i} = a{i}';
 a{i} = a{i}(:,1:10);

end

文件夹中有10个文本文件,每个文件包含一个随机矩阵,该矩阵包含从0到2的整数,每个文本文件的名称均以"test_file_num_"开头,在{i}中,我有一个10x10矩阵.现在,我想将a {i}矩阵表示为图像或图形或其他任何东西,但是要有一些东西可以用不同的颜色显示矩阵的内容.感谢您的所有帮助.

There are 10 text file in the folder each of which contain a random matrix containing integer numbers 0 to 2 and name of each text file starts with "test_file_num_" and in a{i} I have the matrix which is 10x10 matrix. Now I want to represent the a{i} matrix as an image or graph or anything but to have something which would show the contents the matrix in a different color. Thanks for all your help.

推荐答案

如果文件中的数据是结构化的,则意味着数字之间有制表符或空格,您可以直接使用importdata将数据加载到矩阵中.之后,您可以使用imagesc生成图像.要分配颜色,您可以使用colormap功能更改颜色图.因此您的代码应如下所示:

If the data in your file is structured, meaning has tabs or spaces between the numbers you can directly use importdata to load your data into a matrix. After that, you can use imagesc to produce the image. To assign the colors you can change the colormap using the colormap function. So your code would look something like this:

% Example data
A = floor(3*rand(10)); 
% Change colormap with just 3 colors (Red,Green,Blue)
cmap = [1 0 0;0 1 0;0 0 1];
figure; imagesc(A); colormap(cmap); colorbar;

希望有帮助!

这篇关于来自Matlab矩阵的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆