Creating a stacked tiff file causes image offset - matlab

I have a an image processing program whose results need to be saved to a stacked tiff file. The code for the method that does this is included below. When the resulting image is opened in ImageJ it gives the notification "Unexpected image offset" and only can display the first image in the stack.
fig = figure;
figure(fig);
fileN = handles.fileName;
[m n] = size(fileN);
m = 0;
for i=1:n
if(fileN(i) == '.')
m = i;
break
end
end
fileN = fileN(:,1:m-1);
fileP = handles.filePath;
saveFilePath = strcat(fileP,fileN,'-snake-analysis.tif')
im = imread(handles.fileFull, 1);
imshow(im, []);
hold on
plot(handles.xsFinal(1,:), handles.ysFinal(1,:), 'r-');
hold off
saveas(fig, saveFilePath);
for i=2:handles.numFrames
im = imread(handles.fileFull, i);
imshow(im, []);
hold on
plot(handles.xsFinal(i,:), handles.ysFinal(i,:), 'r-');
hold off
saveas(fig, 'inter.tif');
a = imread('inter.tif');
imwrite(a, saveFilePath,'Compression', 'none','WriteMode','append');
delete 'inter.tif';
end
Does anyone know what is causing this problem?

The built-in ImageJ 1.x TIFF opener does not fully handle the baseline TIFF specification: it cannot handle out-of-sequence planes.
If you use ImageJ2, you can use the SCIFIO library to open your TIFF via the File ▶ Import ▶ Image... command. The Bio-Formats Importer plugin can also open such TIFF files.

Related

MATLAB code for combining .fig files from a folder to generate a video/ animation?

I am trying to make an animation from a sequence of figures (.fig) files of MATLAB from a folder in my system. I referred to the questions here and here.
Finally, I have the code mkvideo.m :
% Creating Video Writer Object
writerObj = VideoWriter('peqr.avi');
% Using the 'Open` method to open the file
open(writerObj);
% Creating a figure.
% Each frame will be a figure data
Z = peqr;
surf(Z);
axis tight
set(gca,'nextplot','replacechildren');
set(gcf,'Renderer','zbuffer');
[figures,var] = uigetfile('*.fig','Multiselect','on');
for k = 1:length(figures)
Multi_Figs = [var,filesep,figures{k}];
Op = openfig(Multi_Figs);
% Frame includes image data
frame = getframe;
% Adding the frame to the video object using the 'writeVideo' method
writeVideo(writerObj,frame);
close(Op);
end
% Closing the file and the object using the 'Close' method
close(writerObj);
I am getting the following error
Warning: No video frames were written to this file. The file may be invalid.
> In VideoWriter/close (line 278)
In VideoWriter/delete (line 213)
In mkvideo (line 2)
Undefined function or variable 'peqr'.
Error in mkvideo (line 8)
Z = peqr;
I think this code should be able to produce a video once in user interface, I select the files?
It would be helpful if I could get some help to rectify the error in this code so that the animation or video making is possible.

File Importing Sequence changed using parfor (different results using parfor and for)

I have an issue with the file importing when converting my for loop into parfor and would appreciate any help I can get.
The structure of my code is as following:
myFolder = '../Test';
if option1 == 1
ext = 'EW';
filePattern1 = fullfile(myFolder, '*.EW1'); % input motion
datFiles1 = dir(filePattern1);
filePattern2 = fullfile(myFolder, '*.EW2'); % surface motion
datFiles2 = dir(filePattern2);
elseif option1 == 2
ext = 'NS';
filePattern1 = fullfile(myFolder, '*.NS1'); % input motion
datFiles1 = dir(filePattern1);
filePattern2 = fullfile(myFolder, '*.NS2'); % surface motion
datFiles2 = dir(filePattern2);
end
parfor k = 1:length(datFiles1)
baseFileName1 = datFiles1(k).name;
baseFileName2 = datFiles2(k).name;
fullFileName1 = fullfile(myFolder, baseFileName1);
fullFileName2 = fullfile(myFolder, baseFileName2);
%display file importing sequence
fprintf(1, 'Now processing %s\n', baseFileName1);
record1 = load(baseFileName1);
fprintf(1, 'Now processing %s\n', baseFileName2);
record2 = load(baseFileName2);
npts2 = length(record2(:,1));
%this is the end of file importing part and the following is to process the imported data, and as that is not relevant to this issue, I didn’t show them.
end
The datFiles1 and datFiles2 look like this:
And datFiles2 goes with the same sequence except with EW2 extensions.
Then it should display as in Filename1.EW1, Filename1.EW2, Filename2.EW1, Filename2.EW2, Filename3.EW1, Filename3.EW2, Filename4.EW1, Filename4.EW2 …
As like this: (if I only use for loop)
However, if I use parfor, the sequence is messed up as it seems like it randomly imports some file and then follows the sequence again
[enter image description here][3]
The first 4 lines are right. Then the 6th line starts to show an error when it should have been MYGH050008281720.EW2
The key is to process each pair of EW1, EW2 files(with same filenames) in a loop. Is there any other way to import files in parfor? Thanks!

Matlab Code to Convert Video to Sequence of Frames

hi can someone tell me whats wrong with my Matlab Code to Convert Video to Sequence of Frames . it doesn't work.
clc
clear all
close all
file=aviinfo('test.avi');
frm_cnt=file.NumFrames
FileExtension='.bmp'
h = waitbar(0,'Please wait');
for i=1:frm_cnt
% Read the Frame of the Video file
frm(i)=VIDEOREADER ('test1.avi',i);
% Convert Frame to image file
frm_name=frame2im(frm(i));
%Create Filename to store
Filename=strcat(strcat(num2str(i)),FileExtension);
% Write image file to the current folder
imwrite(frm_name,Filename);
waitbar(i/frm_cnt,h)
end
%Close Progress bar
close(h)
Try this :
shuttleVideo = VideoReader('shuttle.avi');
ii = 1;
while hasFrame(shuttleVideo)
img = readFrame(shuttleVideo);
filename = [sprintf('%03d',ii) '.jpg'];
fullname = fullfile(workingDir,'images',filename);
imwrite(img,fullname) % Write out to a JPEG file (img1.jpg, img2.jpg, etc.)
ii = ii+1;
end

Embedding data from a text file into DWT subband using steganography

I am attempting to embed data from a text file(which contains only numeric data) into LL subband of an image using a steganography. I am getting an error "Error using bitset ASSUMEDTYPE must be an integer type name" in the line of code:
L(ii,jj)=bitset(L(ii,jj),1,stego(ii,jj));
I have attempted to run in debugger but I am having no luck. I think it must be something to do with the data type of L?? I have tried changing image to binary,double etc but I still get this error! Please can someone give me some advice on where I am going wrong?I have a insert my code below
% loading cover image
img=imread('lena.bmp');
image=im2double(img);
% get DWT of image
[LL,LH,HL,HH] = dwt2(image,'haar');
A = importdata('minutiaTest.txt');
I = dec2bin(A,8);
L=LL;
% determine size of LL subband
Mc=size(L,1); %Height
Nc=size(L,2); %Width
% determine size of message object
Mm=size(I,1); %Height
Nm=size(I,2); %Width
for ii = 1:Mc
for jj = 1:Nc
stego(ii,jj)=I(mod(ii,Mm)+1,mod(jj,Nm)+1);
end
end
for ii = 1:Mc
for jj = 1:Nc
L(ii,jj)=bitset(L(ii,jj),1,stego(ii,jj));
end
end
stego_image = idwt2(LL,LH,HL,HH,'haar');
imwrite(uint8(stego_image),'stego.bmp');
figure; imshow(stego_image,title('Stego image'));

reading and displaying video file frame by frame

I am newly working with Matlab. I want to read a video file and do some calculations every frame and display every frame. I wrote the following code but every time it only displays the first frame. can anybody please help.
mov=VideoReader('c:\vid\Akiyo.mp4');
nFrames=mov.NumberOfFrames;
for i=1:nFrames
videoFrame=read(mov,i);
imshow(videoFrame);
end
Note: mmreader API has been discontinued by MATLAB so prefer using VideoReader.
See comment by #Vivek.
I usually do this:
obj=mmreader('c:\vid\Akiyo.mp4');
nFrames=obj.NumberOfFrames;
for k=1:nFrames
img=read(obj,k);
figure(1),imshow(img,[]);
end
As far as your code is concerned, I saw MATLAB's documentation. You should do the things in following order:
mov=VideoReader('c:\vid\Akiyo.mp4');
vidFrames=read(mov);
nFrames=mov.NumberOfFrames;
for i=1:nFrames
imshow(vidFrames(:,:,i),[]); %frames are grayscale
end
Function read() and the field NumberOfFrames() are now deprecated, Matlab suggests using
xyloObj = VideoReader(file);
vidHeight = xyloObj.Height;
vidWidth = xyloObj.Width;
mov = struct('cdata',zeros(vidHeight, vidWidth, 3,'uint8'), 'colormap',[]);
while hasFrame(xyloObj)
mov(k).cdata = readFrame(xyloObj,'native');
end
In case you want to estimate a number of frames in the video, use nFrames = floor(xyloObj.Duration) * floor(xyloObj.FrameRate);
Below suggested code is showing only one frame
imshow(vidFrames(:,:,i),[]);
I am doing following things to store each frame
obj = VideoReader('path/to/video/file');
for img = 1:obj.NumberOfFrames;
filename = strcat('frame',num2str(img),'.jpg');
b = read(obj,img);
imwrite(b,filename);
end
This will store all the frames in your home directory.And yes, as also suggested by Vivek and Parag
You need to use VideoReader as mmreader has been discontinued by
MATLAB.
*=I was making a function to play any .avi file as a set of frames in a figure. Here's what a did. A bit of a combo of what you've done, except my NumberOfFrames wasn't working: (noteL this also shows it in colour)
function play_video(filename)
% play_video Play a video file
% play_video(filename) plays the video file specified by filename in a MATLAB Figure window.
figure
set(figure, 'Visible', 'on')
mov=VideoReader(filename);
vidFrames=read(mov);
duration = mov.Duration;
frame_rate = mov.FrameRate;
total_frames = duration .* frame_rate
for i=1:1:total_frames
imshow(vidFrames(:, :, :, i), []);
drawnow
end