计算图像中前景和背景像素的数量 [英] Count the number of foreground and background pixels in an image

查看:858
本文介绍了计算图像中前景和背景像素的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以字母'B'为例:



如果我们绘制字母'B'的直线4边,那么它将创建一个矩形框。我必须在上图中的此边界框(Recangle Box)中找到前景和背景像素的数量。我没有得到结果。请帮助我。谢谢。

.
If we draw the straight line 4-side of the letter 'B', then it will create a rectangle box. I have to find number of foreground and background pixels within this bounding box(Recangle Box) on the above picture. I did not get the result.pls help me. thank you.

这是我的代码:

 E = imread('1.jpg');
 label = graythresh(E);
 BW = im2bw(E, label);
 imshow(BW)
 L = bwlabel(E);
 z = regionprops(L,'BoundingBox');
 nBlackpixel = sum(z(:))
 nWhitepixel = numel(z) - nBlack


推荐答案

仅使用'BoundingBox'属性

z = regionprops( L, 'BoundingBox' );
nFG = zeros(1,numel(z)); % pre-allocate
nBG = zeros(1,numel(z));
for ii=1:numel(z), % in case there are more than one object in the image
    bb = imcrop( E, z(ii).BoundingBox ); % crop the mask according to BoundingBox
    nFG(ii) = nnz(bb); % count number of FG pixels
    nBG(ii) = numel( bb ) - nFG(ii); % nBG = tot number in BB minus num of FG
end 

这篇关于计算图像中前景和背景像素的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆