Click here to Skip to main content
15,795,331 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to Calculate volume, area, perimeter, major axis, minor axis and eccentricity for binary image of apple, tomato, lemon, orange, guava and gooseberry. So far calculated all I would be getting same value for both volume and area, how to calculate volume?

What I have tried:

import cv2
import pandas as pd
from skimage import measure

# List of binary images
image_list = ["/content/drive/MyDrive/Apple Sample 1/15 1.jpg", "/content/drive/MyDrive/Apple Sample 1/15 2.jpg", "/content/drive/MyDrive/Apple Sample 1/16 1.jpg"]

# Empty list to store extracted features
feature_list = []

# Iterate through the list of images
for image_name in image_list:
    # Read in binary image of apple
    binary_image = cv2.imread(image_name,0)

    # Find contours in the binary image
    contours, _ = cv2.findContours(binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    # Extract the first contour
    cnt = contours[0]

    # Calculate the perimeter of the contour
    perimeter = cv2.arcLength(cnt, True)

    # Extract region properties of apple
    regions = measure.regionprops(binary_image)

    # Extract features of interest
    for region in regions:
        volume = region.area
        area = region.area
        eccentricity = region.eccentricity
        major_axis = region.major_axis_length
        minor_axis = region.minor_axis_length

    # Create a dictionary with extracted features
    feature_dict = {"Volume":volume, "Area": area, "Perimeter": perimeter, "Eccentricity": eccentricity, "Major axis": major_axis, "Minor axis": minor_axis}
    # Append the features of each image to the feature_list

# Convert the list of dictionaries to a dataframe
df = pd.DataFrame(feature_list)

# Print the dataframe
Updated 25-Jan-23 5:50am

1 solution

See Solid Geometry[^] for the different formulae to apply to solid shapes.
Share this answer
Member 15201926 26-Jan-23 3:21am    
Can I get code in python for the above question
Richard MacCutchan 26-Jan-23 4:17am    
No, you are expected to write it. When you hve specific problems then people here will try to help you, but this site is not here to do your work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900