How can i isolate an object and find its orientation with respect to another object?

I am new to image processing toolbox in Matlab and I wanted some directions or steps on how I could do this process. In the center of this image in the center of the hole (on the cross hair), I have a piece of material which is almost hexagonal in shape with reddish color. I have multiple pictures similar to this where the material is oriented slightly differently.
Could someone please tell me how can I isolate this object from the rest of the image and then find its orientation? I tried to use image contrast and segmentation, but it wasnt super clean.

2 commentaires

Can you upload an image without crosshairs and other annotations?
OK, I downloaded what you posted and photoshopped it and got the attached image. I'll try to work on it this weekend if I get a chance. As you can probably tell from my username, I'm an expert in image analysis. By the way global contrast enhancement does nothing as far as helping you to segment the image - it only makes it easier for you to see. But the thresholding will just happen at a different level - it's not going to be any easier to determine what the level is. But local contrast enhancement might help. The first thing I'll try is to convert to HSV color space to see if the hexagon is a slightly different color which can be thresholded. I'll also try a local contrast enhancement by adapthisteq. More sophisticated methods such as simulated annealing or chan-vese segmentation might be needed if the simple methods fail.
But since no one has heard from you since your original post 2 weeks ago, I'm not going to do anything unless I hear from you that it's still a problem you need solved.

Connectez-vous pour commenter.

Réponses (1)

hello
I am not really an image processing expert and therefore also my answer does not relly on the IP toolbox (I don't have /uses)
only "special" function used here comes from here : cghisteq - File Exchange - MATLAB Central
so use this unless someone with higher powers can deliver a better solution ...
basically the cde will center , crop , enhance the contrats, threshold (binarize) then do some basic analysis of the remaining points (red) along radially oriented lines . The result looks more or less like a hexagone (or a potatoe ?) , remains a bit of work to find it's center and main orientation (must find the peaks from the radial values of those light blue points).
hope it helps !
my result so far :
code :
[im,~] = imread('image1.png'); % your image file (RGB)
[r,c,p] = size(im);
%% 1/ find center (of the cross hair)
img = rgb2gray(im);
threshold = 0.25*max(img(:));
ind = img<threshold;
[y,x] = find(ind);
% crop the x,y range of the cross hair area
ii = (x>0.3*c & x<0.8*c) & (y>0.2*r & y<0.8*r);
x = x(ii);
y = y(ii);
% find the x,y coordinates of the horizontal and vertical lines of the cross hair
% using histcounts
[nx,edgesx,binx] = histcounts(x,numel(x));
ix = find(nx>max(nx)/2);
xc = round(mean(edgesx(ix)));
[ny,edgesy,biny] = histcounts(y,numel(y));
iy = find(ny>max(ny)/2);
yc = round(mean(edgesy(iy)));
figure;
imshow(im);title('Input image ');
hold on
plot(x,y,'g.')
plot(xc,yc,'m+','markersize',25,'linewidth',2)
%% center and crop image then do some contrast enhancement
nx = 70;
ny = 70;
im = im(yc-ny:yc+ny,xc-nx:xc+nx,:); % crop to get only the centre
eim = enhanced(im); % see function provided below
eim = rgb2gray(eim);
% new center coordinates after cropping
xc2 = nx+1;
yc2 = ny+1;
% remove points belonging to the cross hair for the next computations
eim2 = eim;
eim2(:,xc2-1:xc2+1) = 1;
eim2(yc2-1:yc2+1,:) = 1;
threshold = 0.25*max(eim2(:));
[y,x] = find(eim2<threshold);
figure;
subplot(1,2,1),
imshow(im);title('Input image ');
hold on
plot(xc2,yc2,'m+','markersize',25,'linewidth',2)
subplot(1,2,2),
imshow(eim); title('Constrast Enhanced image');
hold on
plot(x,y,'r.')
% convert to polar
[theta,r] = cart2pol(x-xc2,y-yc2);
%% find the hexagone = a ribbon of dots that must lie beetween two radii bounds
rmin = 30;
rmax = 50;
theta_new = linspace(-pi,pi);
dt = mean(diff(theta_new));
rmin = 30;
rmax = 50;
for k = 1:numel(theta_new)
ind = (theta>=theta_new(k)-dt/2) & (theta<theta_new(k)+dt/2);
rr = r(ind);
thr = theta(ind);
ii = (rr>rmin & rr<rmax);
rs(k) = median(rr(ii));
ths(k) = mean(thr(ii));
end
% backward conversion to cartesian coordinates
[xs,ys] = pol2cart(ths,rs);
xs = xs + xc2;
ys = ys + yc2;
plot(xs,ys,'c-*','markersize',12,'linewidth',2)
plot(xc2,yc2,'m+','markersize',25,'linewidth',2)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function eim = enhanced(im)
% color image global histogram equalization
% Fex : https://fr.mathworks.com/matlabcentral/fileexchange/64033-cghisteq/files/cghisteq.m?s_tid=srchtitle
maxI= 255;
cim=rgb2hsv(im);
imv =cim(:,:,3);
imv= round(imv.*maxI);
X0 = min(imv(:));
XL =max(imv(:));
bins=X0:XL;
hc=histc(imv(:),bins);
nhc = hc / sum(hc) ;
chc = cumsum(nhc);
T = X0 + (XL-X0).*chc;
eimv=T(imv+1-X0);
cim(:,:,3) = eimv./maxI;
eim=hsv2rgb(cim);
end

Commenté :

le 9 Mar 2026

Community Treasure Hunt

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

Start Hunting!

Translated by