How to divide a rectangle into 4 regions?
Afficher commentaires plus anciens
I want to find the boundary pixels lying on the lines connecting the center of the mass with the vertices. These four points make it possible to divide the boundary rectangle into four parts which are not the same length. In the next step i want to calculate the distance between the border rectangle and the image edge for each part of the borderline. I have attached the images on which I am working and what I want to do.


7 commentaires
Image Analyst
le 5 Avr 2020
The center of mass is not necessarily at the crossing point of the two diagonal lines. In fact the center of mass does not necessarily even need to lie inside the region. Anyway, can you provide a binary image with the blob as white and the background as black? A PNG image file, not a screenshot with all kinds of graphics on it.
joynob ahmed
le 5 Avr 2020
Image Analyst
le 5 Avr 2020
Modifié(e) : Image Analyst
le 5 Avr 2020
But what about the fact that the centroid of the binary blob does not lie where the bounding box diagonals cross? Which of those two locations do you want to take as the center?
% Initialization steps.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
mask = imread('image.bmp');
imshow(mask);
[rows, columns, numberOfColorChannels] = size(mask)
% Binarize the RGB image
if numberOfColorChannels > 1
mask = mask(:,:,1) > 128;
numberOfColorChannels = 1;
end
% Get rid of white rectangular frame surrounding the blob.
mask = imclearborder(mask);
% Fill holes and take the largest blob.
mask = bwareafilt(imfill(mask, 'holes'), 1);
% Fill holes and take the largest blob.
mask = bwareafilt(imfill(mask, 'holes'), 1);
% Show updated mask.
imshow(mask);
props = regionprops(mask, 'BoundingBox', 'Centroid');
% Now get the boundary
boundaries = bwboundaries(mask);
boundaries = boundaries{1};
xBoundary = boundaries(:, 2);
yBoundary = boundaries(:, 1);
hold on;
% Plot red x at the centroid.
plot(props.Centroid(1), props.Centroid(2), 'rx', 'MarkerSize', 400, 'LineWidth', 2);
% Plot bounding box as a yellow rectangle.
rectangle('Position', props.BoundingBox, 'EdgeColor', 'y');
% Plot boundary in red.
plot(xBoundary, yBoundary, 'r-', 'LineWidth', 2);
% Draw lines across diagonals of bounding box.
x1 = props.BoundingBox(1)
y1 = props.BoundingBox(2)
x2 = props.BoundingBox(1) + props.BoundingBox(3)
y2 = props.BoundingBox(2) + props.BoundingBox(4)
line([x1, x2], [y1, y2], 'Color', 'y', 'LineWidth', 2);
line([x2, x1], [y1, y2], 'Color', 'y', 'LineWidth', 2);
% Find midpoints of the box.
xMid = mean([x1, x2])
yMid = mean([y1, y2])
% Get line from upper left to lower right.
coefficients1 = polyfit([x1, x2], [y1, y2], 1)
xLine = 1 : columns;
yLine1 = polyval(coefficients1, xLine);
% Get line from upper right to lower left.
coefficients2 = polyfit([x2, x1], [y1, y2], 1)
yLine2 = polyval(coefficients2, xLine);
% % Get distance of the top line of the bounding box to all points in the upper triangle
% validIndexes = x < xMid & yBoundary < yLine1 & yBoundary < yLine2;
% x = xBoundary(validIndexes);
% y = yBoundary(validIndexes);
% distancesTop = y1 - y;

joynob ahmed
le 6 Avr 2020
joynob ahmed
le 10 Avr 2020
Modifié(e) : joynob ahmed
le 10 Avr 2020
joynob ahmed
le 13 Avr 2020
joynob ahmed
le 25 Avr 2020
Réponses (0)
Catégories
En savoir plus sur Geometric Transformation and Image Registration dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

