How to check if all pixels in a large neighborhood (such as a square or triangle) are all non-zero values?

3 ビュー (過去 30 日間)
I want to check if all pixels in a 9x9 box hold non-zero values. This square is centered around a xy coordinate that I have chosen. I wrote hideous code manually incrementing and decrementing the xy coordinates but this code is several dozen lines long and hard to manage! Is there a better way to do this, perhaps with the strel tool?

採用された回答

Image Analyst
Image Analyst 2017 年 7 月 4 日
Simply use nnz() and pass it the 9 values in the window:
m = magic(9) % Sample data
% Define center of 3x3 window.
row = 3;
col = 3;
% Extract the 9 pixels in the window.
the9Pixels = m(row-1:row+1, col-1:col+1);
if nnz(the9Pixels) == 9
msgbox('All 9 pixels have non-zero values')
else
msgbox('Some of the 9 pixels have zero values')
end
  2 件のコメント
Naim
Naim 2017 年 7 月 4 日
編集済み: Walter Roberson 2017 年 7 月 4 日
is there something similar to this for circles?
the9Pixels = m(row-1:row+1, col-1:col+1);
is there a way I can play around with this to make it a circle?
Image Analyst
Image Analyst 2017 年 7 月 4 日
編集済み: Image Analyst 2017 年 7 月 4 日
Not sure what you're meaning. If you still don't know what to do after reading the FAQ on circles, then write back.
Or, if you want a bigger moving window, then use strel() with the 'disk' option.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 7 月 4 日
Take logical() of the array, to reduce the question to 0 vs 1 (pixel non-zero).
Use conv2() to pass in the logical array together with an array in which array elements are set to 1 in locations that need to be non-zero. If the status of the center pixel is not relevant be sure not to set it to 1 in the mask.
Now compare the result of the conv2 to nnz() of the mask. If the two are the same then all of the pixels in the mask region were set.
This process is essentially the same thing as "image dilation".
  1 件のコメント
Walter Roberson
Walter Roberson 2017 年 7 月 5 日
By the way you can use strel() to create appropriate disk-shaped structuring elements.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by