How do I successfully load a .TIFF image into MATLAB? - 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?

Related

how display a gray image in matlab gui

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.

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.

Montage of grayscale and colour images

Hi all I am using montage command of matlab to display images. However I am facing a problem. The command that I use is given below:
dirOutput = dir('C:\Users\DELL\Desktop\book chapter\Journal chan vese\robust
contour initialization\book for document\4 phase\*.jpg');
fileNames = {dirOutput.name}'
montage(fileNames, 'Size', [1 6]);
export_fig combined1.jpg -r300
I have 6 images (all grayscale). However, the command prompt immediately throws an error like this:
//Error using montage>getImagesFromFiles (line 349)
//FILENAMES must contain images that are the same size.
//Error in montage>parse_inputs (line 225)
// [I,cmap] = getImagesFromFiles(varargin{1});
//Error in montage (line 112)
//[I,cmap,mSize,indices,displayRange] = parse_inputs(varargin{:});
//Error in montage_pics (line 3)
//montage(fileNames, 'Size', [1 6]);
I am even uploading some of my images here:
As can be seen clearly, all the images are grayscale. I then read the image size and they are as follows:
1.128X128 2.128X128*3 3.128X128*3 4.128X128 5.128X128*3 6.128X128*3.
So some of the images are treated as indeed colour images.
My question is how to use the montage command for such images. Another problem is that montage command always needs images of similar sizes. So I wanted to avoid this loopholes.
Of course I could use a software tool to convert the images to the required format but that is a bad way to work. I believe the below code if added to my original code will solve this problem
%Read Each Image
I=imread('image');
I=imresize(I,[128 128]);
I=I(:,:,1);
%Apply montage command
However I have failed to integrate this code in my original code. Please help me to solve this problem. Thanks in advance guys for your valuable suggestions and help.
To montage you have to make sure
The size matches
The datatype matches
All images are grayscale (or all images are rgb, but do not mix)
.
images={'eight.tif','fabric.png','football.jpg'};
%intended size
ssize=128;
%preallocation
IALL=zeros(ssize,ssize,1,numel(images));
for idx=1:numel(images)
%get image, ensure double to avoid issues with different colour depths
I=im2double(imread(images{idx}));
%resize
I=imresize(I,[ssize,ssize]);
%if rgb, change to gray
if size(I,3)>1 %rgb image
I=rgb2gray(I);
end
%insert
IALL(:,:,:,idx)=I;
end
montage(IALL);

Quadtree Decomposition - MATLAB

How can I use the qtdecomp(Image,threshold) function in MATLAB to find a quadtree decomposition of an RGB image?
I tried this:
Ig = rgb2gray(I); % I is the rgb image
S = qtdecomp(I,.27);
but I get this error:
??? Error using ==> qtdecomp>ParseInputs
A must be two-dimensional
Error in ==> qtdecomp at 88
[A, func, params, minDim, maxDim] = ParseInputs(varargin{:});
Also I get this error:
??? Error using ==> qtdecomp>ParseInputs
Size of A is not a multiple of the maximum block dimension
Can someone tell me how can I do it?
One obvious error... In the code above you are still passing the original RGB image I to the QTDECOMP function. You have to pass Ig instead:
S = qtdecomp(Ig,.27);
There are two problem with the code in your original post:
1) The first error is because you need to supply Ig, the greyscale version of your image to the qtdecomp function, rather than the colour version I:
S = qtdecomp(Ig, .27);
2) The second error is because for the function qtdecomp, the image's size needs to be square and a power of 2. I suggest you resize the image in an image editor. For example, if your image is 1500x1300, you probably want to resize or crop it to 1024x1024, or perhaps 2048x2048.
You can find the size of the greyscale version of your image with this MATLAB command:
size(Ig)
To crop it to the 1024x1024 in the top-left corner, you can run this MATLAB command:
Ig = Ig(1:1024, 1:1024);