0

I have made a a group of new pictures using imcrop from the same file, With this code, I know it's long but since the distances are not always the same I find no other way to do it than this:

A001=imcrop(A,[65 159 95 332]);
A002=imcrop(A,[182 161 95 332]);
A003=imcrop(A,[297 164 95 332]);
A004=imcrop(A,[402 165 90 332]);
A005=imcrop(A,[495 168 90 332]);
A006=imcrop(A,[606 166 90 332]);
A007=imcrop(A,[705 171 90 332]);
A008=imcrop(A,[808 175 90 332]);
A009=imcrop(A,[922 175 90 332]);
A0010=imcrop(A,[1031 175 90 332]);

Then I have a series of tasks to be performed on each of the new images, how do i get around that the easiest way? When I import multiple jpegs from a folder I can get it to make a dataset of the files but when I try to do the same with A001:A0010 I get nothing.

This is the task that I want to perform:

greenChannel = A(:, :, 2);

BW = edge(greenChannel,'Prewitt');
figure, imshow(BW)


 %Dialate Lines
se90 = strel('line', 3, 90);
se0 = strel('line', 3, 0);
BWsdil = imdilate(BW, [se90 se0]);;
figure, imshow(BWsdil), title('dilated gradient mask');


%Fill Lines
BWdfill = imfill(BWsdil, 'holes');
figure, imshow(BWdfill);
title('binary image with filled holes');


BWnobord = imclearborder(BWdfill, 4);
figure, imshow(BWnobord), title('cleared border image');

seD = strel('diamond',1);
BWfinal = imerode(BWnobord,seD);
BWfinal = imerode(BWfinal,seD);
figure, imshow(BWfinal), title('segmented image');

L = bwlabel(BWfinal);
s = regionprops(L,'centroid');

What I need help to do is somehow get A001:A0010 into A in the top and run that sequence of commands, hope someone can help me achieve that!

Paul Floyd
  • 5,530
  • 5
  • 29
  • 43
Mikkel Astrup
  • 405
  • 6
  • 18
  • What do you mean by "when i try to do the same with A001:A0010 i get nothing"? What sort of dataset are you trying to create with those images? Have you tried putting the images in a cell array? – beaker Jan 20 '16 at 15:49

1 Answers1

0

This is hairy, but here goes:

A = imread('peppers.png');
A = imresize(A, [1500 1500]); % to handle the indexing range.

A001=imcrop(A,[65 159 95 332]);
A002=imcrop(A,[182 161 95 332]);
A003=imcrop(A,[297 164 95 332]);
A004=imcrop(A,[402 165 90 332]);
A005=imcrop(A,[495 168 90 332]);
A006=imcrop(A,[606 166 90 332]);
A007=imcrop(A,[705 171 90 332]);
A008=imcrop(A,[808 175 90 332]);
A009=imcrop(A,[922 175 90 332]);
A0010=imcrop(A,[1031 175 90 332]);

w = who; % returns the names of all your current variables in a cell.

for i = 1:numel(w)
    % A00 is unique to all the variables you want to process.
    if ~isempty(strfind(w{i}, 'A00')) 
        % hard coding greenChannel and extracting the second plane.
        eval(['greenChannel = ',w{i},'(:,:,2)']); 
        % do the rest of the processing here, 
        % from BW = edge ... to regionprops. 
        % You may have to save the s structure as a cell array.
    end
end

This uses the who command to extract all the current variables, and the eval command to evaluate what is passed in as text, based on the variable names. Note that using eval is dangerous, and should be done only if there are no better alternatives. See Use and implications of eval('expression') in MATLAB code?

akamath
  • 570
  • 2
  • 9
  • Thanks a million for the help! Saved me a ton of time! Only have one question, im not sure how to get the s structure to save as a cell array. I tried the struct2cell command but that isn't working out. – Mikkel Astrup Jan 20 '16 at 23:01
  • The problem is that the size of s varies from 3x1 struct to 7x1 struct, and that i can't seem to figure out – Mikkel Astrup Jan 20 '16 at 23:28
  • so struct2cell will try to create a cell from your structure. I don't think you want that. You could just use a {} constructor to save your entire struct to a cell element. For example, s.a = 1; s.b = 2; s.c = 3; s2.a = 1; s2.b = 2; myCell{1} = s; myCell{2} = s2; and so on.. Hope that helps! – akamath Jan 20 '16 at 23:40