Fill background pixels with the value of the nearest foreground pixel

4 ビュー (過去 30 日間)
Matt J
Matt J 2021 年 9 月 30 日
編集済み: Matt J 2021 年 9 月 30 日
I have a grayscale image I and a logical mask of its foreground FG. I would like to do the operation roughly described as,
I(~FG)=I(nearest_pixel_in_FG)
In other words, similar to bwdist, I need to minimize the distance of each background pixel P to the foreground. However, I do not want the value of the minimum distance to be assigned to P. Instead, I want it assigned the value of the closest foreground pixel that it found. If multiple foreground pixels are equidistant from P, then the average value among them can be used.
Are there any existing tools that will do this operation efficiently?

採用された回答

Sean de Wolski
Sean de Wolski 2021 年 9 月 30 日
Hi Matt,
The second output from bwdist is the index of the closest pixel. I think this is all you need to then index back.
[~,idx] = bwdist([0 0 1 1 0 0 0])
idx = 1×7
3 3 3 4 4 4 4
Not sure how this would handle ties though.
  1 件のコメント
Matt J
Matt J 2021 年 9 月 30 日
編集済み: Matt J 2021 年 9 月 30 日
Thanks, Sean! Easier than I thought.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImages についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by