0
votes

I'm a total beginner to matlab and I'm currently writing a script for extracting data from a thermographic video.

Firstly the video is cut in separate frames. The first frame is opened as a sample picture to define the coordinates of sampling points. The goal is then to select the rgb values of those defined coordinates from a set of frames and save them into a matrix.

Now I have a problem separating the matrix to n smaller matrices. e.g I'm defining the number of points to be selected to n=2 , with a picture count of 31. Now it returns a matrix stating the rgb codes for 31 pictures, each at 2 points, in a 62x3 double matrix...

Now I want to extract the 1st, 3rd, 5th....etc... row to a new matrix...this should be done in a loop, according to the number of n points...e.g 5 points on each picture equals 5 matrices, containing values of 31 pictures....

this is an extract of my code to analyse the pictures, it returns the matrix 'values'

files = dir('*.jpg');
num_files = numel(files);

images = cell(1, num_files);
cal=imread(files(1).name);

n = input('number of selection points?: ');

imshow(cal);

[x,y] = ginput(n);

eval(get(1,'CloseRequestFcn'))

%# x = input('x-value?: ');   manual x selection
%# y = input('y-value?: ');   manual y selection

for k = 1:num_files
    images{k} = imread(files(k).name); 
end

matrix=cell2mat(images);
count=(0:size(matrix,1):size(matrix,1)*num_files);

for k = 1:num_files
    a(k)= mat2cell(impixel(matrix,x+count(k),y));
end

values = cat(1,a{:})
1

1 Answers

0
votes

Easy fix

Do you mean that if you have:

n = 2;
k = 2; % for example
matrix = [1 2 3;
          4 5 6;
          7 8 9;
          8 7 6];

you want it to become

b{1} = [1 2 3;
        7 8 9];
b{2} = [4 5 6;
        8 7 6];

This can be easily done with:

 for ii = 1:n
     b{ii} = matrix(1:n:end,:);
 end

Better fix

Of course it's also possible to just reshape your data matrix and use that instead of the smaller matrices: (continuing with my sample data ^^)

>> d=reshape(matrix',3,2,[]);
>> squeeze(d(:,1,:))

ans =

     1     7
     2     8
     3     9
>> squeeze(d(:,2,:))

ans =

     4     8
     5     7
     6     6

Good practice

Or, my preferred choice: save the data immediately in an easy to access way. Here I think it will be an matrix of size: [num_files x num_points x 3]

If you want all the first points:

rgb_data(:,1,:)

only the red channel of those points:

rgb_data(:,1,1)

and so on. I think this is possible with this:

rgb_data = zeros(num_files, num_points, 3);
for kk = 1:num_files
    rgb_data(kk,:,:) = impixel(images{kk},x+count(k),y);
end

But I don't understand the complete meaning of your code (eg: why matrix=cell2mat(images) ??? and then of course:

count=(0:size(matrix,1):size(matrix,1)*num_files);

is just count=0:num_files; so I'm not sure what would come out of impixel(matrix,x+count(k),y) and I used images{k} :)