Matrix convolution only on some elements

7 ビュー (過去 30 日間)
KRISTIAN MARCON
KRISTIAN MARCON 2022 年 9 月 5 日
コメント済み: KRISTIAN MARCON 2022 年 9 月 6 日
Hi, I'm a junior with matlab experience. I tried to search whatever like my problem on internet without find any answer. I have to do a convolution on a matrix A using conv2 but only if the central element (overlaped on the mask) is 1
For example
A=[ 1 0 1 0 ; 0 1 0 0 ; 1 0 0 0 ; 0 1 0 0]
F=[1 1 1; 1 0 1; 1 1 1]
The convolution conv2(A,F,'same') have to been made only if the central element is 1 (like 1', 3', 5',... elements) skipping the convolution if is 0. Obvious i can accept any other functions like conv2 to do the convolution. Thanks if everybody can help me

採用された回答

Image Analyst
Image Analyst 2022 年 9 月 5 日
Try this:
A=[ 1 0 1 0 ; 0 1 0 0 ; 1 0 0 0 ; 0 1 0 0]
A = 4×4
1 0 1 0 0 1 0 0 1 0 0 0 0 1 0 0
F=[1 1 1; 1 0 1; 1 1 1]
F = 3×3
1 1 1 1 0 1 1 1 1
% Do the complete convolution.
AF = conv2(A, F, 'same')
AF = 4×4
1 3 1 1 3 3 2 1 2 3 2 0 2 1 1 0
% Since we want the convolution ONLY where A == 1,
% we can replace ONLY those places with AF
Aout = A; % Initialize
mask = A == 1
mask = 4×4 logical array
1 0 1 0 0 1 0 0 1 0 0 0 0 1 0 0
Aout(mask) = AF(mask) % Replace with AF but only where A is 1.
Aout = 4×4
1 0 1 0 0 3 0 0 2 0 0 0 0 1 0 0
  1 件のコメント
KRISTIAN MARCON
KRISTIAN MARCON 2022 年 9 月 6 日
Thank you! It's exactly what I need

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by