Finding center of symmetry between zones in a image
조회 수: 11 (최근 30일)
이전 댓글 표시
I have and image such as below (I am pasting only a sketch here) where I want to calculate the center of symmetry and the displacement between the 2 marked zones in the image(Marked in Red and blue). Could anyone suggest a simple algorithm for this (Basic code sniippet would also help). (Please note the signal is symmetric to 180 degree rotation). [The idea is to calculate the center of symmetry between the red and blue zones]
댓글 수: 3
Walter Roberson
2022년 7월 28일
You have not given us any idea how far off the ideal the situation could get.
At the moment, with what you have shown us, and assuming the red and blue lines are not actually present in the images, just regionprops() asking for 'Centroid', sort by x coordinate, then take the middle of each consecutive line segment joining the now-sorted coordinates.
채택된 답변
Image Analyst
2022년 7월 28일
Try this. The centroid of each blob is found and a vertical line is drawn there.
% 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 = 16;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = pwd;
baseFileName = 'mO7aP.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
rgbImage = 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(rgbImage)
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 = rgbImage(:, :, 1);
else
grayImage = rgbImage;
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 3, 1);
imshow(grayImage);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
drawnow;
%--------------------------------------------------------------------------------------------------------
% Display histogram
subplot(2, 3, 2);
histogram(grayImage, 256);
grid on;
title('Histogram of Image', 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% Binarize the image to get a mask.
threshold = 242;
mask = grayImage < threshold;
% Display mask image.
subplot(2, 3, 3);
imshow(mask);
axis('on', 'image');
drawnow;
caption = sprintf('Mask using a threshold of %.1f', threshold);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% Fill the blobs
mask = imfill(mask, 'holes');
% Take 5 largest blobs.
mask = bwareafilt(mask, 5);
subplot(2, 3, 4);
imshow(mask);
axis('on', 'image');
drawnow;
title('Final Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% Label each blob with 8-connectivity, so we can make measurements of it
[labeledImage, numberOfBlobs] = bwlabel(mask, 8);
% Apply a variety of pseudo-colors to the regions.
coloredLabelsImage = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
% Display the pseudo-colored image.
subplot(2, 3, 5);
imshow(coloredLabelsImage);
% Get all the blob properties.
blobMeasurements = regionprops(labeledImage, 'Area', 'Centroid');
numberOfBlobs = size(blobMeasurements, 1);
caption = sprintf('%d Individual Blobs', numberOfBlobs);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
% Get the areas
allAreas = [blobMeasurements.Area];
% Get the centroids
xy = vertcat(blobMeasurements.Centroid);
xCentroids = xy(:, 1);
yCentroids = xy(:, 2);
% Draw lines over centroids.
hold on;
for k = 1 : numberOfBlobs
xline(xCentroids(k), 'LineWidth', 2, 'Color', 'k');
plot(xCentroids(k), yCentroids(k), 'y+', 'LineWidth', 2, 'MarkerSize', 30);
end
hold off;
% Show size of Areas.
subplot(2, 3, 6);
bar(allAreas);
grid on;
xlabel('Area in Pixels', 'FontSize', fontSize, 'Interpreter', 'None');
ylabel('Blob Index', 'FontSize', fontSize, 'Interpreter', 'None');
title('Blob Areas', 'FontSize', fontSize, 'Interpreter', 'None');
댓글 수: 11
Image Analyst
2022년 7월 29일
You can crop the image so that the new image starts at the top of the box and the last line of the image is the last line of the box. Then find the centroids as I showed you. To find the center of the two boxes (#'s 2 and 5) in the left image you can do
% Find "center of gravity of the 2 boxes in the left image"
xctr = (xCentroid(2) + xCentroid(5)) / 2;
To do the same thing for the right image for blobs 3 and 4:
xctr = (xCentroid(3) + xCentroid(4)) / 2;
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!