finding 4 neighbor of pixel in grayscale image.

조회 수: 4 (최근 30일)
ahmed khaldoon alzubaidi
ahmed khaldoon alzubaidi 2024년 6월 2일
편집: DGM 2024년 6월 2일
i am trying to find the 4 neighbor of pixel x0,y0 in grayscale image but i getting empty x,y . her is the code i am using
function [x,y]= neighbours(image,x0,y0,thres)
image=double(image);
interest = image(x0,y0);
pixel =zeros(size(image));
pixel(x0,y0)=1;
x=[];
y=[];
up= image(conv2(pixel,[0 1 0; 0 0 0; 0 0 0],'same')>0);
if abs(up- interest)<=thres
if isempty(up)~=1
x(1)=x0;
y(1)=y0-1;
end
end
down= image(conv2(pixel,[0 0 0; 0 0 0; 0 1 0],'same')>0);
if abs(down- interest)<=thres
if isempty(down)~=1
x(2)=x0;
y(2)=y0+1;
end
end
left= image(conv2(pixel,[0 0 0; 1 0 0; 0 0 0],'same')>0);
if abs(left- interest)<=thres
if isempty(left)~=1
x(3)=x0-1;
y(3)=y0;
end
end
right= image(conv2(pixel,[0 0 0; 0 0 1; 0 0 0],'same')>0);
if abs(right- interest)<=thres
if isempty(right)~=1
x(4)=x0+1;
y(4)=y0;
end
end
end

채택된 답변

DGM
DGM 2024년 6월 2일
편집: DGM 2024년 6월 2일
Disregarding edges, the 4-neighbors of a pixel IM(y,x) are obviously
%IM(y,x+[-1 1]) % horizontally-adjacent pair
%IM(y+[-1 1],x) % vertically-adjacent pair
... but your code is doing a bunch of unrelated and apparently redundant things, so if you're not asking for the obvious answer, what are you actually asking?
It's fairly clear that x0 and y0 are expected to be scalars, based on the use of subscript indexing, so why is it necessary to perform four convolutions of the entire image instead of doing rudimentary addressing of the array?
Assuming that you expect the output to be fixed length, then:
% inputs
inpict = imread('cameraman.tif');
x0 = 128;
y0 = 128;
threshold = 128;
[x y] = neighbours(inpict,x0,y0,threshold)
x = 4x1
128 128 NaN NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
y = 4x1
129 127 NaN NaN
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
function [xs,ys] = neighbours(inpict,x0,y0,threshold)
% blah blah blah write a synopsis here
%
% INPICT is expected to be single-channel
% X0,Y0 must be scalar integers
% THRESHOLD must be scaled appropriate for the class of INPICT
%
% Outputs are fixed-length subscript vectors corresponding to the
% [N; S; E; W] neighbors of INPICT(X0,Y0). Neighbors which are
% outside the image page geometry or have an absolute intensity
% difference greater than THRESHOLD are filled with NaN.
% these must be integers
x0 = round(x0);
y0 = round(y0);
% check inputs
sz = size(inpict,1:3);
if sz(3) ~= 1
error('expected INPICT to be a single-channel image')
end
if x0 < 1 || x0 > sz(2)
error('X0 must lie within the image geometry')
end
if y0 < 1 || y0 > sz(1)
error('Y0 must lie within the image geometry')
end
if ~isscalar(x0) || ~isscalar(y0)
error('X0 and Y0 must be scalars')
end
% expand to candidate sample positions
xs = [x0; x0; x0 + [1; -1]]; % [N; S; E; W]
ys = [y0 + [1; -1]; y0; y0];
% map valid candidates
validss = ys >= 1 & ys <= sz(1) & xs >= 1 & xs <= sz(2);
% build index list
idx = sub2ind(sz(1:2),ys(validss),xs(validss));
% find neighbors which are similar in intensity
dI = double(inpict(idx)) - double(inpict(y0,x0));
similarintensity = abs(dI) < threshold;
% combine the two masks
% this is the map of valid subscripts which have a similar pixel intensity
validss(validss) = validss(validss) & similarintensity;
% replace invalid coordinates with NaN?
xs(~validss) = NaN;
ys(~validss) = NaN;
end
My guess is that you were ignoring the scale of your input image when you selected your threshold value.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by