I want to generate image name to read it later in the matlab code. I have name of the image in the work space as:
ImageName =
'OECA 2682_001_001'
I have done following to generate name and read image:
ext_img='*.jpg';
varname = genvarname(ImageName,ext_img)
image = imread('varname');
But varname is coming out to be :
varname =
x0x27OECA2682_001_0010x27
instead of OECA 2682_001_001.jpg. I want to read image as:
image=imread('OECA 2682_001_001.jpg');
How can I generate name of image with jpg extention to read image. Please help!
Thanks
That's not how genvarname works. You want to do:
varname = [genvarname(ImageName) '.jpg']
but I don't think you need genvarname at all. It's used for making a variable name, but you are not using it like that.
Also, instead of using
image = imread('varname');
you want
image = imread(varname);
Related
I am working on a GUI and I have a file named 'work.mid'. The user can make some modifications to it and when they click the save button I want it to be saved as 'work1.mid' to 'c:\saved_datas\'. When they click that button second time, it should save it as 'work2.mid', on the third time 'work3.mid' and so on. Here's the code I have so far:
nmat = readmidi_java('work.mid');
Name = fullfile('c:\saved_datas\', '?????');
writemidi_java(nmat, Name);
Figuring out what should go at ????? is where I'm stuck.
The following code would work if you have no prior work*.mid or if you have any number of sequential work*.mid files inside c:\saved_datas\. Now, if the files are not in sequence, this code could be tweaked for that, just let me know if you would like to handle that case too.
Code listed here -
%// Parameters
org_filename = 'work.mid';
main_dir = 'c:\saved_datas\'; %//'
%// Your code
nmat = readmidi_java(org_filename);
%// Added code
[~,filename_noext,ext] = fileparts(org_filename)
filenames = ls(strcat(main_dir,filename_noext,'*',ext))
new_filename = strcat(filename_noext,num2str(size(filenames,1)+1),ext)
Name = fullfile(main_dir,new_filename)
%// Your code
writemidi_java(nmat, Name);
For achieving uniqueness of filenames, some also use timestamps. This could be implemented like this -
org_filename = 'work.mid'; %//'
main_dir = 'c:\saved_datas\'; %//'
[~,filename_noext,ext] = fileparts(org_filename)
new_filename = strcat('filename_noext','-',datestr(clock,'yyyy-mm-dd-hh-MM-SS'),ext)
Name = fullfile(main_dir,new_filename);
This could be done a couple of ways depending on how you have structured your GUI. You need to keep track of how many times the button has been pressed. In the callback for the button you could use a persistent variable ('count') and increment it by one at the start of the function. Then construct the filename with filename = ['work' num2str(count) '.mid']. Alternatively you could increment a class member variable if you have implemented your GUI using OOP.
To save the file use the 'save()' function with the previously constructed file name and a reference to the variable.
Check out the documentation for persistent variables, save, fullfile and uiputfile for extra info.
I'm building a facial recognition program and loading a whole bunch of images which will be used for training.
Currently, I'm reading my images in using double loops, iterating through subfolders in a folder.
Is there any way, as it iterates, that the images file name can be used before the image is read and stored?
eg. I have an image person001.jpg. How can you retrieve that name (person001) then read the image in like: person001 = imread('next iteration of loop which happens to be person001');
Thanks in advance.
I strongly recommend not to use unstructured variables. First it's very difficult to do operations like "iterate over all images", second you can get strange problems covering a function name with a variable name. Instead i would use a struct with dynamic field names or a map. A solution with a Map propably allows all possible file names.
Dynamic field names:
dirlisting=dir('.jpg');
for imageIX=1:numel(dirlisting)
%cut of extension:
[~,name,~]=fileparts(dirlisting(imageIX).name);
allImages.(name)=imread(dirlisting(imageIX).name);
end
You can access the images in a struct with allImages.person001 or allImages.(x)
Map:
allImages=containers.Map
dirlisting=dir('.jpg');
for imageIX=1:numel(dirlisting)
%cut of extension:
[~,name,~]=fileparts(dirlisting(imageIX).name);
allImages(name)=imread(dirlisting(imageIX).name);
end
You can access the images in a Map using allImages('person001'). Using a Map there is no need to cut of the file extension.
I am having quite a bit of trouble updating a string using a push button in my created matlab GUI. The goal would be to have this button preform a function on an image, whose string has been placed in a textbox on the GUI, then replace the old string with the new image's string
The main issue that I have run into is that I need the file to be in .raw format, but I am unable to figure out how I would accomplish that. I can replace the old string with a new string in .png format with the following code by adding it the guide GUI code.
handles.currentImage = imread(get(handles.Textbox1,'string'));
handles.currentImage = Addnoise(handles.currentImage); %addnoise is the function i created.
imwrite(handles.currentImage, 'photonoise', 'png');
pathname = 'C:\Documents and Settings\staff\My Documents\MATLAB\photonoise.png';
set(handles.Textbox1, 'string', fullfile(pathname));
But i am totally lost on how to make the string format a .raw. If there is some code I do not know about that i need to add to the GUI or to the Addnoise function, please let me know, I would greatly appreciate it.
Since .raw images are not supported by imwrite, you won't be able to use it. However, since .raw is a image format that isn't compressed this can be easily done using fwrite.
Here's an example using a 300 x 100 matrix of just zeroes (black):
cmodel=(zeros(300,100,1));
fid=fopen('blackimage.raw','w+');
cnt=fwrite(fid,cmodel,'uint8');
fclose(fid);
so in your case you'll need to modify this to the following:
fid=fopen('myimage.raw','w+');
cnt=fwrite(fid,handles.currentImage,'uint8');
fclose(fid);
Warning: my knowledge of the .raw format is pretty limited as well as imread so please take this with a grain (maybe even a cube) of salt
I am reading a dicom file in matlab and modifying some data of it and trying to save it into another file, but while doing so, the private dicom data are either not written at all (when 'WritePrivate' is set to 0) or it's written as a UINT8 array which become incomprehensible and useless. I even tried to copy the data that I get in from the original dicom file to a new structure and write to a new dicom file but even though the private data remains fine in new structure it doesn't remain so in the new dicom file. Is there any way to keep this private data intact while copying in to a new dicom file without changing the matlab dicom dictionary?
I have provided the following code to show what I'm trying to do.
X=dicomread('Bad011_4CH_01.dcm');
metadata = dicominfo('Bad011_4CH_01.dcm');
metadata.PatientName.FamilyName='LastName';
metadata.PatientName.GivenName='FirstName';
birthday=metadata.PatientBirthDate;
year=birthday(1,1:4);
newyear=strcat(year,'0101');
metadata.PatientBirthDate=newyear;
names=fieldnames(metadata);
h=metadata;
dicomwrite(X,'example.dcm',h,'CreateMode','copy');
newh=dicominfo('example.dcm');
Here the data in newh contains none of the private data. If I change the code to the following
dicomwrite(X,'example.dcm',h,'CreateMode','copy','WritePrivate',1);
In this case the private data gets totally changed to some UIN8 array and useless. The ideal solution for my task would be to enable keeping the private data in the newly created dicom file without changing the matlab dicom dictionary.
Have you tried something like:
dicomwrite(uint16(image), fileName, 'ObjectType', 'MR Image Storage', ...
'WritePrivate', true, header);
where "header" is a struct composed of name-value pairs using the same format as header data that you would get from MATLAB's dicominfo function? My general approach to image creation in MATLAB is to avoid using CreateMode 'copy' and instead build my own DICOM header by explicitly copying the attributes that it makes sense to copy and generating my own values for attributes that should have new values.
To write private tags, you would do something like:
header.Private_0045_10xx_Creator = 'MY_PRIVATE_BLOCK';
header.Private_0045_1001 = int32(65535);
If you then write this out using dicomwrite and read it back in using hdr = dicominfo('mynewimg');, you can see that it really did write the value as a 32-bit integer even though, unfortunately, if is always going to read the data in as a vector of uint8 values.
>> hdr.Private_0045_1001
ans =
255
255
0
0
As long as you know what type to expect, you should be able to typecast the data back to the desired type after you've read the header. For example:
>> typecast(hdr.Private_0045_1001, 'int32')
ans =
65535
I know I'm about 8 years late, but have you tried
dicomwrite(..., 'VR', 'explicit')
?
It solves the "reading as uint8" problem for me.
Edit:
Actually, it looks like you need to specify a dicom dictionary with the VR of that tag. If you combine this with 'VR', 'explicit', then the program reading the dicom won't need to dictionary file.
I'm working with a path with a lot of images and I need to make a for function to iterate all of them, and do stuff with each image. I made this funcion for this, but I miss one little detail:
myfiles = dir(fullfile('./mypath','*.png')); # path
for i=0:TOTAL_NUMBER_OF_IMAGES_OF_THE_PATHS
im = imread(['./mypath/', myfiles(i).name,'']); # im = current image
do stuff...
So, what can I put on that TOTAL_NUMBER_OF_IMAGES_OF_THE_PATHS?
Thanks in advance
You'll need the number of files for TOTAL_NUMBER_OF_ELEMENTS. dir returns a struct and you can get the number of elements using length or numel. So your for loop would read like:
for i=1:length(myfiles)
...
end