Matlab 提取彩色区域
我想在对特定图像应用SRM分割方法后,在MATLAB中提取每个彩色区域 我尝试了以下方法,但它似乎提取了具有不同颜色(不仅仅是相同的颜色等级)和最大面积的区域Matlab 提取彩色区域,matlab,image-segmentation,Matlab,Image Segmentation,我想在对特定图像应用SRM分割方法后,在MATLAB中提取每个彩色区域 我尝试了以下方法,但它似乎提取了具有不同颜色(不仅仅是相同的颜色等级)和最大面积的区域 I = imread('./img/bfly.jpg'); imshow(I) bw = im2bw(I); imshow(bw) L = bwlabel(bw); imshow(L == 0) props = regionprops(L); [~,ind] = max([props.Area]); imshow(L == ind)
I = imread('./img/bfly.jpg');
imshow(I)
bw = im2bw(I);
imshow(bw)
L = bwlabel(bw);
imshow(L == 0)
props = regionprops(L);
[~,ind] = max([props.Area]);
imshow(L == ind);
有没有办法分别提取每种颜色
这是一个示例图像。我想单独提取棕色,单独提取绿色,等等
由于您的图像似乎没有平滑的颜色变化,因此使用
unique
将图像转换为标签矩阵(您也可以使用rgb2ind
执行此操作)然后使用accumarray
将颜色分离为不同的图像应该很简单:
[Iu,ia,iu] = unique(reshape(I,[],3),'rows');
counts = accumarray(iu,1);
[counts,sortinds] = sort(counts,'descend');
现在假设您想要N
最大的组件:
N = 10;
largestLabels = sortinds(1:N);
然后是彩色ii
的图像:
mapi = reshape(iu == largestLabels(ii),size(I,1),size(I,2));
numeli = counts(ii)
对应的RGB值和每种颜色的像素数:
>> colorRegionSummary = [uint32(Iu(largestLabels,:)) counts(1:N)]
colorRegionSummary =
89 120 23 8206 % green
73 59 42 4370 % dark brown (wing)
64 128 184 2723 % blue (right shade)
105 136 25 2143 % green (bottom right shade)
64 127 178 1667 % blue (top left shade)
170 151 191 1380 % purple
58 132 201 1372 % blue (left shade)
177 130 45 1242 % orange (bottom wing shade)
184 123 50 1193 % orange (top wing shade)
118 114 56 586 % tan (top right)
请注意,这些不是连接的组件,只是具有相同颜色的组件。对于给定的
mapi
,您可以应用以获取该颜色的连接组件。由于您的图像似乎没有平滑的颜色变化,因此使用unique
将颜色分离为不同的图像以将图像转换为标签矩阵应该很简单(您也可以使用rgb2ind
执行此操作)然后是accumarray
:
[Iu,ia,iu] = unique(reshape(I,[],3),'rows');
counts = accumarray(iu,1);
[counts,sortinds] = sort(counts,'descend');
现在假设您想要N
最大的组件:
N = 10;
largestLabels = sortinds(1:N);
然后是彩色ii
的图像:
mapi = reshape(iu == largestLabels(ii),size(I,1),size(I,2));
numeli = counts(ii)
对应的RGB值和每种颜色的像素数:
>> colorRegionSummary = [uint32(Iu(largestLabels,:)) counts(1:N)]
colorRegionSummary =
89 120 23 8206 % green
73 59 42 4370 % dark brown (wing)
64 128 184 2723 % blue (right shade)
105 136 25 2143 % green (bottom right shade)
64 127 178 1667 % blue (top left shade)
170 151 191 1380 % purple
58 132 201 1372 % blue (left shade)
177 130 45 1242 % orange (bottom wing shade)
184 123 50 1193 % orange (top wing shade)
118 114 56 586 % tan (top right)
请注意,这些不是连接的组件,只是具有相同颜色的组件。对于给定的
mapi
,您可以应用以获取该颜色的连接组件。您可以先对三个颜色数组(RGB)进行编码,以便将它们合并到一个二维数组中,例如
2Dimage = I(:,:,1) + 1e3*I(:,:,2) + 1e6*I(:,:,3)
这样,每种颜色都有一个唯一的编号:R+1e3*G+1e6*B。请注意,每个通道都用间隔[0255]中的数字编码
现在,您可以使用
C = unique(2Dimage)
为了获得你需要寻找的独特颜色,然后
for idx = 1:length(C)
find(C(idx)==2Dimage)
end
定位图像的不同部分。可以在相应的位置/索引处从原始图像I轻松获取颜色。您可以从编码三个颜色数组(RGB)开始,以便将它们合并为一个二维数组,例如
2Dimage = I(:,:,1) + 1e3*I(:,:,2) + 1e6*I(:,:,3)
这样,每种颜色都有一个唯一的编号:R+1e3*G+1e6*B。请注意,每个通道都用间隔[0255]中的数字编码
现在,您可以使用
C = unique(2Dimage)
为了获得你需要寻找的独特颜色,然后
for idx = 1:length(C)
find(C(idx)==2Dimage)
end
定位图像的不同部分。可以在相应的位置/索引处从原始图像I轻松获取颜色。@chappjc:我不想只提取棕色,我想提取每种颜色并在单独的窗口中显示每一部分。最后3行用于什么?此代码选择具有较大颜色的对象est区域。你是从某处复制的吗?@user2752385我知道了。试试我的更新答案。它适用于你附加的图像。@chappjc:我不想只提取棕色,我想提取每种颜色并在单独的窗口中显示每一部分。最后3行是做什么的?这段代码选择了面积最大的对象。你复制了吗om某处?@user2752385我明白了。试试我的更新答案。它适用于你所附的图像。Thaank you very much thaan you very much thaan you very morry for later问题,但我如何定位图像的不同部分?让我这样说:2Dimage合并了图像的三层(R、G和B)到1。它的二维结构仍然存在。因此,您可以使用从2Dimage操作中获得的索引来选择图像中的相应位置。很抱歉,最近的问题是,在这之后,我如何定位图像的不同部分?让我这样说:2Dimage合并了图像的三层(R、G和B)但它的二维结构仍然存在。因此,您可以使用从2D图像操作中获得的索引在图像中选择相应的位置。