Region histogram

Python

Compute and plot the region histogram. [dependency] - numpy - matplotlib - pspylib

main 1 file
Region histogram..py
plot_region_histogram.py 1.6 KB
Region histogram..py 1619 bytes
import os
import numpy as np
import matplotlib.pyplot as plt
from pspylib.tiff.reader import TiffReader

def plot_region_histogram(region, size:tuple=(8,8)):
    # Be careful ifimum shift
    fig = plt.figure(figsize=size, facecolor='white')
    hist , edges = region_histogram(region)
    ## add dummy infimum value: len(hist)+1 = len(edges)
    pl_hist = np.insert(hist,0,0)
    plt.plot(edges*1000,pl_hist)
    plt.ylim([0, hist.max()])
    plt.xlim(edges.min()*1000,edges.max()*1000)
    return fig

def __optimal_bins(gray_image):
    vmin = np.min(gray_image)
    vmax = np.max(gray_image)
    # cvt Scale (Pico -> Nano)
    bound = int((vmax - vmin)* 1000)
    if bound < 10:
        bound = 128
    count = gray_image.size
    sqrt_count = int(np.sqrt(count))

    if (sqrt_count < bound) & (count < bound * 50):
        bins = sqrt_count                                                  
    else: bins = bound
    if bins > 128:
        bins = 128
    return bins

def region_histogram(region):
    bins = __optimal_bins(region)
    hist,edges = np.histogram(region,bins)
    return hist, edges

if __name__ == "__main__":
    samples_path = r"C:\Park Systems\SmartScan\samples"
    tiff_path = os.path.join(samples_path, "Image", "Cheese.tiff")
    tiff = TiffReader(tiff_path)
    Zdata = tiff.data.scanData.ZData
    header = tiff.data.scanHeader.scanHeader
    dshape = (int(header['height'][0]), int(header['width'][0]))
    tiff_image = np.reshape(Zdata,dshape)
    tiff_image = np.flipud(tiff_image)

    plot_region_histogram(tiff_image)
    plt.show()
Comments (1)
admin_alex
admin_alex 1 month, 1 week ago

It'll be more useful to me, if you provide pip package manager or wheel file, requirements.txt for pspylib library

Snippet Information
Author: jungyu.lee
Language: Python
Created: Oct 23, 2025
Updated: 0 minutes ago
Views: 51
Stars: 1

Links & Files

Additional Files (1):