Processing a stack of images in matlab - matlab

I am currently working on an image processing task that requires me to process a stack of .png images all at once in Matlab (I have very limited matlab knowledge). I have looked at various sites trying to figure out how to do this. My most recent attempt was based on the answer in this link: http://www.mathworks.com/matlabcentral/answers/7665-images-to-stacks, however I keep getting the error:
"Assignment has more non-singleton rhs dimensions than non-singleton subscripts"
My .png's are numbered sequentially (Heart 001.png, Heart 002.png,...) and my exact code is as follows:
I = zeros(240,320,253,'uint8');
for ii = 1:253
I(:,:,ii) = imread(sprintf('Heart %s.png'),num2str(ii,'%03i')));
end
Any help would be greatly appreciated!

Your image reading code looks fine, but the way you construct the file names is wrong. You are passing the result of num2str to imread as it's image format argument, but you intended to pass it to sprintf. How about you try imread(sprintf('Heart %03d.png', ii));?

I found the solution to be downloading imshow3D.m from http://www.mathworks.com/matlabcentral/fileexchange/41334-imshow3d--3d-imshow- ,
and then implementing the following code:
clear;
clc;
I = zeros(240,320,253,'uint8');
for k = 1:253
PNGFileName = strcat('Heart ',32, num2str(k), '.png');
imageData = imread(sprintf(PNGFileName));
Heart = imageData(:,:,1);
I(:,:,k) = Heart;
end
imshow3D(I)

Related

MATLAB - Regarding int2int LWT2 and ILWT

I write following code in MATLAB to apply int2int lifting wavelet transform to the lena.jpg image.
I=imread('lena.jpg');
im=double(I);
lshaarInt = liftwave('haar','int2int');
els = {'p',[-0.125 0.125],0};
lsnewInt = addlift(lshaarInt,els);
[LL1,HL1,LH1,HH1] = lwt2(im,lsnewInt)
LL1=uint8(LL1);LH1=uint8(LH1);HL1=uint8(HL1);HH1=uint8(HH1);
figure()
subplot(2,2,1);imshow(LL1);
subplot(2,2,2);imshow(LH1);
subplot(2,2,3);imshow(HL1);
subplot(2,2,4);imshow(HH1);
Now I apply inverse LWT using following code without applying any other operations on sub-bands:
LL1=double(LL1);LH1=double(LH1);HL1=double(HL1);HH1=double(HH1);
wmd1= (ilwt2(LL1,HL1,LH1,HH1,lsnewInt));
wmd=uint8(wmd1);
I compare two images 'I' and 'wmd' using Normalized Co-relation Coefficient (NCC)
In output, in place of 1 as a answer I got 0.9994.
When I compare the intensity values of two images then the values are not same.
LWT is the lossless data technique but i found that the data loss.
Please suggest me where I am wrong.
I found the solution
The problem is with following code:
LL1=uint8(LL1);LH1=uint8(LH1);HL1=uint8(HL1);HH1=uint8(HH1);
No need to convert all the above values from double to int.
I got NCC value as 1 when I gave comment to above statement.
Thank you for response.

Big tiff read and view in Matlab

I have downloaded a btf file (big tiff) from the links below, how can I read it and "imshow" it? is there a way to convert it to tiff format as the btf is not that common?
Link:
https://drive.google.com/file/d/0ByhuP_NuuARtSW9aeTdPUTlRdWM/view?usp=drive_web
http://www.photomacrography.net/forum/viewtopic.php?t=28990&sid=cca737a2e0bc7ea3e2e41f0d6e75f5a9
I used this code:
t = Tiff('d:/Image_687.btf','w8');
imageData = read(t);
and got this error:
Error using tifflib
Unable to retrieve PhotometricInterpretation.
Error in Tiff/getTag (line 838)
tagValue = tifflib('getField',obj.FileID,Tiff.TagID.(tagId));
Error in Tiff/read (line 1487)
photo = obj.getTag('Photometric');
Error in Untitled2 (line 2)
imageData = read(t);
The real issue with your code is the second parameter that you have passed to Tiff. As the documentation states, the second parameter indicates in what mode to open the file. You have specified w8 which the documentation states is:
open TIFF file for writing a BigTIFF file; discard existing contents.
This means that it is deleting your image before you even start! If you want to use the Tiff class, you'll want to either use no second parameter or the r parameter to open the file for reading.
t = Tiff('Image_687.btf');
t = Tiff('Image_687.btf', 'r');
That being said, in general it is better to try to load it with a higher level function such as imread. The Tiff class is a much lower-level function that can be a little harder to manipulate but may provide some needed specialty functionality.
im = imread('Image_687.btf');
size(im)
3072 4080 3
I had to do a little manipulation for display because the RGB values weren't between 0 and 255
im = double(im);
im = uint8(255 * im ./ max(im(:)));
imshow(im);

MATLAB loading and saving a single image from a 32 bit tiff stack

I'm using MATLAB_R2011a_student. I have some image stacks saved as 32 bit tiffs, some over 1000 frames. I would like to be able to pull out a specific frame from the stack and save it as a 32 bit tiff or some readable format where there would be no data loss from the original. Currently my code looks like this:
clear, clc;
k=163;
image=('/Users/me/Filename.tiff');
A = uint8(imread(image, k));
B=A(:,:,1);
J=imadjust(B,stretchlim(B),[]);
imwrite(J,'/Users/me/163.tif','tif');
(I'm assuming reading it as 8 bit, and the way I'm saving are not the best way to do this)
Either way this code works for a seemingly random number of frames (for example in one file.tiff the above code works for frames 1-165 but none of the frames after 165, for a different file.tiff the code works for frames 1-8 but none of the frames after 8) I'm also getting a strange horizontal line in the output image when this does work:
??? Error using ==> rtifc
Invalid TIFF image index specified.
Error in ==> readtif at 52
[X, map, details] = rtifc(args);
Error in ==> imread at 443
[X, map] = feval(fmt_s.read, filename, extraArgs{:});
Thanks!
The best way (in my opinion) to handle tiff stacks is to use the Tiff library available since a few years. I must admit that I don't know much about OOP but I managed to understand enough to load a tiff stack and manipulate it.That's the kind of simple demo I wish I had seen a year ago haha.
I the following example I load a single stack and store it all into a 3D array. I use imfinfo to fetch infos about the images, notably the number of images/stack and the actual image dimensions. If you want you can choose to load only one image using appropriate indices. Please try the code below and play around with it; you'll understand what I mean.
clear
clc
%// Get tiff files you wish to open
hFiles = dir('*.tif');
%// Here I only have 1 multi-tiff file containing 30 images. Hence hInfo is a 30x1 structure.
hInfo = imfinfo(hFiles(1).name);
%// Set parameters.
ImageHeight = hInfo(1).Height;
ImageWidth = hInfo(1).Width;
SliceNumber = numel(hInfo);
%// Open Tiff object
Stack_TiffObject = Tiff(hFiles.name,'r');
%// Initialize array containing your images.
ImageMatrix = zeros(ImageHeight,ImageWidth,SliceNumber,'uint32');
for k = 1:SliceNumber
%// Loop through each image
Stack_TiffObject.setDirectory(k)
%// Put it in the array
ImageMatrix(:,:,k) = Stack_TiffObject.read();
end
%// Close the Tiff object
Stack_TiffObject.close
Hope that helps.

SSIM Coding Error

I have some questions. I try to follow some coding from Mathworks:
I = imread('cameraman.tif');
ssimValues = zeros(1,10);
qualityFactor = 10:10:100;
for i = 1:10
imwrite(I,'compressedImage.jpg','jpg','quality',qualityFactor(i));
ssimValues(i) = ssim(imread('compressedImage.jpg'),I);
end
I just change the image file which is a.jpg and b.jpg but I get this error from MATLAB:
Undefined function 'ssim' for input arguments of type 'uint8'
Error in SSIMTesting (line 6)
ssimValues(i) = ssim(imread('logohalal1.jpg'),i);
Why is that ? Can someone help me explain the code and the error ? Sorry because I'm new in MATLAB.
Thank you.
MATLAB release notes for the Image Processing Toolbox shows that this function was new to R2014a. If you have an older version of MATLAB, or you don't have that toolbox, you don't have it. This sort of issue can be avoided by using only examples found in the help on your local installation of MATLAB rather than the online help.
To check your version of MATLAB and installed toolboxes, type ver at the command line.
To check if a function can be found on your MATLAB path, you can use which, e.g. which ssim

Matlab integral function & function handles in a loop

'Outliers.m' is called from a higher level .m file. The variables are all defined in the higher level file, and set as globals for access by Outliers.m. The purpose of the code is to identify outliers using Chauvenets Criterion, and for this, I have to calculate the integral of the guassian distribution, using the Integral function and function handles. The code works and gives sensible values when I enter specific variables as a test, but I cannot get it to work in a loop. My data set is comprised of 7 individual samples, each 1x30, all of which need to be analyzed. I have had various errors, read through the guidance on Integral and function handles, but cannot seem to find the solution...Any help or guidance would be very much appreciated.... Here is my code:
n = 7
for x = 1:n
for y = 1:30
z(x,y) = abs((cc(x,y) - mastercc(1,y))/masterccstd(1,y));
xmax(x,y) = mastercc(1,y)+z(x,y)*masterccstd(1,y);
xmin(x,y) = mastercc(1,y)-z(x,y)*masterccstd(1,y);
p(x,y) = 1/(masterccstd(1,y)*(sqrt(2*pi)));
fun(x,y)= #(x,y,z) (exp(-1/2)*z(x,y).^2);
q(x,y) = integral(fun(x,y),xmin(x,y),xmax(x,y),'ArrayValued',true);
pq(x,y) = p(x,y)*q(x,y); % probability
value(x,y) = n*(1/pq(x,y));
count(x,y) = logical(value(x,y) <0.5);
badbins(x)=sum(count(x,:));
end
end
It seems like your error is caused by an invald function definition.
If you try it like this it should work:
fun = #(x,y,z) (exp(-1/2)*z(x,y).^2)
Now it can be called like this for example:
fun(1,2,magic(4))
Solution to the loop problem, courtesy Andrei Bobrov via Matlab Central, link below:
http://www.mathworks.com/matlabcentral/answers/103958#comment_177000
NB: Please note the code is not complete for the purpose I explained in the problem description, but it does solve the Loop error.