img = rgb2gray(imread('20.bmp'));
res = detectHarrisFeatures(img,'MinQuality', 0.1, 'ROI', [1 386 762 20]);
x_vals = double(res.Location(:,1));
y_vals = double(res.Location(:,2));
slope_and_offset = polyfit(x_vals, y_vals,1);
angle = atand(slope_and_offset(1));
fprintf('\nThe screw is oriented %.5f degree to the horizontal.\n', angle)
img_rot = imrotate(img,angle);
res_new = detectHarrisFeatures(img_rot,'MinQuality', 0.1, 'ROI', [1 395 762 10]);
x_vals_rot = double(res_new.Location(:,1));
y_vals_rot = double(res_new.Location(:,2));
slope_and_offset_rot = polyfit(x_vals_rot, y_vals_rot,1);
angle_rot = atand(slope_and_offset_rot(1));
fprintf('\nThe screw is oriented %.5f degree to the horizontal after rotation.\n\n', angle_rot)
subplot(1,2,1)
hold on
title('Original Image')
imshow(img)
scatter(res.Location(:,1),res.Location(:,2),'or')
hold off
subplot(1,2,2)
hold on
title('Corrected Image')
imshow(img_rot)
scatter(res_new.Location(:,1),res_new.Location(:,2),'ob')
hold off
2 Comments
KSSV (view profile)
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/490253-detect-orientation-of-screw-image#comment_765640
Ang Xian Jia (view profile)
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/490253-detect-orientation-of-screw-image#comment_765647
Sign in to comment.