MATLAB Answers

Why rgb2gray(d​ouble(imre​ad('pictur​e.jpg')) gives a matrix of value 1?

30 views (last 30 days)
Aude Rapet
Aude Rapet on 16 Jan 2017
Commented: Aude Rapet on 16 Jan 2017
Hi, I am a beginner in Matlab, I try to read my picture that I cropped with paint and convert it to double, and I obtain:
135x194x3 double with
sans=double(imread('aft2.jpg'));
I want to use it with the function normxcorr2, and I have the error message:
Error using normxcorr2
Expected input number 1, T, to be two-dimensional.
Then, I try to convert it with rgb2gray to eliminate the "x3" :
sans=rgb2gray(double(imread('aft2.jpg')));
but it gives me 135x194 double of 1. (my matrix is only composed of the value 1). I obtain this error message:
The values of TEMPLATE cannot all be the same.
So I tried to change my line :
sans=double(rgb2gray(imread('aft2.jpg')));
and it seems that it is working but I dont understand why and what is the difference if I invert rgb2gray and double.
Thank you for your help Aude

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 16 Jan 2017
Edited: Image Analyst on 16 Jan 2017
You're trying to do too much at once. Simply read it in - it will most likely be a uint8 - then call rgb2gray:
sans = imread('aft2.jpg');
sans2 = rgb2gray(sans); % Convert uint8 color to uint8 gray scale.
sans3 = double(sans2); % Convert to double in the range 0-255.
If you want it to be in the range 0-1 then define sans3 this way:
% Scale min to 0 and max to 1
sans3 = mat2gray(sans2);
or
% Scale 0 to 0 and max to 1
sans3 = double(sans2) / max(sans2(:));

More Answers (0)

Sign in to answer this question.