下面的图是用matlab软件编的,请问如何用matlab计算红色部分的面积

2024-12-19 19:52:36
推荐回答(1个)
回答1:

先找联通域再用regionprops(label,'Area')函数求面积;
clc;
clear;
close all;

I = imread('edgelun1.jpg');
% 提取R,G,B
I1 = I(:, :, 1);
I2 = I(:, :, 2);
I3 = I(:, :, 3);
% 二值化R,G,B
J1 = im2bw(I1);
J2 = im2bw(I2);
J3 = im2bw(I3);
% 连通块分析R,G,B
[L1, num1] = bwlabel(J1);num1
[L2, num2] = bwlabel(J2);num2
[L3, num3] = bwlabel(J3);num3
% 显示
figure;
subplot(2, 2, 1);
imshow(I);
title('Origin')
subplot(2, 2, 2);
imshow(I1);
title('R')
subplot(2, 2, 3);
imshow(I2);
title('G')
subplot(2, 2, 4);
imshow(I3);
title('B')
figure;
subplot(2, 3, 1);
imshow(J1);
title('R-Gray')
subplot(2, 3, 2);
imshow(J2);
title('G-Gray')
subplot(2, 3, 3);
imshow(J3);
title('B-Gray')
subplot(2, 3, 4);
imshow(L1);
title('R-Label')
subplot(2, 3, 5);
imshow(L2);
title('G-Label')
subplot(2, 3, 6);
imshow(L3);
title('B-Label')
% 整合
L = [];
for i = 1 : size(L1, 1)
for j = 1 : size(L1, 2)
temp = [L1(i, j) L2(i, j) L3(i, j)];
if temp == zeros(1, 3)
L(i, j) = 0;
else
if L1(i, j) ~= 0
L(i, j) = L1(i, j);
continue;
elseif L2(i, j) ~= 0
L(i, j) = L2(i, j);
continue;
elseif L3(i, j) ~= 0
L(i, j) = L3(i, j);
continue;
end
end
end
end
% 显示
figure;
imshow(L);
for color=1:num1
temp=I(I==color);
[label,n]=bwlabel(temp);
area=regionprops(label,'Area')
end

就可以求出每个颜色的面积(区域比例)了