Replace repetitive values in a matrix with another value?

7 ビュー (過去 30 日間)
Jessica
Jessica 2012 年 6 月 29 日
コメント済み: JL 2019 年 7 月 23 日
Hello there,
I'm new to Matlab so hopefully I can attempt to explain this! I have an image that has some interference that I am looking to get rid of. I have displayed each pixel in the image as a value from 0 to 255 in a matrix. The interference is in the form of repetitive values. So for example (and simplicity sake) one row of the matrix may look like:
1 2 3 3 3 3 3 4 5 3 3 3 6 3 7
Where the interference would be the string of 3 3 3 3 3 and 3 3 3 and the single 3 there would be valid.
What I would like to do is replace the repetitive values in the entire matrix with a nearest neighbor value say median of the pixel above and below and if that's not possible maybe just NAN. Would anyone know how to do this? Thanks!
  1 件のコメント
Ryan
Ryan 2012 年 7 月 2 日
is the repetitive value known? is the repetitive value always the same for a given image (e.g. will the repeated value in the image always be a 3)?

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

採用された回答

Sean de Wolski
Sean de Wolski 2012 年 7 月 2 日
x = [1 2 3 3 3 3 3 4 5 3 3 3 6 3 7] data
idxrep = ~diff([-inf,x]) %pad with -inf and identify areas of zero change (~)
x(idxrep)=nan; %replace with NaN, or whatever
If you chose the NaN route and want an awesome way to fill them:
y = inpaint_nans(x)
  2 件のコメント
Andrei Bobrov
Andrei Bobrov 2012 年 7 月 2 日
x([false,diff(x)==0]) = nan;
JL
JL 2019 年 7 月 23 日
Hi, would it be possible to for this code to work on have more rows?

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

その他の回答 (3 件)

Walter Roberson
Walter Roberson 2012 年 6 月 29 日
Convert the vector to double-precision data type (needed because integers cannot represent NaN.) diff() it. Replace the places the diff() was 0 with NaN.
Step after that: consider using the File Exchange inpaint_nans

Jessica
Jessica 2012 年 7 月 2 日
The problem that I can see with diff() is that it reduces the number of elements in each row by 1 because it takes the difference between adjacent values. I do not want the matrix size to change at all because it is an image I wish to analyze afterwards and pixel position is important.
  2 件のコメント
Sean de Wolski
Sean de Wolski 2012 年 7 月 2 日
Pad with -inf
diff([-inf,x])
Then what Walter said.
Jessica
Jessica 2012 年 7 月 2 日
Sorry! I'm new to Matlab so what exactly would that do? Thanks for the responses!

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


Jessica
Jessica 2012 年 7 月 2 日
編集済み: Jessica 2012 年 7 月 2 日
So I have
imgname = 'my tif file path'
imgmat = imread(imgname);
idxrep = ~diff([-inf,imgmat])
imgmat(idxrep) = NaN
and I get
Error using horzcat CAT arguments dimensions are not consistent.
Error in test (line 6) idxrep = ~diff([-inf,xd])
  2 件のコメント
Sean de Wolski
Sean de Wolski 2012 年 7 月 2 日
Well, yes. My example was for a row vector. For a matrix you'll have to do a little more work. Specifically you will have to add a column vector to the front of the matrix (rather than a scalar) (this is the cause of the HORZCAT error). Second, you will have to specify which dimension you want to differentiate across.
x = repmat([1 2 3 3 3 3 3 4 5 3 3 3 6 3 7],10,1);
idxrep = [false(size(x,1),1) ~diff(x,1,2)]
x(idxrep) = NaN
Jessica
Jessica 2012 年 7 月 2 日
Ahh! Gotcha! Thanks! And thanks for the inpaint_nan link everyone! That was extremely helpful!

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by