Split a rhombus in an image into four through centroid

1 visualizzazione (ultimi 30 giorni)
I need to find the area of 4 partitions of the rhombus like image. Thank you in advance for the support.
The partition should be based on the centre point and the corners of rhombus. Assuming rombus may be angled.

Risposta accettata

Image Analyst
Image Analyst il 13 Dic 2022
Try this:
% Demo by Image Analyst
clc; % Clear the command window.
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 = 22;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = [];
baseFileName = 'rhombus.png';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
fprintf('It is not really gray scale like we expected - it is color\n');
% Extract the blue channel.
grayImage = rgb2gray(grayImage);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Update the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
drawnow;
%--------------------------------------------------------------------------------------------------------
% Show the histogram
subplot(2, 2, 2);
imhist(grayImage);
title('Histogram of Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
grid on;
%--------------------------------------------------------------------------------------------------------
% Interactively and visually set a threshold on a gray scale image.
% https://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image?s_tid=srchtitle
lowThreshold = 58;
highThreshold = 255;
% [lowThreshold, highThreshold] = threshold(lowThreshold, highThreshold, grayImage)
%--------------------------------------------------------------------------------------------------------
% Create a mask
mask = grayImage >= lowThreshold & grayImage <= highThreshold;
% Fill blobs
mask = imfill(mask,"holes");
% Take largest blob only.
mask = bwareafilt(mask, 1);
subplot(2, 2, 3);
imshow(mask, []);
impixelinfo;
axis('on', 'image');
title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% Measure blob features.
labeledImage = bwlabel(mask);
props = regionprops(mask, grayImage, 'WeightedCentroid', 'Centroid');
if isempty(props)
msgbox('No blobs found.')
return;
end
fprintf('Found %d blobs.\n', numel(props))
% Get the weighted centroid. Not sure which centroid you want.
% xCentroid = props.WeightedCentroid(1);
% yCentroid = props.WeightedCentroid(2);
% Get the unweighted centroid.
xCentroid = props.Centroid(1);
yCentroid = props.Centroid(2);
% Draw lines for the quadrants.
hold on;
xline(xCentroid, 'LineWidth', 2, 'Color', 'r');
yline(yCentroid, 'LineWidth', 2, 'Color', 'r');
hold off;
subplot(2, 2, 1);
hold on;
xline(xCentroid, 'LineWidth', 2, 'Color', 'r');
yline(yCentroid, 'LineWidth', 2, 'Color', 'r');
hold off;
% Get the area of the 4 quadrants. Dividing lines align with the edges of the image.
% Get area of upper left.
quad1 = mask(1 : floor(yCentroid), 1 : floor(xCentroid));
areaUpperLeft = nnz(quad1)
% Get area of upper right.
quad2 = mask(1 : floor(yCentroid), ceil(xCentroid) : end);
areaUpperRight = nnz(quad1)
% Get area of lower left.
quad3 = mask(ceil(yCentroid) : end, 1 : floor(xCentroid));
areaLowerLeft = nnz(quad1)
% Get area of lower right.
quad4 = mask(ceil(yCentroid) : end, ceil(xCentroid) : end);
areaLowerRight = nnz(quad1)
% Show areas as bar chart.
areas = [areaUpperLeft, areaUpperRight, areaLowerLeft, areaLowerRight];
subplot(2, 2, 4);
bar(areas);
title('Areas of Quadrants', 'FontSize', fontSize)
xlabel('Quadrant', 'FontSize', fontSize)
ylabel('Area in pixels', 'FontSize', fontSize)
grid on;
  7 Commenti
Image Analyst
Image Analyst il 15 Dic 2022
I already knew that. You basically just repeated saying that you need to measure the quadrants. But you didn't say why even though I asked directly. Maybe it's secret or proprietary and that's why you won't tell anyone.
But how will your algorithm work on the attached image?
Does it find the vertices? (Like the attached demo)
D D
D D il 19 Dic 2022
I understood your point.. But Such a big tilt is not expected in the input image.. Thank you for helping me with solving the problem..

Accedi per commentare.

Più risposte (1)

Image Analyst
Image Analyst il 12 Dic 2022
If you still can't figure it out (how to get the centroid and use indexing to split it into 4 parts) then write back, but it should be pretty easy.

Categorie

Scopri di più su Images in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by