How can I randomly choose a neighbouring pixel from an Image..

Hello Every one
How can I randomly choose the pixels value from an image.Any help is appreciated.
Thanks

 Accepted Answer

This is unclear. An image does not have a neighboring pixel. A pixel in an image will have 8 neighbors, but the image as a whole does not.
And your subject (whatever it means) does not match the body of your question. "randomly choose the pixels value" is not grammatically correct. It makes a difference if "pixels" is plural, or if you meant "pixel's" meaning possessive.
You might mean that pixels have values from 0 to 255 and that you want to choose one of those integer values. For example, return 169 regardless of whether any particular pixel in the image has that value.
Or you might mean that given some randomly chosen set of pixels, extract one value from the set of values that those pixels have. For example, you chose 3 pixels with the values 30, 100, and 142 and you choose 142 at random from those 3 values.
Or maybe you mean some kind of combination of your subject line and one interpretation of your body. You might mean that you choose some number of pixels at random locations (say it's 3) and you want all of the values that those pixels have. For example you return 30, 100, 142 (all of them, not just one of them).
Perhaps you can use randperm() to select pixel locations:
randomPixelIndexes = randperm(numel(yourImage), numberOfPixelsToReturn);
randomPixelValues = yourImage(randomPixelIndexes);
Why don't you share the intent of this process so we can see if this is even the best approach? Or post your image and tell us what you want to measure or do with it?

19 Comments

I suspect that there are algorithms which involve replacing each (or selected) pixel with a random connected neighbor of the pixels. A dithering algorithm perhaps. Or some kind of comparison with replacing pixels with the median of neighbors or the min or max of the neighbors.
Actually I am applying Pixel based Adaptive Segmenter in Matlab. The algorithm is already been implemented in C++ but I want to implement it in matlab.In their paper http://www.mmk.e-technik.tu-muenchen.de/publ/pdf/12/12hof2.pdf in Background Update step (3.2) they have mentioned that they have also updated the randomly choosen neighbouring pixel yi∈ N(xi). So thats why I want to find the neighbouring pixels from image.
Ah, updating a randomly chosen neighbouring pixel would be the opposite of the code I showed above. Instead of code such as
case 1: rpix = YourImage(i-1,j-1);
you would use
case 1: YourImage(i-1,j-1) = rpix;
where rpix had already been assigned the new value.
There are, of course, more efficient methods if you are vectorizing.
can you please how me the other methods?
it would be more appreciated...
and this code is not working..I am gettting error
clc
close all
clear all
Img = imread('g.bmp');
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 = 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
there is syntax error which is"expression to the left of the equal sign is not a valid target for an alignment"...
You have multiple errors there, none of which did you give the full error message for.
Since YourImage is not yet defined, perhaps you meant:
YourImage = imread('g.bmp');
[m, n, numberOfColorChannels] = size(YourImage);
if numberOfColorChannels > 1
YourImage =rgb2gray(YourImage);
end
Rpix = zeros(m, n, 'uint8');
Next, after the for loops, RPIX is undefined. Use Rpix instead. MATLAB is case sensitive so RPIX is different than Rpix which are both different than rpix. I really suggest you use more descriptive variable names rather than spell them the same and depend on capitalization to distinguish them, which, as you're experiencing, can lead to errors. Here's the fix:
Rpix (i,j) = rpix;
Which line is the error given against?
You appear to have used "img" and "Img" for the same array, one of them with lower-case-I and the other with upper-case-I
not gettting the point.Can you please provide some source code about how can i update the background and its neighbouring pixels.let say
I have a background image bg bg=imrea('s.bmp'); update............
Thanks
I'm not sure what point you're not getting. I already gave you some source code to replace your bad code. Now you have a background image. What does this have to do with the original image. Is it a different image, so that now you have two images: a signal image and a background image?
Thanks Mr.Image Anlyst
I have two images one input(contains some object) and second is background image(static image which contains nothing).Now I want to update my Background image using the equation presented on paper and also want to update random choosen neighbouring pixels from background image.How can I achieve this..
Thanks
In your code (numberOfPixelsToReturn) is a constant number how many pixels can i choose.
That's not applicable anymore. That was when we thought you were taking pixels from randomly around the image, not from among the 8 neighbors of each pixel.
So should I close this question.......?
If you have no other questions, then please "Accept" one of the "Answers." Otherwise, continue asking questions here.
How can I find the minimum distance minimum two images of same size
img1=double(rgb2gray(imread('l.bmp')));
img2=double(rgb2gray((imread('j.bmp')));
[m n]=size(img1);
Mindist=zeros(m,n);
for i=1:m
for j=1:n
Mindist(i,j)=sqrt(img1(i,j)-img2(i,j));
end
end Can I say that this is the minimum distance between two images. Thanks.
Some of the results of subtraction will be negative. Are you intending that the sqrt() will produce imaginary numbers there?
Perhaps you mean PSNR: http://en.wikipedia.org/wiki/PSNR If so, I have a demo for that, just ask.
% Demo to calculate PSNR of a gray scale image.
% http://en.wikipedia.org/wiki/PSNR
% Clean up.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%------ GET DEMO IMAGES ----------------------------------------------------------
% Read in a standard MATLAB gray scale demo image.
grayImage = imread('cameraman.tif');
[rows columns] = size(grayImage);
% Display the first image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Gray Scale Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Get a second image by adding noise to the first image.
noisyImage = imnoise(grayImage, 'gaussian', 0, 0.003);
% Display the second image.
subplot(2, 2, 2);
imshow(noisyImage, []);
title('Noisy Image', 'FontSize', fontSize);
%------ PSNR CALCULATION ----------------------------------------------------------
% Now we have our two images and we can calculate the PSNR.
% First, calculate the "square error" image.
% Make sure they're cast to floating point so that we can get negative differences.
% Otherwise two uint8's that should subtract to give a negative number
% would get clipped to zero and not be negative.
squaredErrorImage = (double(grayImage) - double(noisyImage)) .^ 2;
% Display the squared error image.
subplot(2, 2, 3);
imshow(squaredErrorImage, []);
title('Squared Error Image', 'FontSize', fontSize);
% Sum the Squared Image and divide by the number of elements
% to get the Mean Squared Error. It will be a scalar (a single number).
mse = sum(sum(squaredErrorImage)) / (rows * columns);
% Calculate PSNR (Peak Signal to Noise Ratio) from the MSE according to the formula.
PSNR = 10 * log10( 256^2 / mse);
% Alert user of the answer.
message = sprintf('The mean square error is %.2f.\nThe PSNR = %.2f', mse, PSNR);
msgbox(message);

Sign in to comment.

More Answers (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

6 Comments

So can I say that let suppose I have an image
Img=imread('g.bmp');
Rpix=zeros(size(img));
[m n]=size(img)
For i=1:m-1
For j=1:n-1
RPIX(i'j)=img(i+1,j+1);
End
End
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.
So how can I choose random neighbors pixel?thanks for your help
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.
This code is giving me an error.it is not executing the matlab error is
clc
close all
clear all
img = imread('lena.bmp');
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
end
Error: File: NeighbouringPixels.m Line: 12 Column: 18
The expression to the left of the equals sign is not a valid target for an assignment.
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

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!