How can I randomly choose a neighbouring pixel from an Image..
Show older comments
Hello Every one
How can I randomly choose the pixels value from an image.Any help is appreciated.
Thanks
Accepted Answer
More Answers (1)
Walter Roberson
on 22 Jun 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 Comments
Algorithms Analyst
on 22 Jun 2013
Edited: Walter Roberson
on 22 Jun 2013
Walter Roberson
on 22 Jun 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
on 22 Jun 2013
Walter Roberson
on 22 Jun 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
on 27 Jun 2013
GOVARDHAN
on 21 Apr 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
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!