Hi, can i know how to find the orientation of an object?

15 views (last 30 days)
Nurul Farhana Mohd Fadzli
Nurul Farhana Mohd Fadzli on 13 May 2022
Commented: DGM on 14 May 2022
I need to do a project that can identify the position and orientation of object on the conveyor so that the object will be in a right position/posture before being grab by the robotic arm.
Can anyone help me with the algorithm and the development of coding?

Accepted Answer

DGM
DGM on 13 May 2022
Edited: DGM on 13 May 2022
Well, here's this. It's not robust, but then again, the test image isn't a real process image either, so I'm going to consider it fair play.
inpict = imread('boxconvey.jpg');
% preprocess
inpict = imflatfield(inpict,20);
% binarize
mkrange = [0 1; 0.192 0.409; 0.742 1];
hsvpict = rgb2hsv(inpict);
M = all(hsvpict >= permute(mkrange(:,1),[2 3 1]),3) ...
& all(hsvpict <= permute(mkrange(:,2),[2 3 1]),3);
M = imfill(M,'holes');
M = imopen(M,ones(3));
M = imclose(M,ones(11));
imshow(M)
% get properties
S = regionprops(M,'centroid','image');
C = vertcat(S.Centroid);
numobj = numel(S);
% find number of apparent convex vertices
L = bwlabel(bwperim(M));
thest = zeros(numobj,1);
for k = 1:numobj
% calculate the object radius, sorted by angle
[y x] = find(L==k);
dv = [x y] - S(k).Centroid;
r = sqrt(sum((dv).^2,2));
th = atan2d(dv(:,2),dv(:,1));
[th idx] = sort(th);
r = r(idx);
% peak finding will probably be fragile
% good luck with that
[pk idx] = findpeaks(r,'minpeakprominence',8);
pkth = th(idx);
thest(k) = -mean([max(pkth(pkth<0)) min(pkth(pkth>=0))]);
end
thest
thest = 6×1
15.9395 -13.5102 -10.8771 9.6239 -29.9191 29.2230
% demonstrate that the estimated angles are approximately correct
% by counter-rotating the object images
testimgs = cell(numobj,1);
for k = 1:numobj
th = thest(k);
thisimg = S(k).Image;
testimgs{k} = imrotate(thisimg,-th,'crop');
end
montage(testimgs)
I'm sure someone can come up with a better alternative, so I'll leave that to them.
  5 Comments
DGM
DGM on 14 May 2022
You just get the bounding boxes from regionprops() and plot them.
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/996520/boxconvey.jpg');
% preprocess
inpict = imflatfield(inpict,20);
% binarize
mkrange = [0 1; 0.192 0.409; 0.742 1];
hsvpict = rgb2hsv(inpict);
M = all(hsvpict >= permute(mkrange(:,1),[2 3 1]),3) ...
& all(hsvpict <= permute(mkrange(:,2),[2 3 1]),3);
M = imfill(M,'holes');
M = imopen(M,ones(3));
M = imclose(M,ones(11));
S = regionprops(M,'centroid','image','boundingbox');
C = vertcat(S.Centroid);
numobj = numel(S);
% find number of apparent convex vertices
L = bwlabel(bwperim(M));
thest = zeros(numobj,1);
for k = 1:numobj
% calculate the object radius, sorted by angle
[y x] = find(L==k);
dv = [x y] - S(k).Centroid;
r = sqrt(sum((dv).^2,2));
th = atan2d(dv(:,2),dv(:,1));
[th idx] = sort(th);
r = r(idx);
% peak finding will probably be fragile
% good luck with that
[pk idx] = findpeaks(r,'minpeakprominence',8);
pkth = th(idx);
thest(k) = -mean([max(pkth(pkth<0)) min(pkth(pkth>=0))]);
end
% draw bounding boxes
imshow(M); hold on
for k = 1:numobj
thisbb = S(k).BoundingBox;
boxx = thisbb(1) + [0 thisbb(3)];
boxy = thisbb(2) + [0 thisbb(4)];
plot(boxx([1 1 2 2 1]),boxy([1 2 2 1 1]))
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!