How to find out windowwise mean and standard deviation of some particular pixels.

I have a .txt file with 50 rows and 2 columns indicating 50 pixel positions in an image.I am reading that file with the following statement:
file_id=fopen('C:\Users\Mahua Nandy\PixelPositions_im1.txt','r'); Input=fscanf(file_id,'%d',[2,50]); fclose(file_id);
Input is coming in column order(2 rows and 50 columns).Now I want to find out 3x3 windowwise mean and standard deviation of those particular 50 pixels.
Please help me to code it

Réponses (2)

First I guess you have to create an image - all black I guess.
maxRows = max(Input(1,:));
maxColumns = max(Input(2, :));
grayImage = zeros(maxrows, maxColumns, 'uint8');
Then you have to run down your list of coordinates, figuring out their gray level (from some other image I assume), then assigning/transferring them to the new image (the one that contains only the 50 pixels):
for c = 1 : maxColumns
for r = 1 : maxRows
row = Input(1, r);
column = Input(2, c);
% Transfer the gray level from the other image to
% this location on the new image.
grayImage(row, column) = initialImage(row, column);
end
end
Then you have to use conv2() and stdfilt() to take the mean and std dev in a 3x3 window in the new image which contains the 50 pixels.
meanImage = conv2(grayImage, ones(3)/9, 'same');
stdImage = stdfilt(grayImage);
Be aware though that most of the image will be just black pixels so the mean will be very low. Upload your image first, if you have any follow up questions.

6 commentaires

Well, did this work for you? Are you going to respond?
Thank you vry much for the reply.But still I can not figure out my solutions.Please help. Following are the 50 different pixels of my image.I want to find out mean and std deviation of each pixel considering its 3x3 neighborhood in the original image.
28 16
28 17
28 18
28 27
28 28
30 57
30 58
30 59
30 60
30 61
31 56
31 57
31 58
31 59
31 60
31 115
31 116
31 117
31 118
31 119
32 39
32 40
32 41
32 42
32 43
33 28
33 29
33 30
33 31
33 32
33 118
33 119
33 120
33 121
33 122
34 39
34 40
34 41
34 42
34 43
35 102
35 103
35 104
35 105
35 106
37 74
37 75
37 76
37 77
37 78
38 38
38 39
38 40
38 41
38 42
111 131
111 132
111 133
111 134
111 135
% Gets mean and std dev of an image around 50 isolated coordinates.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear all; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
format compact;
Input = [...
28 16
28 17
28 18
28 27
28 28
30 57
30 58
30 59
30 60
30 61
31 56
31 57
31 58
31 59
31 60
31 115
31 116
31 117
31 118
31 119
32 39
32 40
32 41
32 42
32 43
33 28
33 29
33 30
33 31
33 32
33 118
33 119
33 120
33 121
33 122
34 39
34 40
34 41
34 42
34 43
35 102
35 103
35 104
35 105
35 106
37 74
37 75
37 76
37 77
37 78
38 38
38 39
38 40
38 41
38 42
111 131
111 132
111 133
111 134
111 135]
initialImage = imread('moon.tif');
maxRows = max(Input(:, 2)); % Y value
maxColumns = max(Input(:, 1)); % x value.
grayImage = zeros(maxRows, maxColumns, 'uint8');
numberOfCoordinates = size(Input, 1);
for c = 1 : numberOfCoordinates
for r = 1 : numberOfCoordinates
row = Input(r, 1);
column = Input(c, 2);
% Transfer the gray level from the other image to
% this location on the new image.
grayImage(row, column) = initialImage(row, column);
end
end
meanImage = conv2(double(grayImage), ones(3)/9, 'same');
subplot(1, 2, 1);
imshow(meanImage, []);
title('Mean Image', 'FontSize', fontSize);
stdImage = stdfilt(grayImage);
subplot(1, 2, 2);
imshow(stdImage, []);
title('Std Dev Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
thank you very much but I need 3x3 windowwise mean and standard deviation of those particular pixels where the 3x3 window is the window of original image not new image.Thank you once again for the answer because I think the rest part I can manage.
That's what conv2 does. And what exactly is in the space between your 50 isolated pixels? In an image, there has to be SOMETHING.

Connectez-vous pour commenter.

Thorsten
Thorsten le 19 Fév 2013
Modifié(e) : Thorsten le 19 Fév 2013
Hi Mahua Nandy(Pal), that's all quite straight forward:
1. Let's say you have your image in I and your particular positions in variables x and y
I = imread('cameraman.tif');
x = 10; y = 20; % just for testing
To get the 3 x 3 neighborhood N around (x, y) from your image, use
N = I(y-1:y+1, x-1:x+1);
That gives you a 3 x 3 matrix. The mean and std are
m = mean(N(:));
s = std(N(:));
If you have 50 values of x and y stored in Input, you just do it in a loop:
for i=1:size(Input, 1)
x = Input(i, 1);
y = Input(i, 2);
N = I(y-1:y+1, x-1:x+1);
m(i) = mean(N(:));
s(i) = std(N(:));
end
Done!
Two caveats:
1. verify that each row of Input is [x y] and not [y x]; if the columns are given as y x use
x(i) = Input(i, 2);
y(i) = Input(i, 1);
2. ensure that no x, y lies at the boundary of I, such that x-1, x+1m, y-1 and y+1 is always a valid index to the image. To be one the save side, you could use the statement
N = I(max(1, y-1):min(size(I, 1), y+1), ...
max(1, x-1):min(size(I, 2), x+1);

Community Treasure Hunt

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

Start Hunting!

Translated by