MATLAB Answers

how to sum all pixel within a specific distance from each pixel?

8 views (last 30 days)
Waseem Farooq
Waseem Farooq on 14 Oct 2019
Edited: Shivam Prasad on 5 Nov 2019
Hello everyone, I am trying this concept that the code goes through each pixel of an image and sum all the pixel values(to take a mean value then) in a specific distance (2lambda where lambda = 4) from that pixel. this should be done for all the pixels .
Thank you everyone for your response. let me explain it abit.
img_out(:,:,n) = conv2(img_in, gb,'same');
img_out is the one i want to work with now. i am gonna write some comments about what i want to achieve now.
% calculate the number of pixels in distance limit of 2 lambda to 12 lambbda(lammba is the wavelength of gabor filter but for simplicity here just consider it 4)
% to explain the above comment a bit more i have to start from pixel 1 and will go through all the pixels till the end.
e-g for pixel 1, pixel 1 will be the target pixel and we will have to sum all the pixel values of the neighbouring pixels in the distance of 2lambda to 12 lambda.


Shivam Prasad
Shivam Prasad on 17 Oct 2019
Hi Waseem,
Can you tell me what you mean by :-
"then) in a specific distance (2lambda where lambda = 4) from that pixel. this should be done for all the pixels ."
KALYAN ACHARJYA on 17 Oct 2019
"... in a specific distance"
Distance from?? each pixels ..

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 17 Oct 2019
conv2 with a mask that is 1 for each pixel to be considered. The exact pattern will depend on which distance measurement you want to use. See strel() to construct some of the common ones.

  1 Comment

Shivam Prasad
Shivam Prasad on 18 Oct 2019
Hi Waseem,
I understand that the value of lambda remains the same throughout the operation. However, the size of a pixel is not fixed. It is dependent on resolution of the output screen.
Can you change the distance metric (pixels) to something else?
Also, please specify the unit of lambda.

Sign in to comment.

Shivam Prasad
Shivam Prasad on 18 Oct 2019
Hi Waseem,
Check if this works. I have made some changes to your code.
matchingpixels= zeros(length(array));
for i=1:length(array)
fprintf('Pixels found closer to %i =',i);
for j=1:length(array)
if j~=i
if abs(currentPos-otherPos)<thresholddis
fprintf('%i ',j);
sum = sum+otherPos;
fprintf('\nSum of pixel values = %i\n',sum);


Shivam Prasad
Shivam Prasad on 21 Oct 2019
It's difficult to understand the code at a go. There is no reference to variables like tEnd. I suggest you to go through the logic which I have implemented in the sample code and make changes in your real code accordingly.
The idea is to use a nested for loop. Both the loops will iterate through all the neutrons. Calculate the distance between the neurons from the outer and inner loop. If this distance is between 2 and 12 lambda, output these neurons, and also add this distance in a variable SUM initialized to zero just before the inner loop starts. Output the value of the variable SUM outside the inner loop.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!