使用Matlab im2bw时像素到区域转换无法正常工作

使用Matlab im2bw时像素到区域转换无法正常工作,matlab,image-processing,binary-image,Matlab,Image Processing,Binary Image,我正在编写一个简短的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 what

我正在编写一个简短的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')


myFolder='您的目录';
%获取文件夹中具有所需文件名模式的所有文件的列表。
filePattern=fullfile(myFolder,'*.png');%改变你需要的任何模式。
theFiles=dir(filePattern);
对于k=1:长度(文件)
baseFileName=theFiles(k).name;
fullFileName=fullfile(文件(k).folder,baseFileName);
J=imread(完整文件名);
%J=imcrop(I[50020044003500]);
BW=im2bw(J,0.3);
BW2=补码(BW);
BW3=填充物(BW2,4,“孔”);
%图(1)
%imshowpair(I,BW3,“蒙太奇”)
统计数据=区域属性(BW3、'area'、'PixelList'、'majoraxislength'、'minoraxislength');
面积=零(大小(统计数据));
对于i=1:大小(统计数据,1)
面积(i)=统计数据(i)。面积;
结束
比例=1e-6;
%比例=3.134755344e-8;
截止值=0;
面积=面积*比例;
统计数据(面积)