Replace repetitive values in a matrix with another value?
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jessica
el 29 de Jun. de 2012
Comentada: JL
el 23 de Jul. de 2019
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 comentario
Ryan
el 2 de Jul. de 2012
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)?
Respuesta aceptada
Sean de Wolski
el 2 de Jul. de 2012
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 comentarios
Más respuestas (3)
Walter Roberson
el 29 de Jun. de 2012
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.
0 comentarios
Jessica
el 2 de Jul. de 2012
Jessica
el 2 de Jul. de 2012
Editada: Jessica
el 2 de Jul. de 2012
2 comentarios
Sean de Wolski
el 2 de Jul. de 2012
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
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!