StudyLover
  • Home
  • Study Zone
  • Profiles
  • Contact us
  • Sign in
StudyLover Program calculates Haralick texture features
Download
  1. Python
  2. Pyhton MCA (Machine Learning using Python)
  3. Programs
Program for Image Processing with Pillow : Natural Language Processing Pipeline Demonstration with spaCy
Programs

# main.py

# An alternative demonstration of image feature extraction using scikit-image.

# This program calculates Haralick texture features.

#

# Before running, you may need to install scikit-image, Pillow, and Matplotlib:

# pip install scikit-image Pillow matplotlib


from skimage.feature import graycomatrix, graycoprops

from PIL import Image, ImageDraw

import numpy as np

import matplotlib.pyplot as plt

import os


print("--- Starting Image Feature Extraction Demonstration with scikit-image ---")


# --- Section 1: Create a Sample Image with Texture ---

# We'll create the same sample image to provide a direct comparison.

IMAGE_FILENAME = "texture_sample_skimage.png"

try:

    # Create a new blank image

    img = Image.new('L', (256, 256), 'gray') # 'L' mode is for grayscale

    draw = ImageDraw.Draw(img)

    

    # Draw some lines to create texture

    for i in range(0, 256, 16):

        draw.line([(0, i), (256, i)], fill='white', width=4)

        draw.line([(i, 0), (i, 256)], fill='black', width=4)

        

    img.save(IMAGE_FILENAME)

    print(f"\n--- 1. Sample image '{IMAGE_FILENAME}' created successfully. ---")

except Exception as e:

    print(f"An error occurred while creating the sample image: {e}")



# --- Section 2: Load the Image and Extract Features ---

try:

    # Open the image and convert it to a NumPy array

    with Image.open(IMAGE_FILENAME) as img:

        image = np.array(img)


    print("\n--- 2. Displaying the image to be analyzed. ---")

    plt.imshow(image, cmap='gray')

    plt.title('Image with Texture (for scikit-image)')

    plt.show()


    print("\n--- 3. Calculating Haralick Texture Features. ---")

    # In scikit-image, you first compute the Gray-Level Co-occurrence Matrix (GLCM).

    # This matrix represents how often different combinations of pixel brightness

    # values (grey levels) occur in an image.

    glcm = graycomatrix(image, distances=[1], angles=[0], levels=256,

                        symmetric=True, normed=True)


    # From the GLCM, you can compute the Haralick properties.

    print("Haralick Features (calculated from GLCM):")

    feature_names = ['Contrast', 'Dissimilarity', 'Homogeneity', 'Energy', 'Correlation', 'ASM']

    

    for prop in feature_names:

        # graycoprops calculates the texture properties from the GLCM

        feature_val = graycoprops(glcm, prop.lower())[0, 0]

        print(f"- {prop:<30}: {feature_val:.4f}")


except FileNotFoundError:

    print(f"Error: The file '{IMAGE_FILENAME}' could not be found.")

except Exception as e:

    print(f"An error occurred during feature extraction: {e}")


# --- Clean up the created image file ---

finally:

    print("\n--- Cleaning up created image files. ---")

    if os.path.exists(IMAGE_FILENAME):

        os.remove(IMAGE_FILENAME)

        print(f"Removed '{IMAGE_FILENAME}'")


print("\n--- End of Demonstration ---")


Program for Image Processing with Pillow Natural Language Processing Pipeline Demonstration with spaCy
Our Products & Services
  • Home
Connect with us
  • Contact us
  • +91 82955 87844
  • Rk6yadav@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website