1

我正在编写一个简短的 Matlab 脚本来使用 im2bw 来计算图像中对象的面积。我正在尝试验证这些区域的计算是否正确,但是在参考图像上,我使用的最后一个正方形的区域减少了 2%,我不知道为什么。


myFolder = 'Your Directory';
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.png'); % Change to whatever pattern you need.
theFiles = dir(filePattern);

for k = 1 : length(theFiles)
    baseFileName = theFiles(k).name;
    fullFileName = fullfile(theFiles(k).folder, baseFileName);
    
    J = imread(fullFileName);
    %J = imcrop(I,[500,200,4400,3500]);
    BW = im2bw(J,0.3);
    BW2 = imcomplement(BW);
    BW3 = imfill(BW2,4,'holes');
    
    %figure(1)
    %imshowpair(I,BW3,'montage')

    stats = regionprops(BW3,'area','PixelList','majoraxislength','minoraxislength');


    area = zeros(size(stats));
    for i = 1:size(stats,1)
        area(i) = stats(i).Area;
    end
    scale = 1e-6;
    %scale = 3.134755344e-8;
    cutoff = 0;
    area = area*scale;
    stats(area<cutoff)=[];
    area(area<cutoff)=[];
    
    writematrix(area,'Your Directory','WriteMode','append')
end

    figure(1)
    imshowpair(J,BW3,'montage')

图像为 1000x1000,正方形分别为 20x20、100x100、100x200、200x200。

我假设现实中的参考图像是 1mx1m 并相应地缩放,但 200x200 的正方形似乎并不完全正确。

4

0 回答 0