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

3 次查看(过去 30 天)
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.
  3 个评论
Shivam Prasad
Shivam Prasad 2019-10-17
编辑:Shivam Prasad 2019-10-17
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 ."

请先登录,再进行评论。

回答(2 个)

Walter Roberson
Walter Roberson 2019-10-17
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 个评论
Shivam Prasad
Shivam Prasad 2019-10-18
编辑:Shivam Prasad 2019-10-18
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
Shivam Prasad 2019-10-18
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
  2 个评论
Shivam Prasad
Shivam Prasad 2019-10-21
编辑:Shivam Prasad 2019-11-5
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Geometric Transformation and Image Registration 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by