How can I get nested loops to work in Praat to detect onset in an entire directory? - nested-loops

This is my first question and I do my best to be clear. I have browsed the site without finding any former question that could help me out.
I am trying to get the onset detection script in praat to loop for an entire directory. I have nested the onset detection script as an inner loop to an outer loop going through each file in a specific library. However, I can't seem to make it work. I only get the onset for the first file in my directory.
The onset detection script works well on its own, and the outer loop works fine with other commands such as "get intensity" for example.
Can anybody see what I am doing wrong?
Here is what I have done:
form Get Intensity
sentence Directory .\
comment If you want to analyze all the files, leave this blank
word Base_file_name
comment The name of result file
text textfile intensity_VOT_list.txt
endform
#Print one set of headers
fileappend "'textfile$'" File name'tab$'
fileappend "'textfile$'" 'newline$'
Create Strings as file list... wavlist 'directory$'/'base_file_name$'*.wav
n = Get number of strings
for i from 1 to n
select Strings wavlist
filename$ = Get string... i
Read from file... 'directory$'/'filename$'
soundname$ = selected$ ("Sound")
To Intensity... 100 0
labelline$ = "'soundname$''tab$'"
fileappend "'textfile$'" 'labelline$'
select Intensity 'soundname$'
numberOfFrames = Get number of frames
fileappend "'textfile$'" 'numberOfFrames'
fileappend "'textfile$'" 'newline$'
for i from 1 to numberOfFrames
intensity = Get value in frame: i
if intensity > 40
time = Get time from frame: i
onsetresultline$ = "voice onset time for 'soundname$' is 'tab$''time''tab$'"
fileappend "'textfile$'" 'onsetresultline$'
fileappend "'textfile$'" 'newline$'
exit
endif
endfor
endfor
I would be happy for any help. If you read my question and feel it is badly formulated, please give me feedback on that so that I can try to get better.
Kindly

You were using the same control variable for each for loop, so it was getting overwritten each time. You also had an exit where you wanted your script to jump out of the second for loop. But the exit statement stops the whole script, not the loop. To implement something like last or break you can manually increase the control variable past its end value. This is an example:
form Get Intensity
sentence Directory .\
comment If you want to analyze all the files, leave this blank
word Base_file_name
comment The name of result file
text textfile intensity_VOT_list.txt
endform
#Print one set of headers
fileappend "'textfile$'" File name'tab$'
fileappend "'textfile$'" 'newline$'
strings_object = Create Strings as file list... wavlist 'directory$'/'base_file_name$'*.wav
n = Get number of strings
for i to n
select strings_object
filename$ = Get string... i
Read from file... 'directory$'/'filename$'
soundname$ = selected$ ("Sound")
intensity_object = To Intensity... 100 0
labelline$ = "'soundname$''tab$'"
fileappend "'textfile$'" 'labelline$'
select intensity_object
numberOfFrames = Get number of frames
fileappend "'textfile$'" 'numberOfFrames'
fileappend "'textfile$'" 'newline$'
for j to numberOfFrames ; Renamed your second i into j
intensity = Get value in frame: j
if intensity > 40
time = Get time from frame: j
onsetresultline$ = "voice onset time for 'soundname$' is 'tab$''time''tab$'"
fileappend "'textfile$'" 'onsetresultline$'
fileappend "'textfile$'" 'newline$'
j += numberOfFrames ; This will break out of the loop
endif
endfor
endfor

Related

Transferring a directory contains images in RGB to grayscale

Reading the content of the directory and for every JPEG image converting to grey scale
srcFiles = dir('R:\...\images - Copy\*.jpeg');
for i = 1 : length(srcFiles)
filename = srcFiles(i).name;
try
I = imread(filename);
catch ME
continue
end
IGrey = rgb2gray(I);
imshow(IGrey);
pathOfNewFile = strcat(pathOfGSFolder,filename,'jpeg');
imwrite(IGrey,pathOfNewFile,'jpeg');
end
'R:\...\images - Copy\' is not a valid path. A folder cannot be called ...
When trying to execute the first line you will probably get an error and the variable srcFiles will be empty, so the length of this variable will be 0 and therefore the loop will not execute.

How to save 3d arrays in Matlab that are generated in a loop?

I'm reading in 3d arrays, subtracting all of them from one of them, and trying to save the results as the same type of arrays. They are all equal sizes to each other, 1888x3520x6.
Here is the piece of code that I have:
[FileName,PathName,FilterIndex] = uigetfile('*.x3d*','MultiSelect','on');
numfiles = size(FileName,2);
FileName{1}
entirefile1 =fullfile(PathName,FileName{1})
Im1 = x3dread(entirefile1);
for j = 2:numfiles
FileName{j}
entirefile2 =fullfile(PathName,FileName{j})
Im2 = x3dread(entirefile2);
J = num2str(j);
strcat('ImDelta', J);
ImDelta = imsubtract(Im1, Im2);
end
I see that I'm creating a character sring by using strcat. But I'm not making it into a new file name. Only one file is resulting at the end of the loop.
(x3dread function is similar to "load" for working with images, only it is specifically written to handle the type of the 3dimention files that I have.)
Any help is appreciated. I'm just a beginner.

Issue with averaging images in matlab

when I try to average a folder of jpeg matlab images, all that I get is a blank image. I've gone over my code a million times, and I don't know where I'm
going wrong. (also I know I hard coded some of the numbers but that just because I wanted it to take a specific folder and I've double checked those a million times, they're right.)
%takes all the images in a folder and averages their values
%opens folder
function avg_image = average_images()
folder_name = uigetdir;
folder_directory = dir(folder_name);
filename = folder_directory(3).name;
len = length(folder_directory);
org_image = imread(filename);
sum_image = org_image;
%adds files together
for i = 4:len
filename = folder_directory(i).name;
org_image = imread(filename);
sum_image = sum_image + org_image;
end
%calculates average
avg_image = sum_image/(len-2);
%saves average as a fits file and displays it
imwrite(avg_image, 'averagefile.jpg');
read_image = imread('averagefile.jpg');
imshow(read_image)
end
The problem with your code is that you are reading in the JPGs as uint8 (default) and then doing math with the images as matrices of uint8's (0-255 integers). As you read in org_image, above and inside the for loop, cast the result as a double:org_image = double(imread(filename)). After you're done with the averaging, you need to cast it back, avg_image = uint8(sum_image/(len-2)).
When you do math with uint8's, divisions are messy since decimals are truncated. 4 divided by 8 when both are doubles gives you 0.5. When both are integers, you get 0.

MatLab (Image Processing, Image Acquisition) How to save captured images by webcam without overwriting the original file name?

I want to save images without overwriting them whenever I hit the pushbutton. Can you please help me how save images without overwriting the original? What I want to do is whenever I'll hit the pushbutton, It will generated 1 image at a time without deleting the original.
Just like in digital cameras, whenever I will hit the trigger button, it will save 1 image and the file name will be image1.jpg. So basically, if I will push trigger again, it will capture 1 image again and the file name will be image2.jpg and so on.
here is my code:
counter = 1; %initialize filename increment
vid = videoinput('winvideo',2);
set(vid, 'ReturnedColorSpace', 'RGB');
img = getsnapshot(vid);
imshow(img);
savename = strcat('C:\Users\Sony Vaio\Documents\Task\images\image_' ,num2str(counter), '.jpg'); %this is where and what your image will be saved
imwrite(img, savename);
counter = counter +1; %counter should increment each time you push the button
My code saves and keeps on overwriting the filename image1.jpg.
To make things clear
1 push to the pushbutton, 1 image saves.
it's like it will call the whole block code every hit at pushbutton.
I hope you guys can help me. I really troubled right now :(
Thank you :)
If this is the code that makes up the callback function for that pushbutton, then yes indeed, it will execute the entire block every time you push it.
If that is the case, you'll need to change it to this:
%// initialize filename increment
persistent counter;
if isempty(counter)
counter = 1; end
vid = videoinput('winvideo', 2);
set(vid, 'ReturnedColorSpace', 'RGB');
img = getsnapshot(vid);
imshow(img);
%// this is where and what your image will be saved
savename = [...
'C:\Users\Sony Vaio\Documents\Task\images\image_', ...
num2str(counter), '.jpg'];
imwrite(img, savename);
%// counter should increment each time you push the button
counter = counter + 1;
or, you could check what files are actually present, and use the next logical filename in the sequence:
vid = videoinput('winvideo', 2);
set(vid, 'ReturnedColorSpace', 'RGB');
img = getsnapshot(vid);
imshow(img);
%// this is where and what your image will be saved
counter = 1;
baseDir = 'C:\Users\Sony Vaio\Documents\Task\images\';
baseName = 'image_';
newName = [baseDir baseName num2str(counter) '.jpg'];
while exist(newName,'file')
counter = counter + 1;
newName = [baseDir baseName num2str(counter) '.jpg'];
end
imwrite(img, newName);
Every time you push that button the counter value resets to 1 because of the very first statement:
counter = 1
and hence the error.
counter = length(dir('*.jpg')) + 1; %Counts the number of .jpg files in the directory
That should do the job.
Zaher:
I'm online program about image processing and image acquisition from the camera in writing MATLAB.
When receiving the image every few seconds I get a picture of the camera.
Photos must be stored and processed in statistical process control charts.
When the first image after image acquisition program hangs and stops.
Please code to get images every 10 seconds online from cameras send images that can be used in statistical process control.
Thank

Making a gif from images

I have a load of data in 100 .sdf files (labelled 0000.sdf to 0099.sdf), each of which contain a still image, and I'm trying to produce a .gif from these images.
The code I use to plot the figure are (in the same directory as the sdf files):
q = GetDataSDF('0000.sdf');
imagesc(q.data');
I've attempted to write a for loop that would plot the figure and then save it with the same filename as the sdf file but to no avail, using:
for a = 1:100
q=GetDataSDF('0000.sdf');
fh = imagesc(q.dist_fn.x_px.Left.data');
frm = getframe( fh );
% save as png image
saveas(fh, 'current_frame_%02d.jpg');
end
EDIT: I received the following errors when trying to run this code:
Error using hg.image/get
The name 'Units' is not an accessible property for an instance of class 'image'.
Error in getframe>Local_getRectanglesOfInterest (line 138)
if ~strcmpi(get(h, 'Units'), 'Pixels')
Error in getframe (line 56)
[offsetRect, absoluteRect, figPos, figOuterPos] = ...
Error in loop_code (line 4)
frm = getframe( fh );
How do I save these files using a for loop, and how do I then use those files to produce a movie?
The reason for the error is that you pass an image handle to getframe, but this function excpects a figure handle.
Another problem is that you always load the same file, and that you saveas will not work for gifs. (For saving figures as static images, maybe print is the better option?)
I tried to modify my own gif-writing loop so that it works with your data. I'll try to be extra explicit in the comments, since you seem to be starting out. Remember, you can always use help name_of_command to display a short Matlab help.
% Define a variable that holds the frames per second your "movie" should have
gif_fps = 24;
% Define string variable that holds the filename of your movie
video_filename = 'video.gif';
% Create figure 1, store the handle in a variable, you'll need it later
fh = figure(1);
for a = 0:99
% Prepare file name so that you loop over the data
q = GetDataSDF(['00' num2str(a,'%02d') 'sdf']);
% Plot image
imagesc(q.dist_fn.x_px.Left.data');
% Force Matlab to actually do the plot (it sometimes gets lazy in loops)
drawnow;
% Take a "screenshot" of the figure fh
frame = getframe(fh);
% Turn screenshot into image
im = frame2im(frame);
% Turn image into indexed image (the gif format needs this)
[imind,cm] = rgb2ind(im,256);
% If first loop iteration: Create the file, else append to it
if a == 0;
imwrite(imind,cm,video_filename,'gif', 'Loopcount',inf);
else
imwrite(imind,cm,video_filename,'gif','WriteMode','append','DelayTime',1/gif_fps);
end
end
One more note: When the size of the data is the same for each plot, it makes sense to only use the plot(or in this case, imagesc) command once, and in later loop iterations replace it with a set(ah,'Ydata',new_y_data) (or in this case set(ah,'CData',q.dist_fn.x_px.Left.data'), where ah is a handle of the plot axes (not the plot figure!). This is orders of magnitude faster than creating a whole new plot in each loop iteration. The downside is that the scaling (here, the color-scaling) will be the same for each plot. But in every case that I have worked on so far, that was actually desirable.

Resources