
Make 4 bounding Boxes and record the position, orientation and size of the objects in the image
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
The goal is to record the position, size and orientation of the 4 objects in the image and create a bounding box for each object
.

This is the code I have so far:
pic = imread("image.jpg");
grayImage = rgb2gray(pic);
subplot(2, 2, 1)
imshow(pic)
axis on;
axis image;
caption = sprintf('Original image');
title(caption,'FontSize',11,'Interpreter','none')
drawnow;
subplot(2, 2, 2)
imshow(grayImage)
axis on;
axis image;
caption = sprintf('Original grayscale');
title(caption,'FontSize',11,'Interpreter','none')
drawnow;
BW = imbinarize(grayImage);
subplot(2, 2, 3)
imshow(BW)
axis on;
axis image;
caption = sprintf('Binary image');
title(caption,'FontSize',11,'Interpreter','none');
props = regionprops(BW,'BoundingBox');
allBB = vertcat(props.BoundingBox);
for s = 1 : length(props)
thisBB = props(s).BoundingBox;
rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],...
'EdgeColor','r','LineWidth',2 )
end
drawnow;
LI = bwlabel(BW);
subplot(2, 2, 4)
imshow(grayImage)
axis on;
axis image;
caption = sprintf('Gray scale with box layover');
title(caption,'FontSize',11,'Interpreter','none');
drawnow;
0 comentarios
Respuestas (1)
Image Analyst
el 5 de Mayo de 2023
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 short g;
format compact;
fontSize = 16;
markerSize = 6;
rgbImage = imread("blocks.jpeg");
% 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');
% Convert color image to grayscale.
grayImage = rgb2gray(rgbImage);
else
% It's already grayscale.
grayImage = rgbImage;
end
figure('Name', 'Demo by Image Analyze', 'NumberTitle','off')
subplot(2, 3, 1)
imshow(rgbImage)
axis on;
axis image;
caption = sprintf('Original Color Image');
title(caption,'FontSize',fontSize,'Interpreter','none')
drawnow;
subplot(2, 3, 2)
imshow(grayImage)
axis on;
axis image;
impixelinfo;
caption = sprintf('Grayscale Version of Image');
title(caption,'FontSize',fontSize,'Interpreter','none')
drawnow;
subplot(2, 3, 3)
imhist(grayImage);
grid on;
title('Histogram','FontSize',fontSize,'Interpreter','none');
% Threshold to get a mask
% mask = imbinarize(grayImage, "adaptive", "ForegroundPolarity", "bright");
mask = grayImage < 75;
subplot(2, 3, 4)
imshow(mask)
axis on;
axis image;
caption = sprintf('Binary Image');
title(caption,'FontSize',fontSize,'Interpreter','none');
% Make measurements.
props = regionprops(mask,'BoundingBox', 'Area', 'Orientation');
allBB = vertcat(props.BoundingBox)
allAreas = [props.Area]
allAngles = [props.Orientation]
hold on;
for s = 1 : length(props)
thisBB = props(s).BoundingBox;
rectangle('Position', [thisBB(1),thisBB(2),thisBB(3),thisBB(4)],...
'EdgeColor','r','LineWidth',2 )
fprintf('Block %d has area = %d pixels and angle = %.2f.\n', ...
s, props(s).Area, props(s).Orientation)
xt = thisBB(1);
yt = thisBB(2);
caption = sprintf('%d at %.2f degrees', s, props(s).Orientation);
text(xt, yt, caption, 'VerticalAlignment','bottom', 'Color', 'r', 'FontSize', 12, 'FontWeight','bold');
end
hold off;
drawnow;
[labeledImage, numBlobs] = bwlabel(mask);
labeledOverlayImage = labeloverlay(grayImage, labeledImage);
subplot(2, 3, 5)
imshow(labeledOverlayImage)
axis on;
axis image;
caption = sprintf('With box overlays');
title(caption,'FontSize',fontSize,'Interpreter','none');
drawnow;
% Maximize window.
g = gcf;
g.WindowState = "maximized";

Block 1 has area = 13796 pixels and angle = 35.29.
Block 2 has area = 16141 pixels and angle = -62.62.
Block 3 has area = 8714 pixels and angle = -52.44.
Block 4 has area = 17142 pixels and angle = 72.18.
0 comentarios
Ver también
Categorías
Más información sobre Lighting, Transparency, and Shading en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!