How to find guassian gradient of a matrix with many Nan values
조회 수: 3 (최근 30일)
이전 댓글 표시
I have a matrix with 40 x 60 pixels with many Nan values ,When I applied guassian gradient ,result is Nan matrix.
GHow to ignore Nan values for applying guassian gradient.
댓글 수: 0
답변 (1개)
Image Analyst
2019년 11월 29일
I'd use a modified median filter. So
% Set all nan values to inf, take the median filter of the array.
nanIndexes = isnan(m);
m(nanIndexes) = inf;
mfm = medfilt2(m, [3,3]); % Make window big enough to cover any nan regions.
% Then replace nan's in the original matrix with the median filtered values:
m(nanIndexes) = mfm(nanIndexes)
Then do your gradient operation, like with imgradient() or whatever. Try that and see if it works. If it doesn't, attach your data and code.
댓글 수: 3
Image Analyst
2019년 12월 3일
I don't know how. m and nanIndexes are both 2-D matrices, not vectors of zero, as long as there are nan's in the m matrix. Here's a small example
m = magic(7);
m(4,4) = nan % Set the middle pixel to nan.
% Set all nan values to inf, take the median filter of the array.
nanIndexes = isnan(m)
m(nanIndexes) = inf
mfm = medfilt2(m, [3,3]) % Make window big enough to cover any nan regions.
% Then replace nan's in the original matrix with the median filtered values:
m(nanIndexes) = mfm(nanIndexes)
First you'll see
m =
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 NaN 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
then after repair you'll see
m =
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 26 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
Save your original matrix in a .mat file
save('answers.mat', 'm');
then attach here with the paper clip icon.
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!