Reputation:
I'm trying to find the number of black pixels with NO black pixel neighbours at any of right, upper right, lower right .
mask1 = [0|1 0|1 0; 0|1 0|1 0; 0|1 0|1 0];
black = double (x28462847_1_1 < 128);
result = conv2 (black, mask1, 'same');
count = sum (result(:) == 1)
I tried using this mask. Does anyone know where I'm going wrong
Upvotes: 0
Views: 141
Reputation: 35525
I think the best way of doing this is:
notblack=black>0; %if its not zero, then its not black
mask=[0 1 0; 1 0 1; 0 1 0]% or mask=[0 0 1; 0 0 1; 0 0 1]; in your case.
result = conv2(double(notblack),mask, 'same'); % sum all values
count=sum(result(:)==sum(mask(:)));
Upvotes: 1
Reputation: 11064
To detect all black pixels (ones) which has no black pixels at any of its 3 (upper/lower) right pixels, you can calculate the cross correlation with the following mask:
mask = [0 0 -1;
0 1 -1;
0 0 -1];
The result will be 1
for the desired positions and smaller for the others.
result = conv2 (double(black), rot90(mask,2), 'same') == 1; % Note that I used `conv2` with `rot90` to calculate the cross correlation.
count = sum (result(:)); % count the number of desired elements
Example
For the following image
rng('default')
black = rand(10) > 0.8;
I obtain the following result:
figure
imshow(~black, 'InitialMagnification','fit')
hold on
[x, y] = find(result);
plot(y, x, '*') % highlight the found positions with an asterisk
Upvotes: 1