i have 250 images stored at
E:\HandVein_DataSet
these 250 images belong to 50 person , this mean each one has 5 images
these pictures are arranged as following
0001hv1-0001hv2-0001hv3-0001hv4-0001hv5 ....0002hv1-0002hv2-0002hv3-0002hv4-0002hv5 until 0050hv1-0050hv2-0050hv3-0050hv4-0050hv5
I want to read all these images and processing them according my steps and then saved the resultant images after preproceesing in a specific file,for example at E:\final result.
How can i do this in matlab?
I'm assuming that the filenames are, e.g., 0001hv1.jpg (you can change the extension as necessary). You don't say what the output is; I'm assuming it's a modified version of the input image and that you want to use the same naming scheme.
in_dir = 'E:/Hand/Vein_DataSet';
out_dir = 'E:/final\ result';
for px = 1 : 50,
for hx = 1 : 5,
fname = sprintf('%04dhv%d.jpg', px, hx);
current_image = imread(fullfile(in_dir, fname));
% do processing
out_image = some_function(current_image);
% save output
imwrite(out_image, fullfile(out_dir, fname));
end
end
Related
I want to know size of header in an image in jpeg2000 format. how can I find out this header?
I want to calculate pure size of image.jp2 in hard disk without the size of header.
I use iminfo to find some information in matlab about image, but I don't know that can I find the header size of the image from this?
Actually I compress an image with jpeg2000 and I want to calculate rate of compression without header size.
please help me.
J2K files have 4 compulsory top-level boxes. They are
JPEG 2000 signature box
File Type box
JP2 Header box
Contiguous Codestream box
Each box is preceded by 4 byte marker and 4 byte size value. So in MATLAB it should be something like this
fname='C:\Users\admin\Documents\MATLAB\SO\Jpeg2k\balloon.jp2';
fid = fopen(fname);
headerMark = uint8('jp2h');
matchCnt = 1;
ch = fread(fid,1,'*uint8');
matchCnt = matchCnt+isequal(headerMark(1),ch);
while matchCnt < 5 && ~feof(fid)
ch = fread(fid,1,'*uint8');
matchCnt = matchCnt+isequal(headerMark(matchCnt),ch);
end
if matchCnt == 5
fseek(fid,ftell(fid)-8,'bof');
sizeBytes = fread(fid,4,'*uint8');
sizeVal = arrayfun(#(x,y) bitshift(x,y,32), uint32(sizeBytes), [3:-1:0]');
sizeVal = bitor(bitor(bitor(sizeVal(1),sizeVal(2)),sizeVal(3)),sizeVal(4));
end
fclose(fid);
I don't know what is you final task (getting header size seems to be halfway) but I recommend to see the quickstart guide for JPEG2000, JPEG2000 validator (written in python) and the validator overview.
I am working with lung data sets in matlab, but I need to sort the slices correctly and show them.
I knew that can be done using the "instance number" parameter in Dicom header, but I did not manage to run the correct code.
How can I do that?
Here is my piece of code:
Dicom_directory = uigetdir();
sdir = strcat(Dicom_directory,'\*.dcm');
files = dir(sdir);
I = strcat(Dicom_directory, '\',files(i).name);
x = repmat(double(0), [512 512 1 ]);
x(:,:,1) = double(dicomread(I));
axes(handles.axes1);
imshow(x,[]);
First of all, to get the DICOM header, you need to use dicominfo which will return a struct containing each of the fields. If you want to use the InstanceNumber field to sort by, then you can do this in such a way.
%// Get all of the files
directory = uigetdir();
files = dir(fullfile(directory, '*.dcm'));
filenames = cellfun(#(x)fullfile(directory, x), {files.name}, 'uni', 0);
%// Ensure that they are actually DICOM files and remove the ones that aren't
notdicom = ~cellfun(#isdicom, filenames);
files(notdicom) = [];
%// Now load all the DICOM headers into an array of structs
infos = cellfun(#dicominfo, filenames);
%// Now sort these by the instance number
[~, inds] = sort([infos.InstanceNumber]);
infos = infos(inds);
%// Now you can loop through and display them
dcm = dicomread(infos(1));
him = imshow(dcm, []);
for k = 1:numel(infos)
set(him, 'CData', dicomread(infos(k)));
pause(0.1)
end
That being said, you have to be careful sorting DICOMs using the InstanceNumber. This is not a robust way of doing it because the "InstanceNumber" can refer to the same image acquired over time or different slices throughout a 3D volume. If you want one or the other, I would choose something more specific.
If you want to sort physical slices, I would recommend sorting by the SliceLocation field (if available). If sorting by time, you could use TriggerTime (if available).
Also you will need to consider that there could also potentially be multiple series in your folder so maybe consider using the SeriesNumber to differentiate these.
I wrote a matlab code that reads images from folder(images are in RGB). It reads the image one by one. After reading the first image it re-sizes it and then reshapes it so that the new image dimension is (any number of rows X 3 volumes) using this function:
Vectrgb = reshape(rgb,[],3);
Then the code will read a new image.
How should I append the matrix of the second image to the one?
Below is the code:
Folder = 'fo\';
Files = dir([Folder, '\*.png']);
Y=[];
for i = 1 : length(Folder)
name = strcat('telo-phase\',Files(i).name);
image = imread(name);
rgb = imresize(image, 0.50); % resize the image
Vectrgb = reshape(rgb,[],3);
end
Vectrgb1 = Vectrgb';[ind2,ctrs2]= kmeans(double(Vectrgb1),2);
If you wat Y to be the concatanation of all RGB vectors of all images you need to add this line inside the loop
Y = cat( 1, Y, Vecrgb );
PS,
When dealing with filenames and paths it is a good practice to use fullfile command:
Files = dir( fullfile( Folder, '*.png' ) );
Makes your code platform independent.
Here i am sharing one of my data which are in .dat file. I have 16162 different files. I merged all into one file and want to read it in matlab and need to extract three parameter's values from a single file and arrange them in either row wise or column wise. I can do it by using C sharp codes but i want to do it by using matlab. can anybody help me out for writing the codes please?
Here is the one sample file data:
DISTRIBUTION: monomodal log-normal
n : 1.000
r_mod: .010
sigma: 1.400
number conc., surface. conc., volume conc.
.1087E+01 .1866E-02 .7878E-05
part. ave. radius, surf. ave. radius, vol. ave. radius :
.1149E-01 .1169E-01 .1201E-01
surface mean radius, volume mean radius :
.1267E-01 .1392E-01
eff. variance :
.9939E-01
Let's say: I want to extract or read three parameters (r_mod, sigma, Surface means radius). The corresponding values for these three parameters from the file I put in this page is .010 , 1.400 , .1267E-01
The output should be (Which i want):
r_mod sigma surface mean radius .01 1.4 1.27E-02 .02 1.4 2.67E-02 .03 1.4 3.98E-02 ... .. .. .. .. .. .. .. ..
I have more than thousands similar files in a same directory. I want to read all those files in matlab and the output should show in this way in a single file.
Given that all files have the exact same structure, the following will do the job (just make sure to ream the comments in the code, you will need to adapt your file names and number of files to read):
n = 2; % Number of files you want to go through
vals = zeros(1,3*n);
str = 'r_mod sigma surface mean radius ';
k = 1;
for i = 1:n
path = ['myFile',num2str(i),'.dat']; % change this to fit your file names
fid = fopen(path, 'rb');
data = textscan(fid,'%s');
fclose(fid);
data = data{1};
vals(k) = str2double(data{8});
vals(k+1) = str2double(data{10});
vals(k+2) = str2double(data{40});
k = k+3;
end
out = [str, num2str(vals)];
fid = fopen('output.txt', 'w');
fprintf(fid,out);
The file output.txt now contains your desired line. You may change the format if you want the output file to be .dat as well.
I have a folder that has various images I am trying to read images in a loop from folder one by one using imread command, any suggestion how to do it best
Thanks
You can use the dir function to get all files inside a directory. This will return a vector of structs which also includes the filenames
loop over all structs
check if file is an image (check e.g. extension)
read image
You have to synchronize your image names to read in a loop. They have to be like image1.jpg, image2.jpg, image3.jpg... You have 10 images like this.
NumberOfimages=10; %chose the number of images you want to give input
prefix_image='image'; %change the desired input image name here only
fileformat='.jpg'; %change the desired input image format here only
for num=1:NumberOfimages
image = imread(strcat(prefix_image,num2str(num),fileformat));
end
well this function will help you to read many images at once even with diferent names:
First put all your images in a folder for example: D:/lab/
they must be in the same format for example: tif
to call the function you type :
A=imread_many( );
and all the images will be in the variable A
if you want the first image you type A{1} if u want the second u type A{2} ..etc
function [ A ] = imread_many( )
srcFiles = dir('D:\lab\*.tif'); % the folder in which ur images exists
for i = 1 : length(srcFiles)
filename = strcat('D:\lab\',srcFiles(i).name);
A{i} = imread(filename);
figure, imshow(A{i});
end
end
srcFiles = dir('C:\Users\Omm\Downloads\multicharacterrec\*.png'); % the folder in which ur images exists
for i = 1 : length(srcFiles)
filename = strcat('C:\Users\Omm\Downloads\multicharacterrec\',srcFiles(i).name);
I = imread(filename);
figure, imshow(I);
This can Help !
S = struct2cell(dir('*.jpg'));
FileNames = S(1,:);
lenDb = length(FileNames);
result= struct('img', {});
for j = 1 : lenDb
result(j).img = imread(FileNames{j})
end
All the images are in struct "result.img"
To have access just call result(NUM).img
Ex:
image(result(1).img)