I have the following code:
clc; clear all; % This part reads and shows the image selected I = imread('115.2.jpg'); imshow(I) % Ginput is used to select the Hinge position [x,y] = ginput(1) % drawpoint is used to mark the Hinge position h = drawpoint('Position',[x y]); % Convert the RGB image to a Grayscale image G=im2gray(I); level = graythresh(G) % Binarize the grayscale image BW = imbinarize(G,level); % Display the Grayscale image figure title('Grayscale Image') imshow(G) % Display the Binarized image figure title('Binarized') imshow(BW) % Fill holes BW2 = imfill(BW,'holes'); % Display the Image after filling holes figure imshow(BW2) title('Filled Image') % Mark the Hinge here as well h = drawpoint('Position',[x y]); imwrite(BW2, 'BW2.jpg') % Find the centroid measurements = regionprops(BW2, 'Centroid'); imshow(BW2); centroids = cat(1,measurements.Centroid); hold on; plot(centroids(:,1), centroids(:,2), 'r+', 'MarkerSize', 30, 'LineWidth', 1); hold off;
Since you're working with a binary image here, BW2, you can use the bwpropfilt function to clean up the small areas.
BW2 = bwpropfilt(BW2, 'Area', 1);
The last input argument determines the largest # of regions to keep, so it can be lowered to just the one. From there regionprops would only detect the single centroid.
You could also pass area as an input to regionprops as it is and use the centroid of the largest area.
measurements = regionprops(BW2, 'Centroid', 'Area'); [~,ind] = max(cat(1,measurements.Area)); centroid = measurements(ind).Centroid;
Matlabsolutions.com provides guaranteed satisfaction with a
commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain
experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support
to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been
empanelled after extensive research and quality check.
Matlabsolutions.com provides undivided attention to each Matlab
assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services
include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work
done at the best price in industry.