How can I randomly choose a neighbouring pixel from an Image..
Mostrar comentarios más antiguos
Hello Every one
How can I randomly choose the pixels value from an image.Any help is appreciated.
Thanks
Respuesta aceptada
Más respuestas (1)
Walter Roberson
el 22 de Jun. de 2013
switch randi(8,1,1)
case 1: rpix = YourImage(i-1,j-1);
case 2: rpix = YourImage(i-1,j);
case 3: rpix = YourImage(i-1,j+1);
case 4: rpix = YourImage(i,j-1);
case 5: rpix = YourImage(i,j+1); %skip i,j as that is the pixel itself
case 6: rpix = YourImage(i+1,j-1);
case 7: rpix = YourImage(i+1,j);
case 8: rpix = YourImage(i+1,j+1);
end
6 comentarios
Algorithms Analyst
el 22 de Jun. de 2013
Editada: Walter Roberson
el 22 de Jun. de 2013
Walter Roberson
el 22 de Jun. de 2013
You would need to use RPIX(i,j) instead of RPIX(i'j)
The code you show should work, but it will not choose a neighbor at random.
Algorithms Analyst
el 22 de Jun. de 2013
Walter Roberson
el 22 de Jun. de 2013
Img = imread('g.bmp');
Rpix = zeros(size(img));
[m n] = size(img)
for i = 2:m-1
for j = 2:n-1
switch randi(8,1,1)
case 1: rpix = YourImage(i-1,j-1);
case 2: rpix = YourImage(i-1,j);
case 3: rpix = YourImage(i-1,j+1);
case 4: rpix = YourImage(i,j-1);
case 5: rpix = YourImage(i,j+1); %skip i,j as that is the pixel itself
case 6: rpix = YourImage(i+1,j-1);
case 7: rpix = YourImage(i+1,j);
case 8: rpix = YourImage(i+1,j+1);
end
RPIX(i,j) = rpix;
end
end
Caution: the code would need to be changed if g.bmp is an RGB image.
Algorithms Analyst
el 27 de Jun. de 2013
GOVARDHAN
el 21 de Abr. de 2014
Delete colon from ur code and run it.
clc close all clear all img = imread('brain1.jpg'); img=rgb2gray(img); Rpix = zeros(size(img)); [m n] = size(img); for i = 2:m-1 for j = 2:n-1 switch (randi(8,1,1)) case 1 rpix =img(i-1,j-1); case 2 rpix =img(i-1,j); case 3 rpix = img(i-1,j+1); case 4 rpix = img(i,j-1); case 5 rpix = img(i,j+1); %skip i,j as that is the pixel itself case 6 rpix = img(i+1,j-1); case 7 rpix = img(i+1,j); case 8 rpix = img(i+1,j+1); end Rpix(i,j) = rpix end
Categorías
Más información sobre Image Arithmetic en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!