How can i find the area of objects with RGB
2 次查看(过去 30 天)
显示 更早的评论
Everythime I try find the area my valours is very wrong.This square has a 10000cm^2 and I don't know how this object has the area ocupped
My code:
close all; clear all; clc; %%
%pasta=('F:\LDSC\Eco\Processamento direto\Versao 3\P3-17032017WW');
pasta=('F:\LDSC\Eco\CAMPOS\2CAMPO\eefd\b\croped');
dire = dir(pasta); % nome do arquivo, DSC* abre todos os arquivos que começam com esse nome
tt = length (dire); % conta quantidade de arquivos semelhantes
w=1;
areareal=zeros(tt-2,100)
c=1;
for i=3:tt
arq = fullfile([dire(i).name]); % le 1 arquivo
%Algoritmo pra aplicar o threeshold
y=imread(arq);
%Não esquece de chamar a imagem com o nome e extensão certa. %bw=rgb2gray(y);% FUNÇÃO TRANSFORMA IMAGEM EM CINZA %.90 foi o melhor numero para aplicar o filtro e nao perder qualidade imwrite(y,'foto.JPG') original = imread('foto.JPG'); pt=size(original);%pixelxpixel pixeltotal= pt(1,1)*pt(1,2);
imshow(original);
x = original(:,:,1) < 90;
imshow(x);
[B,L] = bwboundaries(x, 'noholes');
stats = regionprops(L, 'Area');
qtd_x = sum([stats.Area] > 1);
imshow(original);
hold on
for k = 1:length(B)
area = stats(k).Area*10000/(pixeltotal-stats(1).Area);
%*0.022469 Valor de pixel calculado
if area > 1
%Valor pra ser lido na AREAREAL
areareal(w,c)=area;
c=c+1;
end
%Aprimoramento do código
%'Abrir Txt com valores de area '
%Imprimir Foto X - Valor de área
if area > 1
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'black', 'LineWidth', 2);
text(boundary(1,2), boundary(1,1), sprintf('%.3f',area),...
'Color', 'black',...
'FontSize', 12,...
'FontWeight', 'bold',...
'BackgroundColor', 'white');
end
end
hold on
% % aqui vai a rotina de préprocessamento
w=w+1;
c=1;
save
%
end
0 个评论
采纳的回答
Image Analyst
2017-8-23
To convert from pixels to square cm you need to do a spatial calibration. Basically you have to somehow identify something in the image of known dimensions and then get a "cm per pixel" factor that you can multiply distances by to convert them from pixels to cm, and multiply by the square of that to convert an area in pixels to square cm.
See my attached demo.
4 个评论
Image Analyst
2017-8-28
I don't know what you did. What you should do is to threshold the image for dark things, then label and call regionprops. Something like
binaryImage = grayImage < 128;
binaryImage = imclearborder(binaryImage);
[labeledImage, numRegions] = bwlabel(binaryImage);
props = regionprops(labeledImage);
See my Image Segmentation Tutorial for a full demo. http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862&sort=downloads_desc
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!