Montage of grayscale and colour images - matlab

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);

Related

How to generate proper labelled image from MATLAB (Image Labeler) for image segmentation

I get black image whenever exporting labels to file in the Image Labeler App in MATLAB R2019a
Here is what i do:
Export Labels > To File
Also, I know that PNG file's image value is composed as 0 1 2 and this is the reason image apperas black.
I tried this piece of code and I get the result with distingished but the dimensions of the image are changed.
figure;
[i, m] = imread('PixelLabelData/Label_1.png');
imshow(i,m);
I need is Labelled png image with proper dimensions. How can I do this ?
I am using https://supervise.ly instead of MATLAB. Development is much easier and faster.

Convolving an image with a mask displays an all white image

Very new to MatLab, just figuring some things out and had a question. I am basically trying to filter/blur an image using conv2() but I am getting an all white image when I am using imshow()
I am reading the image in with
testImage = imread('test.bmp');
This is a uint8 image of a grayscale.
I am trying to convolve the image with a 4 x 4 matrix.
fourByFour = ones(4);
When I actually execute, I am getting all white with imshow()
convolvedImage = conv2(testImage, fourByFour);
I should expect a filter placed on the image, not an entirely white one.
Any help would be appreciated.
I don't have your test image so I explain on an image. As the definition of conv2 it returns the two-dimensional convolution.
So please look at this little code:
clc;% clear the screen
clear all;% clear everything
close all;% close all figures
test = imread('test.bmp');
% read test image that is .bmp format and size :294x294x3 and uint8.
fourByFour = ones(4); % define 4 * 4 matrix all ones
convolvedImage = conv2(test(:,:,1), fourByFour);
%apply the ones matrix on image but to use conv2 on this image we apply on one of channels
figure, imshow(convolvedImage,[])
This is my command window, out put:
I'm using MAtlab 2017a, and if I use conv2(test, fourByFour); instead of conv2(test(:,:,1), fourByFour); ,the error is :
Error using conv2
N-D arrays are not supported.
So we should attention to class type and dimensions. And one more thing, in your command window please type edit conv2 you can read the details of this function and how to use it, but never edit it:). Thanks
test = imread('test.bmp');
% read test image that is .bmp format and size :294x294x3 and uint8.
fourByFour = ones(4);
% define 4 * 4 matrix all ones
test=rgb2gray(test);
% convert colorimage into gray
convolvedImage = conv2(double(test), double(fourByFour));
%perform the convolution
figure, imshow(convolvedImage,[])
% display the image

showing dicom files in matlab

I want to show a stack of dicom files. I already have the code that loads the images, but i get an warning saying
Warning: Image is too big to fit on screen; displaying at 67%
> In imuitools\private\initSize at 71
In imshow at 282
In montage at 147
In dicom at 11
If you know how to fix this please let me know.
Here is my code:
% Preallocate the 256-by-256-by-1-by-20 image array.
X = repmat(uint16(0), [256 256 1 20]);
% Read the series of images.
for p=1:20
filename = sprintf('brain_%03d.dcm', p);
X(:,:,1,p) = dicomread(filename);
end
% Display the image stack.
montage(X,[])
The imshow function is very limited in its ability to rescale images, and suffers from some aliasing problems. To work around this, you can rescale your images using imresize prior to the call to imshow.
I had a lot of difficulty with imshow and the underlying image function, so I wrote a wrapper class that dynamically resizes images to fit the figure window. It is called RViewer, and it is a drop in replacement for image: http://www.mathworks.com/matlabcentral/fileexchange/46051-rviewer

Matlab Imread resizes tif file

so I'm using the imread function in matlab and when I save the TIFF file and open it in photoshop, it has a white border and I can't understand why. I want to maintain its resolution as a 512 by 512 image. Any ideas why? And how I can fix that?
Here's a sample code:
B = imread('W_noise1.tif');
for n = 1:5,
B = medfilt2(B);
end
B = filter2(fspecial('average',3),B)/255;
imshow(B)
Are you sure it's an issue with imread? I'd be surprised if it is.
See this link about medfilt2 where it explains that "medfilt2 pads the image with 0s on the edges, so the median values for the points within [m n]/2 of the edges might appear distorted."
EDIT: I tried to replicate your problem. This is an issue with print where it puts a white frame around the image after you save it. This functionality, print is made for printing plots. If you want to save the image, you should use imwrite.

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);