imcrop成1变量

时间:2016-01-20 15:14:26

标签: image matlab loops image-processing

我使用同一个文件中的imcrop创建了一组新图片。使用此代码,我知道它很长但是由于距离并不总是相同,我找不到其他方法它比这个:

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]);

然后我要对每个新图像执行一系列任务,如何绕过最简单的方法?当我从一个文件夹中导入多个jpeg时,我可以让它来创建文件的数据集但是当我尝试对A001进行相同的操作时:A0010我什么都没得到。

这是我想要执行的任务:

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');

我需要帮助做的是以某种方式将A001:A0010放到顶部的A中并运行命令序列,希望有人可以帮助我实现这一目标!

1 个答案:

答案 0 :(得分:0)

这很毛茸茸,但这里有:

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

这使用who命令提取所有当前变量,并使用eval命令根据变量名称评估作为文本传递的内容。请注意,使用eval是危险的,只有在没有更好的替代方案时才应该这样做。见Use and implications of eval('expression') in MATLAB code?