Clear Filters
Clear Filters

Trying to find the neighboring pixel outside the region boundary along the centre

2 views (last 30 days)
I have a ROi where I want to find the pixel value of a boundary pixel and its neighboring pixel outwardly to the region boundary along the radial ray between the region centre and this boundary pixel. h.jpg explains the problem better. I want to find the pixel value of C1 and C2. h2.jpg is the image where I want to implement the above problem. The region inside the yellow boundary is my ROI. I want to find the value of a pixel on the yellow boundary and the value of the pixel outwardly to the region boundary along the radial ray between the region centre and the boundary pixel. I tried the following line of code but I think my process is wrong. Any suggestions would be appreciated.
I2=imread('h2.jpg')
[nr, nc] = size(I2);
[r, c] = find(I2);
aboves = [r - 1, c];
aboves(r == 1, :) = [];
belows = [r + 1, c];
belows(c == nr, :) = [];
lefts = [r, c - 1];
lefts(c == 1, :) = [];
rights = [r, c + 1];
rights(c == nc, :) = [];
aboveleft = [r - 1, c - 1];
aboveleft(r == 1 | c == 1, :) = [];
aboveright = [r, c + 1];
aboveright(r == 1 | c == nc, :) = [];
belowleft = [r + 1, c - 1];
belowleft(r == nr | c == 1, :) = [];
belowright = [r + 1, c + 1];
belowright(r == nr | c == nc, :) = [];
neighbors = [aboveright; lefts; belows; aboveleft; belowright; aboves; rights; belowleft];

Answers (1)

Satwik
Satwik on 24 May 2024
Hi Warid!
I understand that you want to identify and analyse two specific types of pixels related to a ROI within an image outlined by a yellow boundary: one pixel on the boundary (C1) and another immediately outside the ROI, along a line extending from the ROI's centre through C1 (C2). To achieve your goal, you may follow these steps in MATLAB.
  1. Identify the Yellow Boundary : You'll need to convert your image to the HSV color space (or another appropriate space) to effectively isolate yellow colors.
% Read the image
I = imread('h2.jpg');
% Convert image to HSV
Ihsv = rgb2hsv(I);
% Define yellow color range in HSV
% Note: These values might need adjustment based on your specific shade of yellow
hueRange = [0.1 0.2]; % Example range for yellow, adjust as needed
saturationMin = 0.5; % Minimum saturation of yellow
valueMin = 0.5; % Minimum value/brightness of yellow
% Create a binary mask for yellow regions
yellowMask = (Ihsv(:,:,1) >= hueRange(1)) & (Ihsv(:,:,1) <= hueRange(2)) & ...
(Ihsv(:,:,2) >= saturationMin) & (Ihsv(:,:,3) >= valueMin);
% Optional: Clean up the mask
yellowMask = imfill(yellowMask, 'holes'); % Fill holes
yellowMask = bwareaopen(yellowMask, 50); % Remove small objects
% Find boundaries
[B, ~] = bwboundaries(yellowMask, 'noholes');
2. Find Contours (Boundaries) of the Yellow Region:
% Assuming B contains multiple boundaries, find the largest one
boundaryLengths = cellfun(@(x) size(x, 1), B); % Calculate the length of each boundary
[~, largestBoundaryIndex] = max(boundaryLengths); % Find the index of the largest boundary
boundary = B{largestBoundaryIndex}; % This is now the largest boundary, as a numeric array
3. Calculate the Centroid of the ROI:
% Calculate centroid of the ROI.
stats = regionprops(yellowMask, 'Centroid');
% Check if stats is not empty to safely extract the centroid
if ~isempty(stats)
centroid = stats.Centroid; % This gives you [x, y] coordinates of the centroid
else
error('No regions found. Check the binary mask and color thresholds.');
end
4. Find Boundary Pixel (C1) and Outward Neighbour (C2):
% Proceed with the calculation for one point on the boundary
point = boundary(1,:); % Correct usage
% Continue with the rest of your calculations as before
% Direction from centroid to boundary point
direction = [point(2) - centroid(1), point(1) - centroid(2)];
direction = direction / norm(direction); % Normalize
% Find C1 (boundary pixel value)
C1 = I(point(1), point(2), :); % This will be a 1x1x3 RGB value
% Step outward to find C2
stepSize = 1; % Adjust as needed
newPoint = round([point(2) + direction(1) * stepSize, point(1) + direction(2) * stepSize]);
% Ensure newPoint is within image bounds
newPoint(1) = max(min(newPoint(1), size(I, 2)), 1);
newPoint(2) = max(min(newPoint(2), size(I, 1)), 1);
% Find C2 (neighbor pixel value)
C2 = I(newPoint(2), newPoint(1), :); % This will be a 1x1x3 RGB value
This script provides a basic framework. You might need to loop through all boundary points or select specific ones based on your requirements. Also, the color range for identifying yellow might require adjustment depending on the specific hues in your image. This approach assumes the yellow boundary is distinct and can be isolated with color thresholding. If the boundary is not clearly defined by color, you might need a more sophisticated method to identify the ROI.
For more information on some of the functions used in the above code, you can refer to these documentation links:
Hope this helps!

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!