Write the function to return the x and y coordinates of the input pixel’s connected neighbours, i.e., the x and y coordinates of all pixels in the set c(p) for the given P.

24 次查看(过去 30 天)
I = imread('F:\IMAGE SYSTEMS\LAB 2\EEET2169-Lab2-main\images\iceberg.png');
J = double(I);
% Pixel of interest
poi = [190 180];
x0 = 190;
y0 = 180;
% n - neighbouring pixels
n1 = ([x0+1 y0]);
n2 = ([x0-1 y0]);
n3 = ([x0 y0+1]);
n4 = ([x0 y0-1]);
% v - intensity of the poi
vpoi = J(poi(1),poi(2));
v1 = J(n1(1),n1(2));
v2 = J(n2(1),n2(2));
v3 = J(n3(1),n3(2));
v4 = J(n4(1),n4(2));
% a - absolute value
a1 = abs(v1-vpoi);
a2 = abs(v2-vpoi);
a3 = abs(v3-vpoi);
a4 = abs(v4-vpoi);
I don't know how to proceed after this. Anyone please help me with this!

回答(1 个)

Suraj Kumar
Suraj Kumar 2025-2-25
You were on the right track to find the x and y coordinates of all connected neighbors of a specific pixel in an image. I've made a few enhancements to the script to improve its functionality:
1. I've introduced a threshold to determine when the intensity differences between the pixel of interest and its neighbors are small enough to consider them connected. This helps in accurately identifying connected pixels based on intensity similarity.
2. I've added boundary checks to ensure that the neighbor pixel indices are within the bounds of the image. This step is crucial for preventing errors when accessing pixel values, especially at the edges of the image.
You can refer to the attached code snippet below for a clearer understanding:
function connectedNeighbors = findConnectedNeighbors(imagePath, pixelCoords, threshold)
I = imread(imagePath);
J = double(I);
x0 = pixelCoords(1);
y0 = pixelCoords(2);
vpoi = J(x0, y0);
neighbors = [
x0+1, y0;
x0-1, y0;
x0, y0+1;
x0, y0-1
];
connectedNeighbors = [];
for i = 1:size(neighbors, 1)
xn = neighbors(i, 1);
yn = neighbors(i, 2);
if xn > 0 && xn <= size(J, 1) && yn > 0 && yn <= size(J, 2)
vn = J(xn, yn);
a = abs(vn - vpoi);
if a <= threshold
connectedNeighbors = [connectedNeighbors; xn, yn];
end
end
end
end
Hope this works for you!

类别

Help CenterFile Exchange 中查找有关 Image Segmentation and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by