how display a gray image in matlab gui - matlab

I have an image and I want to convert that image into gray. After that, I tried to display that gray image in GUI but it became exactly look like a rainbow. How can I display a gray image in GUI? I tried the below code but it does not work. Please help
X = imread('j3.jpg');
X=rgb2gray(X);
axes(handles.axes1);
imshow(X)
But I got the following error:
Error using matlab.ui.Figure/set
While setting the 'Colormap' property of 'Figure':
Value must be either:
an Mx3 array of type single or double in the range [0 1]
an Mx3 array of type uint8
Error in images.internal.basicImageDisplay (line 71)
set(fig_handle, 'Colormap', map);
Error in imshow (line 293)
hh = images.internal.basicImageDisplay(fig_handle,ax_handle,...
Error in gray>pushbutton1_Callback (line 83)
imshow(X)
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in gray (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>#(hObject,eventdata)gray('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
What is the issue and how can it be resolved?

It appears that you have named your GUI gray.m which conflicts with the builtin gray function which provides the grayscale colormap (the default for imshow). As a result, when MATLAB tries to create the grayscale colormap (colormap(gray)), it instead calls your GUI and the output is not what colormap is expecting, resulting in the error that you've shown.
You will need to rename your GUI to something else and you should have no issues.

Related

How do I successfully load a .TIFF image into MATLAB?

I have a TIFF image that I am trying to read with the following code in MATLAB:
I = imread('example.tiff');
imshow(I)
However, when I try to do so, I get the following error:
Error using images.internal.imageDisplayValidateParams>validateCData (line 118)
Multi-plane image inputs must be RGB images of size MxNx3.
Error in images.internal.imageDisplayValidateParams (line 30)
common_args.CData = validateCData(common_args.CData,image_type);
Error in images.internal.imageDisplayParseInputs (line 79)
common_args = images.internal.imageDisplayValidateParams(common_args);
Error in imshow (line 253)
images.internal.imageDisplayParseInputs({'Parent','Border','Reduce'},preparsed_varargin{:});
When I save the same image in JPG format and use the same code to read it, I have no issues loading it into the image viewer. Can anyone diagnose the problem and offer workarounds? I just want to understand what is going on here. Is the file simply too big?

suptitle Error using axes Invalid axes handle

I am trying to create histograms in a loop. I am creating two figures and want a suptitle above them, but when I do that the title of the subplots doesn't work anymore . This is my code
suptitle('Observation')
for i=1:c:b
i
MagObs1=[];
subplot(b,1,i);
MagObs1=MagObs(:,i);%0 and 1s
minMagObs1=min(MagObs1);
MagObs2=MagObs1(MagObs1>0.001);
h1=histogram(MagObs2,NumberBins,'Normalization','probability');
title([num2str(DepthObs(i)),'m']);
h1.BinLimits=[bottomVel topVel];
xlabel('Current speed (m/s)');
ylabel('Frequency');
end
figure(2);% clf;
suptitle('Model')
for i=1:c:b
subplot(b,1,i);
h2=histogram(MagMatrixH1(i,:),NumberBins,'Normalization','probability')
title([num2str(DepthObs(i)),'m'])
h2.BinLimits=[bottomVel topVel]
xlabel('Current speed (m/s)')
ylabel('Frequency')
end
and this is the error I get
Error using axes
Invalid axes handle
Error in suptitle (line 98)
axes(haold);
Error in Histogram (line 118)
suptitle('Observation')
This is my output. Normally, every figure has multiple histograms underneath each other, but for this example I only show one.
As you can see in the second picture. "Im" should be '300m', could you help me fix this subplots title?
In the (very little) documentation that suptitle has, it says:
"Use this function after all subplot commands."
Try adding it in the end of your plotting

error encountered (validattribute ) while running surf matlab code

This code produces error:
validateattribute(I{'logical','unit8'.....
Please anyone can tell me why this is so..
I = imread('flowers.jpg');
I=im2double(I);
points = detectSURFFeatures(I);
imshow(I); hold on;
plot(points.selectStrongest(10));
detectSURFFeatures takes grayscale images. If your image is RGB, you should call rgb2gray frist.
The error message should be telling you something to that effect.

Matlab's Canny edge function not working

When I try to run
BW = edge(im,'canny')
where im is my image (256X256 uint8).
This is the error I get:
Error using gradient (line 3)
Not enough input arguments.
Error in edge>smoothGradient (line 709)
derivGaussKernel = gradient(gaussKernel);
Error in edge (line 213)
[dx, dy] = smoothGradient(a, sigma);
Error in ps_1_1 (line 2)
BW = edge(im,'canny')
As the function works fine for me when I tested it I think you probably pass an image to the function which is not grayscale (meaning each pixels having one gray value), if that is not the case either, try to reinstal the library because as antony mentioned in the comments, the function works fine. but anyway be sure to read the edge document carefully.

mat2gray with structure fields

I'm trying to display a matrix as an image with mat2gray and imshow. The data is saved as part of a structure. When I call the field separately I can display the image fine, but if I try to do it inside of a for loop it doesn't work. Does any one have an idea why this might be happening?
Example:
imshow(mat2gray(MyVals(5).Data))
Result: Success!
for L=1:numel(MyVals)
imshow(mat2gray(MyVals(L).Data))
end
Result:
Attempted to access limits(2); index out of bounds because
numel(limits)=0.
Error in mat2gray (line 38) if limits(2)==limits(1) % Constant Image