dividing image into unequal quadrants and indicating position as an index of those.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
How can I divide an image with two perpendicular lines to divide it into segments and get the position of any object indicating the index of that object in any of those segment
0 Kommentare
Antworten (1)
Sufiyan
am 1 Mär. 2023
Hello,
You can refer to the code below for dividing the image into unequal quadrants, To get the position and the belonging segment of the object you can change the ‘object_index’.
img = imread('img.jpg');
gray = rgb2gray(img);
% Threshold the image to create a binary mask
thresh = graythresh(gray);
bw = imbinarize(gray, thresh);
% Labels each connected component in the binary mask
labelled = bwlabel(bw);
% Finding the centroid of each connected component
stats = regionprops(labelled,'Centroid');
number_of_objects_in_image = numel(stats);
% Define the object index you want to find the segment for
% ---USER GIVEN
%give this with in the range of number of objects
object_index = 771;
% Find the connected component (object) with the specified index
[row, col] = find(labelled == object_index);
object_centroid = [mean(col), mean(row)];
% Dividing the image into four equally sized segments by taking 2
% perpendicular lines
[h, w] = size(bw);
segment_height = round(h/2);
segment_width = round(w/2);
% mask of the segmented regions
mask = zeros(h, w);
mask(1:segment_height, 1:segment_width) = 1; % Segment 1
mask(1:segment_height, segment_width+1:w) = 2; % Segment 2
mask(segment_height+1:h, segment_width+1:w) = 3; % Segment 3
mask(segment_height+1:h, 1:segment_width) = 4; % Segment 4
% Determine which segment the object belongs to
segment_index = mask(round(object_centroid(2)), round(object_centroid(1)));
% Display the segment index for the object
fprintf('Object %d is in segment %d.\n', object_index, segment_index);
%gives the index of that object
[x,y]=find(labelled==object_index,1,'first');
fprintf("total number of objects =%d\n",number_of_objects_in_image);
fprintf("index of the object number %d is [%d,%d]\n",object_index,x,y);
you can go through the documentation regionprops to understand more abot the function 'regionprops'.
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!