Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7735969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:38:00+00:00 2026-06-01T07:38:00+00:00

This program currently inputs an image of a coin, thresholds it, binarizes it, and

  • 0

This program currently inputs an image of a coin, thresholds it, binarizes it, and finds the major and minor axis lengths of the segmented elliptical using the regionprops function. How do I output a subplot where I draw the axes used to calculate the ‘MajorAxisLength’ and ‘MinorAxisLength’ over the original image?

I have appended my code for your perusal.

% Read in the image.
folder = 'C:\Documents and Settings\user\My Documents\MATLAB\Work';
baseFileName = 'coin2.jpg';
fullFileName = fullfile(folder, baseFileName);
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
    fullFileName = baseFileName; % No path this time.
    if ~exist(fullFileName, 'file')
        %Alert user.
        errorMessage = sprintf('Error: %s does not exist.', fullFileName);
        uiwait(warndlg(errorMessage));
        return;
    end
end

rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 3, 1);
imshow(rgbImage, []);
title('Original color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));

% Extract the individual red color channel.
redChannel = rgbImage(:, :, 1);
% Display the red channel image.
subplot(2, 3, 2);
imshow(redChannel, []);
title('Red Channel Image', 'FontSize', fontSize);
% Binarize it
binaryImage = redChannel < 100;
% Display the image.
subplot(2, 3, 3);
imshow(binaryImage, []);
title('Thresholded Image', 'FontSize', fontSize);

binaryImage = imfill(binaryImage, 'holes');
labeledImage = bwlabel(binaryImage);

area_measurements = regionprops(labeledImage,'Area');
allAreas = [area_measurements.Area];
biggestBlobIndex = find(allAreas == max(allAreas));
keeperBlobsImage = ismember(labeledImage, biggestBlobIndex);
measurements = regionprops(keeperBlobsImage,'MajorAxisLength','MinorAxisLength')

% Display the original color image with outline.
subplot(2, 3, 4);
imshow(rgbImage);
hold on;
title('Original Color Image with Outline', 'FontSize',fontSize);
boundaries = bwboundaries(keeperBlobsImage);
blobBoundary = boundaries{1};
plot(blobBoundary(:,2), blobBoundary(:,1), 'g-', 'LineWidth', 1);
hold off;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:38:02+00:00Added an answer on June 1, 2026 at 7:38 am

    I had the same task as you for some project I did 2 years ago. I’ve modified the code I used then for you below. It involved calculating the covariance matrix for the datapoints and finding their eigenvalues/eigenvectors. Note here that because of circular symmetry, the minor and major axis will be somewhat “random”. Also note that I have made the image binary in a very naïve way to keep the code simple.

    % Load data and make bw
    clear all;close all; clc; 
    set(0,'Defaultfigurewindowstyle','docked')
    
    I = imread('american_eagle_gold_coin.jpg');
    Ibw = im2bw(I,0.95);
    Ibw = not(Ibw);
    
    figure(1);clf
    imagesc(Ibw);colormap(gray)
    
    %% Calculate axis and draw
    
    [M N] = size(Ibw);
    [X Y] = meshgrid(1:N,1:M);
    
    %Mass and mass center
    m = sum(sum(Ibw));
    x0 = sum(sum(Ibw.*X))/m;
    y0 = sum(sum(Ibw.*Y))/m;
    
    %Covariance matrix elements
    Mxx = sum(sum((X-x0).^2.*Ibw))/m;
    Myy = sum(sum((Y-y0).^2.*Ibw))/m;
    Mxy = sum(sum((Y-y0).*(X-x0).*Ibw))/m;
    
    MM = [Mxx Mxy; Mxy Myy];
    
    [U S V] = svd(MM);
    
    W = V(:,1)/sign(V(1,1)); %Extremal directions (normalized to have first coordinate positive)
    H = V(:,2);
    W = 2*sqrt(S(1,1))*W; %Scaling of extremal directions to give ellipsis half axis
    H = 2*sqrt(S(2,2))*H;
    
    figure(1)
    hold on
        plot(x0,y0,'r*');
        quiver(x0,y0,W(1),H(1),'r')
        quiver(x0,y0,W(2),H(2),'r')
    hold off
    

    Original image
    Axis found from binary image
    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently developing my program using sample BluetoothChat program on android device. This
I am currently developing a program that uses C#'s Dictionary container (specifically, SortedDictionary). This
import string ## This part of the code initializes the program by recieving inputs
This program should first open an image, and then allow it to be manipulated
I currently wrote a small program for a manager at work. It inputs an
http://img136.imageshack.us/img136/3508/texturefailz.png This is my current program. I know it's terribly ugly, I found two
I'm having trouble getting a sample program to link correctly (in this case against
This is a current clip of my program, as it is, the datagridview is
This code below doesn't work correctly since my MFC program is in unicode circumstance.
If I understand this correctly, the program counter points to the address of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.