How to divide a rectangle into 4 regions?

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
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
joynob ahmed le 5 Avr 2020
I am sorry for the screenshot but it doesn't have graphics,I have got this image by using bwtraceboundary and drawrectangle.I am now giving the actual image in black and white.Thanks for your reply.
Image Analyst
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
joynob ahmed le 6 Avr 2020
I think what I need is the center of the mass and the lines that are connecting the center of the mass with the vertices of the rectangle.Actually I am trying to find border irregularity according to an research article.So I have attached the article for you.According to this article I have already rotated my image.I have faced problem in the next step that I have asked help for.
joynob ahmed
joynob ahmed le 10 Avr 2020
Modifié(e) : joynob ahmed le 10 Avr 2020
I need the centroid to be connected with the vertices of the rectangle and thus it will be divided into 4 regions. Then I need the distances between the boundary and rectangle in each region as a 1D data so that by converting it into 2D I can have the graph like this.
joynob ahmed
joynob ahmed le 13 Avr 2020
image analyst,can you help me with that?
joynob ahmed
joynob ahmed le 25 Avr 2020
I didn't understand the function of coefficient and how you have determined the distance.Can you explain this?

Connectez-vous pour commenter.

Réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by