我正在读取图像与印记,这导致768x1024x3矩阵与R,G,B值的每个像素。
我有一个函数,接收图像并返回每个像素的片段标签矩阵,所以这个矩阵是768x1024。标签只是数字1,2,3,4,这取决于函数找到多少个不同的段。
现在我要计算图像每个片段中的平均红、绿和蓝值。所以我想用片段标号矩阵中的索引来找出组的所有R,G,B值为单独的数组,然后能够计算平均值。
有什么聪明的办法吗?使用分段矩阵中每个1值的索引,从imread矩阵中获取值,并将分段分组成不同的数组?我想用循环和蛮力来解决这个问题,但有没有更好的方法来做到这一点?
发布于 2014-05-28 19:11:00
编辑了,以便同时处理所有通道。
让img作为您的RGB图像,并labels标签数组。
您可以使用以下标签隐藏RGB图像:
% create a 3-channels mask:
labelsRGB=repmat(labels, 1, 1, 3);
Segment1=img.*(labelsRGB==1);标记为1的段中的平均值是:
avg=mean(mean(Segment1, 1), 2);得到avg(1)中re的平均值,avg(2)中绿色的平均值,等等。
其他部分的视频。
发布于 2014-05-28 19:41:33
这是一个代码,您将得到您的一切,不需要循环。
码
%// img is your input RGB image (NxMx3)
%// L is your label matrix (NxM)
t1 = bsxfun(@eq,L,permute(unique(L),[3 2 1]));
t2 = bsxfun(@times,permute(img,[ 1 2 4 3]),t1);
t2(t2==0)=nan;
out = squeeze(nanmean(nanmean(t2)))
%// out is the desired output matrix that is (NLx3),
%// where NL is the number of labels. Thus, the mean of labels is
%// along the rows and the corresponding values for R, G and B are in the three
%// columns of it.解释
让我们用一些随机值来测试img -
img = randi(9,3,4,3)给我们-
img(:,:,1) =
9 7 5 3
7 7 2 4
1 6 7 9
img(:,:,2) =
8 6 6 4
4 9 3 9
3 9 8 1
img(:,:,3) =
5 4 4 5
7 2 5 3
2 3 1 3L从1到8的一些假定值
L = [1 3 3 4;
4 5 8 8;
5 6 7 2]代码输出是-
out =
9 8 5
9 1 3
6 6 4
5 4 6
4 6 2
6 9 3
7 8 1
3 6 4让我们看看如何理解输出。
看看输入,让我们选择标签8,它位于(2nd row,3rd col)和(2nd row,4th col)位置。在3.中这些位置对应的R值是[2 4],因此R均值/平均值必须是[2 4]。同样地,对于6,它必须来自[3 9],对于B来说,它必须来自[5 3],那就是4.。
让我们看一下表示[3 6 4],的out的8th行,它是前面计算出来的平均值。同样,其他平均值也可以从out中解释。
发布于 2014-05-28 19:44:01
这是一种普遍的选择。在这种情况下,您不需要遍历不同的段来获得每个片段的平均值。
%simulated image and label
img=rand(10,12,3);
labeled=[ones(10,3),ones(10,3)*2,ones(10,3)*3,ones(10,3)*4];
% actual code for the mean
red_mean = regionprops(labeled, img(:,:,1), 'MeanIntensity')https://stackoverflow.com/questions/23919881
复制相似问题