How to count black pixels block by block in an image I divided into 16x16 blocks?
    조회 수: 8 (최근 30일)
  
       이전 댓글 표시
    
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
댓글 수: 0
채택된 답변
  monika shivhare
      
 2018년 5월 30일
        Assuming that if count(number of black pixels in a block) exceeds a certain threshold, you want to change the edges of block to a different color in the original image. You can use the following code below the one you have created cell array "Blocks"
% suppose threshold value is th =150 
% different color is white nc=[r g b]
th=150;
nc=[255 255 255];
[r,c]=size(Blocks);
for i=1:r
    for j=1:c
        %cb is no of black pixels
        cb=nnz(Blocks{i,j}==0);
        if(cb>th)
            %putting edges to a different color nc
            x=(i-1)*16+1;
            for y=(j-1)*16+1:j*16
                RGB(x,y,:)=nc;
            end
            x=i*16;
            for y=(j-1)*16+1:j*16
                RGB(x,y,:)=nc;
            end
            y=(j-1)*16+1;
            for x=(i-1)*16+1:i*16
                RGB(x,y,:)=nc;
            end
            y=j*16;
            for x=(i-1)*16+1:i*16
                RGB(x,y,:)=nc;
            end
          end
      end
  end
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 File Exchange에서 Computer Vision with Simulink에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

