Regionprops - finding the weighted centroid of one half of the image frame.

11 Ansichten (letzte 30 Tage)
I am trying to find the weightedcentroid of an image that is mirrored. As in, a frame will have an image twice, split down the middle. How can I set the region coordinates to find the weighted centroid of just one side of the frame and capture a single instance of the image?
Thank you.

Akzeptierte Antwort

Image Analyst
Image Analyst am 4 Jul. 2019
Try this demo. Adapt as needed, like to change the input filename:
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 = 15;
%===============================================================================
% Read in gray scale demo image.
% Let's let the user select from a list of all the demo images that ship with the Image Processing Toolbox.
folder = fileparts(which('cameraman.tif')); % Determine where demo folder is (works with all versions).
baseFileName = 'cameraman.tif';
% Get the full filename, with path prepended.
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);
% Display the image.
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
% 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.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(rgbImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = rgbImage(:, :, 2); % Take green channel.
else
grayImage = rgbImage; % It's already gray scale.
end
% Now it's gray scale with range of 0 to 255.
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
% Make a mask for the top half of the image
binaryImage = false(rows, columns);
binaryImage(1 : round(rows/2), 1 : columns) = true;
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Upper half mask', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
% Use it to zero out the other parts of the image.
grayImage(~binaryImage) = 0;
% Display the image.
subplot(2, 2, 3);
imshow(grayImage, []);
title('Masked gray image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
%------------------------------------------------------------------------------
% Measure blobs
props = regionprops(binaryImage, grayImage, 'WeightedCentroid');
xy = props.WeightedCentroid
% Put a red cross at the centroid.
hold on;
plot(xy(1), xy(2), 'r+', 'MarkerSize', 100, 'LineWidth', 3);
% Update title
caption = sprintf('Centroid at x (column) = %.1f, y (row) = %.1f', xy(1), xy(2));
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
0000 Screenshot.png
  3 Kommentare
Image Analyst
Image Analyst am 8 Jul. 2019
No, not true. Did you use 'Centroid' instead of 'WeightedCentroid' by mistake? Look at my demo - the red cross is not in the exact middle of the top half - it's slightly up and to the right, which is where it should be because the upper right is brighter than the lower left. It's at (x,y) = (133.3, 571.) as the title says, NOT at (128, 64) like it would be if it were at the (unweighted) center of the top half.
If you still don't believe me, post your script and one or two of your many images you claim it doesn't work with.
Or else prove it to yourself and ask regionprops for BOTH Centroid and WeightedCentroid and plot them both and observe that they are probably in different locations (Only exception would be if your grayscale image is completely symmetric in the upper half which is unlikely.
Anusha Nagella
Anusha Nagella am 9 Jul. 2019
Thank you for the clarification - it seemed the images I used had a weighted centroid that looked to be around where the centroid would be.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Julie
Julie am 4 Jul. 2019
Can't you just delete the mirrored half? Otherwise, just remove all objects present in the mirrored half using the regionprops location.

Community Treasure Hunt

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

Start Hunting!

Translated by