フィルターのクリア

how to get the center of frame/frames of an rgb/greyscale image?

5 ビュー (過去 30 日間)
MAHMOOD JAMIL
MAHMOOD JAMIL 2018 年 3 月 22 日
コメント済み: MAHMOOD JAMIL 2018 年 3 月 28 日
I want to find the center of a frame of a greyscale image and also the center of a particular shape (let's say circle) in that image.

採用された回答

Gopichandh Danala
Gopichandh Danala 2018 年 3 月 26 日
編集済み: Gopichandh Danala 2018 年 3 月 27 日
In case of a whole grayscale image, just count #.of.rows and #.of.cols and find center accordingly.
For a specific shape or object get a binary image of it and use
regionprops
which has lot of properties including
centroid
Sample Code for something like this
img = rgb2gray(imread('img.jpg'));
[rows, cols, channels] = size(img);
figure, imshow(img);
% to extract center of a whole grayscale image
center_col = ceil(cols/2);
center_row = ceil(rows/2);
fprintf('\ncenter_row: %0.2f; center_col: %0.2f', center_row, center_col)
centroid_shape = regionprops(img, 'centroid');
xy = [centroid_shape.Centroid];
xCentriods = xy(1:2:end);
yCentroids = xy(2:2:end);
% This image has multiple components
% to extract or filter based on size use bwareafilt
% to label use bwlabel
BW = bwareafilt(logical(img),1); % This extracts largest '1' object..
figure, imshow(BW);
% Then get centroid of that specific BW
BW_centroid = regionprops(BW, 'centroid');
center_col1 = BW_centroid.Centroid(1);
center_row1 = BW_centroid.Centroid(2);
fprintf('\ncenter_row: %0.2f; center_col: %0.2f', center_row1, center_col1)
Modified by ImageAnalyst suggestions..
For more information read: bwareafilt, bwareaopen, regionprops
  5 件のコメント
MAHMOOD JAMIL
MAHMOOD JAMIL 2018 年 3 月 27 日
Thanks, it worked!
Image Analyst
Image Analyst 2018 年 3 月 28 日

Now that I've seen your image, I would not do it like that. First of all, you don't need to call regionprops twice because you can simply call bwareafilt() before it to extract the largest blob. However, that is not the biggest problem. I think you want the centroid of all the white stuff in the image, and the center of all that may not (actually probably won't) coincide with the center of the largest blob. Therefore I'd do it like I'm showing in my answer.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2018 年 3 月 28 日
This code will find the center of all the white stuff -- all together as if it were one object instead of multiple separate objects. And I think that is what you want, rather than just the centroid of the largest blob.
If you have a gray scale image instead of a binary one, then you can add on an additional formula to find the weighted centroid. Of course for a binary image it won't make any difference because the weighted centroid and the unweighted centroid coincide perfectly.
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 = 20;
% Check that user has the specified Toolbox installed and licensed.
hasLicenseForToolbox = license('test', 'image_toolbox'); % license('test','Statistics_toolbox'), license('test','Signal_toolbox')
if ~hasLicenseForToolbox
% User does not have the toolbox installed, or if it is, there is no available license for it.
% For example, there is a pool of 10 licenses and all 10 have been checked out by other people already.
ver % List what toolboxes the user has licenses available for.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
%===============================================================================
% Read in gray scale demo image.
folder = pwd; % Determine where demo folder is (works with all versions).
baseFileName = 'binary image to find centriod.jpg';
% 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 Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
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(:, :, 3); % Take blue channel.
else
grayImage = rgbImage; % It's already gray scale.
end
% Now it's gray scale with range of 0 to 255.
% Display the image.
subplot(1, 2, 1);
imshow(grayImage, []);
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
%------------------------------------------------------------------------------
% 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;
% Convert to logical (binary) with range false and true.
binaryImage = grayImage > 128;
% Get rid of white surround.
binaryImage = imclearborder(binaryImage);
% Display the image.
subplot(1, 2, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
% Get the x and y values:
[y, x] = find(binaryImage);
% Get the mean x and mean y.
xCenter = mean(x);
yCenter = mean(y);
% Plot the center.
hold on;
plot(xCenter, yCenter, 'r+', 'MarkerSize', 50, 'LineWidth', 2);
  1 件のコメント
MAHMOOD JAMIL
MAHMOOD JAMIL 2018 年 3 月 28 日
Thank you Image Analyst for such detailed explanation. Yes, you are right at your point, I could not think about it

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGeometric Transformation and Image Registration についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by