Count black and white pixels on a image
显示 更早的评论
Hello,
I have this 800x800 image and i want to count the number of black and white pixels in it. To do so i have read the image and divide it (the matrix) in 200x200 cells. But now i am with a bit of dificulty in making a scipt that can count B&W pixels in each cell and returns the values, something like this:
Cell(1,1)--> Black_pix=100; white_pix=300 Cell(1,2)--> Black_pix=100; white_pix=300
I already figured it out how to count B&W pixels in the entire image to do so, i've written the following script:
xmax=800;
ymax=800;
Image = imread('3080.jpg');
BW = im2bw(Image);
BW1=double(BW);
White_pix=0;
Floc=0;
for j=1:(xmax)-1
for i=1:(ymax)-1
if BW1(i,j)==0
White_pix=White_pix+1;
else
Black_pix=Black_pix+1;
end
end
end
My problem is making this process for all of the cells created with "mat2cell" function. Any help would be appreciated!
Thanks
Fearing that my english was so bad that i didn't explain myself very well, i'm posting a small schematic of the paper sheet divided in smaller elements.
I need to have something like this:
Element 1 as 3000 black pixels and 4500 white pixels Element 2 as 4000 black pixels and 9800 white pixels ...
2 个评论
Jan
2011-7-21
I hate hate hate tinypic. I see the header line with the blue background. Then "Tags: Click and addtags..." and "<< Prev Next>>" buttons, which lead me to pictures of naked human. Then I get some Tools on the left bottom and some elemnts under "Grab your code". The "Images You'll also enjoy" are hilarious. I'd only enjoy to see, what Nuno wants to show. And there is absolutely nothing.
@Nuno: How is it possible, that a 200x200 block of a BW image contains 3000 black and 4500 white pixels?! Then sum must be 40.000, otherwise it is not a BW image in opposite to your shown code.
Nuno
2011-7-21
采纳的回答
更多回答(6 个)
Jan
2011-7-20
I do not see the reason to use a {200 x 200} cell, and this cell does not appear in your code at all.
To count the Black and White pixels in the full image:
Image = imread('3080.jpg');
BW = im2bw(Image);
nBlack = sum(BW(:));
nWhite = numel(BW) - nBlack;
Now let's divide the image into a {200 x 200} cell - although I do not see any reason for such an inefficient storage method:
BW2 = permute(reshape(BW, 4, 200, 4, 200), [1, 3, 2, 4]);
BWC = num2cell(BW2, [1, 2]);
nPixel = numel(BWC{1}); % Number of pixels per block
for jC = 1:size(BWC, 2)
for iC = 1:size(BWC, 1)
aBlock = BWC{iC, jC};
nBlack = sum(aBlock(:));
nWhite = nPixel - nBlack;
... Now whatever you want to do with these numbers
end
end
But I guess this more efficient solves your problem already:
BW2 = reshape(BW, 4, 200, 4, 200);
nBlack = reshape(sum(sum(BW2, 1), 3), 200, 200);
nWhite = 16 - nBlack;
Now Black and White are 200x200 arrays containing the numbers of pixels for each 4x4 sub-blocks.
Wolfgang Schwanghart
2011-7-20
How about using blockproc to get the number of white pixels (in case you have the image processing toolbox)?
A = full(sprand(800,800,0.3));
fun = @(block_struct) sum(block_struct.data(:));
C = blockproc(A,[200 200],fun);
Regards, W.
Nuno
2011-7-21
3 个评论
Sean de Wolski
2011-7-21
Did you bother reading: Christoph's, Jan's and my response?
Nuno
2011-7-21
Sean de Wolski
2011-7-21
What is not clear about this?
white = sum(Image(:)); %number of white px
black = numel(Image) - white; %number of black px
Christoph
2011-7-20
image=ones(800,800);
image(1:400,:)=0;
[b w]=countBW(image)
function [black white]=countBW(Image)
black=length(Image(Image==0));
white=length(Image(Image==1));
end
Do you mean something like that?
5 个评论
Christoph
2011-7-20
I assume that you need to use your 200x200 cells otherwise no need for anything else than what Jan already wrote.
Jan
2011-7-20
@Christoph: I was confused also - the "white" pixels have the value 0... So an efficient counting is: "black = sum(Image(:))". The creation of the temporary arrays "tmp1=(Image==1)" and "tmp2=Image(tmp)" consume a lot of time.
Sean de Wolski
2011-7-20
white = sum(Image(:)); @ Jan typo
black = numel(Image) - white;
Jan
2011-7-20
@Sean: These wiered bits are going to drive me crazy. In the question I find: "if BW1(i,j) == 0, White_pix = White_pix+1". However, the OP will have the power to decide if black is black or white is black or white is white...
Sean de Wolski
2011-7-20
Good point...
Sean de Wolski
2011-7-21
0 个投票
tinypic makes this possible though: Nuno's image, no porn/ads/taggings/things you may (or may not) be interested in etc.

2 个评论
Nuno
2011-7-21
Image Analyst
2022-7-3
Is that the '3080.jpg' image? It's now missing from tinypic.com.
That's another disadvantage of posting images on third party web sites -- they can be removed or taken down. Would not have happened if it was hosted here on the Mathworks site.
类别
在 帮助中心 和 File Exchange 中查找有关 Get Started with MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!