Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
mat2gray

Subject: mat2gray

From: Bridget

Date: 14 Oct, 2010 13:13:04

Message: 1 of 3

I have a 5x716 matrix that I am trying to make into an image. When I use mat2gray() it produces the image, but as a single line. How do I make it so that each row in the matrix is a new row in the image?

Subject: mat2gray

From: Sean

Date: 14 Oct, 2010 14:00:07

Message: 2 of 3

"Bridget " <moomoo_453@hotmail.com> wrote in message <i96vl0$eb0$1@fred.mathworks.com>...
> I have a 5x716 matrix that I am trying to make into an image. When I use mat2gray() it produces the image, but as a single line. How do I make it so that each row in the matrix is a new row in the image?

I'm unable to replicate your problem. Are you possibly just not zoomed in enough?

 A = magic(700);
 A = A(1:5,:);
B = mat2gray(A);
imshow(B)
whos
%{
  Name Size Bytes Class Attributes

  A 5x700 28000 double
  B 5x700 28000 double
%}

Subject: mat2gray

From: ImageAnalyst

Date: 14 Oct, 2010 14:13:46

Message: 3 of 3

On Oct 14, 9:13 am, "Bridget " <moomoo_...@hotmail.com> wrote:
> I have a 5x716 matrix that I am trying to make into an image. When I use mat2gray() it produces the image, but as a single line. How do I make it so that each row in the matrix is a new row in the image?
----------------------------------------------------------------------------------------------------------------------------------------------
Each row is a row - nothing extra to do is required.
Whether you even need to call mat2gray depends on what you want to
do. Your matrix is already an image and you can use it directly for
many things, such as using imshow() or something. On the other hand
if you *need* to normalize your array to the range 0-1 for some reason
(such as calling imhist()), then you can call mat2gray if you want.

Try this code. It will show the image and the histogram

clc;
clear;
close all;
workspace;
fontSize = 20;

% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end

% Generate some sample data in a 5x716 matrix.
grayImage = rand(5, 716);
% Display the original gray scale image.
subplot(1, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Enlarge figure to full
screen.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

% Let's compute and display the histogram.
normalizedImage = mat2gray(grayImage);
[pixelCount grayLevels] = imhist(normalizedImage);
subplot(1, 2, 2);
bar(pixelCount);
title('Histogram of image', 'FontSize', fontSize);
xlabel('Bin Number', 'FontSize', fontSize);
ylabel('Pixel Count', 'FontSize', fontSize);

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us