MATLAB Answers

Get rectangle of 4 points and detect formed rectangle orientation

20 views (last 30 days)
wbadry
wbadry on 30 Apr 2018
Answered: Image Analyst on 30 Apr 2018
Hello, I have an image as shown below
I wrote the following script to detect their centroids
clc;
clear all;
imgRGB = imread('img1.png');
subplot(2,2,1)
imshow(imgRGB), title('Original Image')
imgGray = rgb2gray(imgRGB);
subplot(2,2,2)
imshow(imgGray),title('Gray Image')
imgBinary = imbinarize(imgGray,'global');
imgBinary = imcomplement(imgBinary);
subplot(2,2,3)
imshow(imgBinary), title('Binary Image')
hblob = vision.BlobAnalysis;
hblob.Connectivity = 8;
hblob.AreaOutputPort = false;
hblob.BoundingBoxOutputPort = false;
centroid = step(hblob,imgBinary);
table(centroid)
subplot(2,2,4)
imshow(imgRGB), title('Detected Objects')
hold on
for i = 1 : numel(centroid(:,1))
plot(centroid(i,1),centroid(i,2),'+r');
end
My two questions are:
  1. Is there any mean to draw a rectangle lines between them even if image is rotated ?
  2. Id there any mean to detect the orientation of formed rectangle ?

  0 Comments

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 30 Apr 2018
Once you have the centroids, you can use plot() to plot a line in the overlay above the image. It doesn't care if the rectangle is aligned with vertical and horizontal or not. If you need them burned into the image, you can use imline() to create a binary image that you can use as a mask to burn in values.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!