Converting tiff images to avi in matlab - matlab

I'm trying to use VideoWriter to convert large amounts of tiff images into an avi video. I'm having trouble with the for loop to convert the images into a video. Here is the code I have so far:
function tif2avi
clc; close all;
[imagelist,p]=uigetfile('*.tif','MultiSelect','on',...
'Select LIST to plot'); pause(0.5); cd(p);
if ~iscell(imagelist); disp('imagelist not cell'); return; end;
outputVideo = VideoWriter('0424_rat01.avi');
outputVideo.FrameRate = 16;
outputVideo.Quality = 100;
open(outputVideo);
for i=1:numel(imagelist)
img=imread(imagelist(i));
writeVideo(outputVideo,img);
end
Can anyone help me out? I think the problem is in my for loop.
This is the error I get when I try to run the code:
Warning: No video frames were written to this file. The file may be invalid.
> In VideoWriter.VideoWriter>VideoWriter.close at 289
In VideoWriter.VideoWriter>VideoWriter.delete at 238
Error using imread>parse_inputs (line 476)
The filename or url argument must be a string.
Error in imread (line 335)
[filename, fmt_s, extraArgs] = parse_inputs(varargin{:});
Error in tif2avi (line 14)
img=imread(imagelist(i));

Your imagelist is a cell array and therefore should be accessed using curly braces (instead of regular parentheses).
Replace
img = imread( imagelist(i) );
with
img = imread( imagelist{i} );
and see what happens.
PS,
It is best not to use i as a variable name in Matlab.

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.

Dimensions are not Ok in matlab

When I take image from img from commands =
imread('C:\Users\Administrator\Desktop\Detect\F04U2MPIXQG15WQS.LARGE.jpg');
figure,imshow(img)
It works fine but when I use
[filename, pathname] = ...
uigetfile({'*.jpg';'*.jpeg';'*.png';'*.*'},'Select Image File');
img=strcat(pathname,filename);
error shown is Error in skinDetect2Func (line 8)
yuv(:,:,y) = (img(:,:,r)+2.*img(:,:,g)+img(:,:,b))/4; Error in
bwFingers1 (line 39)
out=skinDetect2Func(img);
Anyone can help me out I am just stuck in here. :(
Here is the complete code:
function out=skinDetect2func(img)
imshow(img);
sz=size(img); r=1;g=2;b=3;y=1;u=2;v=3;
yuv=img;
region=yuv;
yuv(:,:,y) = (img(:,:,r)+2.*img(:,:,g)+img(:,:,b))/4;
yuv(:,:,u) = img(:,:,r)-img(:,:,g);
yuv(:,:,v)=img(:,:,b)-img(:,:,g);
region = (yuv(:,:,u)>20 & yuv(:,:,v)<74) .* 255;
toc;
out=region;
%filtering
out=im2bw(out); out=bwareaopen(out,100);
out=imdilate(out,strel('diamond',4));
%retain largest only
res=out;
cc=bwconncomp(res);
arr=(cellfun('length',cc.PixelIdxList));
newLabel=res;
if ~isempty(round(arr))
msz=0;
for i=1:length(arr)
if msz<arr(i:i)
msz=arr(i:i);
index=i;
end
end
labels=labelmatrix(cc);
newLabel=(labels==index);
out=newLabel;
end
out=imfill(out,'holes');
end***
ehm,
This is a string:
img=strcat(pathname,filename); % STRing conCATenation
You still need to imread...
The correct code would be
imgpath=strcat(pathname,filename);
img=imread(imgpath);

Series of dicom images into specific format in matlab

I have series of matlab images which belong to a single patient. I found some code online, but it is sowing some error. I want something like this, Image
Here is the code I have.
% Preallocate the 256-by-256-by-1-by-20 image array.
X = repmat(int16(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,[])
I found this code from here:
https://www.mathworks.com/company/newsletters/articles/accessing-data-in-dicom-files.html
Error using montage>validateColormapSyntax (line 339)
An indexed image can be uint8, uint16, double, single, or logical.
Error in montage>parse_inputs (line 259)
cmap = validateColormapSyntax(I,varargin{2});
Error in montage (line 114)
[I,cmap,mSize,indices,displayRange,parent] = parse_inputs(varargin{:});
Error in Untitled2 (line 9)
montage(X,[]);
The syntax for calling the montage function has changed since that code sample was written (back in 2002!). As noted in the comments section of the File Exchange submission for the sample DICOM data files, the new correct syntax is this:
montage(X, 'DisplayRange', []);
You were getting that error since the new syntax interprets montage(X, []); as if X were an indexed color image (which isn't allowed to be a signed int16 type, according to the error) with an empty color map [].

Convert video into greyscale in matlab

I'm trying to convert a video file into greyscale. When I try running the Matlab script I get a "Invalid video data-must be a numeric or logical data type". Can someone help me with what I am doing wrong? I am also new to matlab.
filename = 'Project1.m4v';
vid = VideoReader(filename);
newVid = VideoWriter('NewVid');
open(newVid);
numFrames = vid.NumberOfFrames;
for frame = 1 : numFrames
% Extract the frame from the movie structure.
thisFrame = read(vid, frame);
%Convert each frame to black and white
gray = rgb2gray(thisFrame);
writeVideo(newVid,gray);
end
close(newVid);
implay(newVid);
Use implay('NewVid.avi') instead of implay(newVid);
The only problem in your code is the last line: implay(newVid);.
newVid is a VideoWriter object - you create it using newVid = VideoWriter('NewVid');.
I recommend you to add '.avi' file extension to 'NewVid' file name:
Use: newVid = VideoWriter('NewVid.avi');
implay does't take VideoWriter object as input parameter.
Instead of displaying an error message in Matlab workspace, it displays the error message in the video window.
All you need to do, is replacing last code line with implay('NewVid.avi').

Matlab error when playing a sound

I'm using matlab gui, I'm recording a sound then save it in a folder in c, then display the recorded sound that are in the folder in a listbox when I press play on the sound wav. Matlab gives an error the error is :
************Error using audioread (line 74)**
***The filename specified was not found in the MATLAB path.
Error in Monitoring_System>play_Callback (line 178)
[q, Fs] = audioread(thisstring);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in Monitoring_System (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in #(hObject,eventdata)Monitoring_System('play_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback*************
-The record code:
format shortg
c = clock;
fix(c);
a=num2str(c);
year=strcat(a(1),a(2),a(3),a(4),a(5));
month=strcat(a(19),a(20));
day=strcat(a(33),a(34));
hour=strcat(a(48),a(49));
min=strcat(a(63),a(64));
sec=strcat(a(74),a(75));
name=strcat(year,'-',month,'-',day,'-',hour,'-',min,'-',sec);
fullpath=fullfile('c:\monitoringsystem',name);
wavwrite(y,44100,fullpath);
y=[];
The code to diplay them in the listbox:
d = dir('C:\monitoringsystem\*.wav'); %get files
set(handles.listbox1,'String',{d.name})
The code to play the sound that is chosen from the listbox:
allstrings = cellstr( get(handles.listbox1, 'String') );
curvalue = get(handles.listbox1, 'Value');
thisstring = allstrings{curvalue};
[q, Fs] = audioread(thisstring);
soundsc(q,44100);
Any help how to solve this problem, with keeping to save in a specific folder. I copied the recorded sound to the matlab folder and then pressed play in the gui for that wav sound it didin't give any error.
Did you try debugging this and seeing what d contains after selecting a file?
As per the documentation, d = dir('C:\monitoringsystem\*.wav'); returns a struct with the following fields: name, date,bytes, isdir, datenum (at least on MATLAB 2015a). Although {d.name} gives you the filename correctly, you should note that this is a relative path only, so MATLAB doesn't where to look for the file unless it's in the active directory.
I'm not entirely sure why you went through all the trouble with allstrings, curvalue, and thisstring, but if I understand correctly what you're trying to do, I would suggest one of two approaches:
Define the default path (i.e. C:\monitoringsystem) in a constant, and then use it when saving\loading:
DEFAULT_PATH = `C:\monitoringsystem`; %// Definition
...
fullpath = fullfile(DEFAULT_PATH,name); %// When saving
...
d = dir(fullfile(DEFAULT_PATH,'*.wav')); %// When listing files
...
[q, Fs] = audioread(fullfile(DEFAULT_PATH,{d.name})); %// When reading a file
Using uigetfile:
[FileName,PathName] = uigetfile('*.wav','Select the WAV file');
FullPath = fullfile(PathName,FileName);
(and then the rest is very similar to the 1st case)