8 views (last 30 days)

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.

Walter Roberson
on 17 Oct 2019

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.

Shivam Prasad
on 18 Oct 2019

Hi Waseem,

Check if this works. I have made some changes to your code.

array=[1.0,5.0,25.0,27.0,35.0,38.0,41.0,50.0];

matchingpixels= zeros(length(array));

thresholddis=4.0;

for i=1:length(array)

fprintf('Pixels found closer to %i =',i);

currentPos=array(i);

sum=0;

for j=1:length(array)

if j~=i

otherPos=array(j);

if abs(currentPos-otherPos)<thresholddis

fprintf('%i ',j);

sum = sum+otherPos;

end

end

end

fprintf('\nSum of pixel values = %i\n',sum);

end

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.

Opportunities for recent engineering grads.

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

Start Hunting!
## 3 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485249-how-to-sum-all-pixel-within-a-specific-distance-from-each-pixel#comment_756432

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485249-how-to-sum-all-pixel-within-a-specific-distance-from-each-pixel#comment_756432

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485249-how-to-sum-all-pixel-within-a-specific-distance-from-each-pixel#comment_757194

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485249-how-to-sum-all-pixel-within-a-specific-distance-from-each-pixel#comment_757194

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485249-how-to-sum-all-pixel-within-a-specific-distance-from-each-pixel#comment_757223

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/485249-how-to-sum-all-pixel-within-a-specific-distance-from-each-pixel#comment_757223

Sign in to comment.