How to count black pixels block by block in an image I divided into 16x16 blocks?
Show older comments
I'm trying to write some code that divides a binary image into 16x16 non overlapping blocks and then goes through each block and counts the number of black pixels in it, then if the count exceeds a certain threshold the edges of the block are changed to a different color on the original image.
So far I managed to divide my image into the 16by16 blocks, but I don't know how to go through each of these and get the data I need, so any help would be appreciated.
Here is the code I got so far:
clear all;
RGB=imread('test.jpg');
I=rgb2gray(RGB);
imshow(I);
kernel = [1 1 1;1 10 -1;-1 -1 -1];
filteredImage = imfilter(I, kernel, 'same');
imshow(filteredImage);
threshold = graythresh(filteredImage);
X=im2bw(filteredImage,threshold);
BlockImage = im2double(X);
[m,n] = size(BlockImage);
Blocks = cell(ceil(m/16), ceil(n/16));
counti = 0;
for i = 1:16:m-15
counti = counti + 1;
countj = 0;
for j = 1:16:n-15
countj = countj + 1;
Blocks{counti,countj} = BlockImage(i:i+15,j:j+15);
end
end
Accepted Answer
More Answers (0)
Categories
Find more on Neighborhood and Block Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!