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!