【问题标题】:Image object width and height - Matlab图像对象的宽度和高度 - Matlab
【发布时间】:2020-08-18 13:29:49
【问题描述】:

基于这个问题Width and height of a rotated polyshape object - Matlab 我正在运行很酷的代码。它可以提取边界框的高度和宽度,但是我怎样才能获得“真实”对象的最大高度和宽度(这并不总是像所附图像中的边界框)?

代码:

clc;
clear all;
 
Image =  imread('E:/moon.gif');
BW = imbinarize(Image);
BW = imfill(BW,'holes');
BW = bwareaopen(BW, 100);
 
stat = regionprops(BW,'ConvexHull','MinFeretProperties');
 
% Compute Feret diameter perpendicular to the minimum diameter
for ii=1:numel(stat)
    phi = stat(ii).MinFeretAngle; % in degrees
    p = stat(ii).ConvexHull * [cosd(phi),-sind(phi); sind(phi),cosd(phi)];
    minRect = max(p) - min(p); % this is the size (width and height) of the minimal bounding box
    stat(ii).MinPerpFeretDiameter = minRect(2); % add height to the measurement structure
    width = minRect(1)
    height = minRect(2)
end

【问题讨论】:

  • 请说明您所说的“真实”高度和宽度是什么意思。为什么最小费雷特直径及其垂直费雷特直径不合适?
  • 使用脚本得到的图像宽度=128.0944,高度=225.6806。如果我没记错的话,这些是边界框尺寸。我所说的“真实宽度和高度”的意思,例如这种情况是白色像素的宽度(它不是高度的一半)。
  • 所以你想把物体拉直,然后得到宽度和高度,是吗?这几乎没有那么简单,它很快就会变得复杂:如果你的形状有树枝怎么办,例如形状像海星?
  • 你是对的,但我没有考虑分支对象。这种物体我该怎么做?
  • 您必须将对象缩小为一条线(参见中轴、骨架和细化),然后测量线的长度以及线到边缘的距离原始对象。要做到这一点并不容易。只要没有分支,骨架就可以正常工作。

标签: matlab


【解决方案1】:

你可以试试这个代码:-

clc;    % Clear the command window.
close all;  % Close all figures (except those of imtool.)
workspace;  % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 25;

%===============================================================================
% Get the name of the first image the user wants to use.
baseFileName = 'A.jpg';
folder = fileparts(which(baseFileName)); % Determine where demo folder is (works with all versions).
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

%=======================================================================================
% Read in demo image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the original image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');

% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% 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;
hp = impixelinfo(); % Set up status line to see values when you mouse over the 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(:, :, 1); % Take red channel.
end

% Display the image.
subplot(2, 2, 2);
imshow(grayImage, []);
axis on;
caption = sprintf('Gray Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;

% Display the histogram of the image.
subplot(2, 2, 3);
histogram(grayImage, 256);
caption = sprintf('Histogram of Gray Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
grid on;
drawnow;

%=======================================================================================
binaryImage = grayImage < 150;
% Keep only the largest blob.
binaryImage = bwareafilt(binaryImage, 1);
% Display the masked image.
subplot(2, 2, 3);
imshow(binaryImage, []);
axis on;
caption = sprintf('Binary Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
drawnow;

% Get the bounding box of the blob.
props = regionprops(binaryImage, 'BoundingBox');
boundingBox = [props.BoundingBox]

% Display the original image.
subplot(2, 2, 4);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
% Plot the bounding box over it.
hold on;
rectangle('Position', boundingBox, 'LineWidth', 2, 'EdgeColor', 'r')

【讨论】:

  • 代码不起作用,看起来它正在给出边界框。
  • 请考虑解释您的答案,而不是转储一些代码。
猜你喜欢
  • 1970-01-01
  • 2015-03-25
  • 1970-01-01
  • 2017-10-28
  • 1970-01-01
  • 1970-01-01
  • 2010-11-18
  • 1970-01-01
  • 2018-01-13
相关资源
最近更新 更多