Asked by Algorithms Analyst
on 22 Jun 2013

Hello Every one

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

Thanks

Answer by Image Analyst
on 22 Jun 2013

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?

Show 16 older comments

Image Analyst
on 27 Jun 2013

Perhaps you mean PSNR: http://en.wikipedia.org/wiki/PSNR If so, I have a demo for that, just ask.

Algorithms Analyst
on 29 Jun 2013

please send link for the demo..

Image Analyst
on 29 Jun 2013

% 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);

Log in to comment.

Answer by 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

Show 3 older comments

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

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.

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

Log in to comment.

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
## 0 Comments

Log in to comment.