Neighbors of a pixel
39 次查看(过去 30 天)
显示 更早的评论
I want to calculate the mean of the neighbors of the pixels in an image. I want to do it for all pixels not only the internal ones. That means for example that I want to calculate the mean of neighbors of the pixels (1,1) or (1,size(image,2)) or (size(image,1),size(image,2)) which means I cannot use a matrix divided by 8 as a kernel for all these pixels because for example neighbors of (1,1) are only 3 not 8. Does anyone have an idea how to do it without using 8 ifs?
0 个评论
采纳的回答
Johannes Korsawe
2016-10-14
Let A be your matrix.
% use the help of a bigger matrix
B=nan(size(A)+2);
B(2:end-1,2:end-1)=A;
% pre-define memory for result
result = 0*A;
% calculate!
for i=2:size(A,1)+1,
for j=2:size(A,2)+1,
tmp=B(i-1:i+1,j-1:j+1);
tmp(2,2)=nan;
result(i-1,j-1)=mean(tmp(~isnan(tmp)));
end
end
更多回答(3 个)
Guillaume
2016-10-14
Well, you can certainly use a convolution for the central part. I would just use smaller convolution kernels for the edges so:
img = reshape(1:200, 10, 20); %demo image
meanimg = [mean2(img(1:2, 1:2)), conv2(img(1:2, :), ones(2,3)/6, 'valid'), mean2(img(1:2, end-1:end)); ...
conv2(img(:, 1:2), ones(3,2)/6, 'valid'), conv2(img, ones(3)/9, 'valid'), conv2(img(:, end-1:end), ones(3,2)/6, 'valid'); ...
mean2(img(end-1:end, 1:2)), conv2(img(end-1:end, :), ones(2,3)/6, 'valid'), mean2(img(end-1:end, end-1:end))]
That is one convolution for the central part, 1 convolution for each edge and just mean2 for each corner.
0 个评论
Image Analyst
2016-10-14
I'd do it a different way. I'd do a full convolution so that I can get the sums and pixel counts at each window location. Then I'd crop off the outer layer (to give an output of the same size as the original) and finally divide them. Here's my demo, with extensive comments.
% Read in sample image.
grayImage = imread('cameraman.tif');
% Make an image of 1's so we can count how many
% neighbors there are at each pixel location.
binaryImage = ones(size(grayImage));
% Define a kernel to do the summing of the images at each location.
kernel = ones(3);
% Get sum of gray levels at each window location.
% Use 'full' option so we can let the window slide out and count neighbors of edge pixels.
sumImage = conv2(double(grayImage), kernel, 'full');
% Count the pixels at each window location.
countImage = conv2(double(binaryImage), kernel, 'full');
% Get the mean by dividing the sum by the pixel count.
% but ignore the outer 1-pixel-wide layer.
meanImage = sumImage(2:end-1, 2:end-1) ./ countImage(2:end-1, 2:end-1);
Don't be afraid - the actual code is only 5 lines long.
0 个评论
Rose Mahmudi
2019-4-15
hello guys
I need help with the same question but a little diffrent.
I want to obtain all 8 neighborhood connectivity for each pixles in an image.
so after i read the image and convert it to gray level image what can I do for obtaining 8neighbor-c???
and I have another problem ... I want to use first row as neighbor for the last row and vice versa. also I want to do same for columns.
could you help me figure out pleaseeeee.
thank you very much
8 个评论
Image Analyst
2019-4-27
I don't know. I don't have the Fuxxy toolbox and never run evalfis(). Good luck though.
Rose Mahmudi
2019-4-28
thank you very much for your code and your help. I'll try to figure it out some how.
:) best regards
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Data Workflows 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!