himbeles / gridfit

Simple Python library for fitting a (rectangular) grid of shapes in an image to a set of points.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

gridfit

github action codecov

Simple Python library for fitting a (rectangular) grid of shapes in an image to a set of points.

Note that the current beta version still lacks a number of features.

gridfit example

Requirements

This module requires Python >= 3.8 and the following packages:

  • numpy
  • scipy
  • opencv-python
  • matplotlib

Installation

Install with pip

$ pip install git+https://github.com/nelsond/gridfit

Example usage

First, find the dominant angle of a rectangular grid in an image (uses the standard deviation of the Radon transformation).

import numpy as np
from gridfit.rect import find_dominant_angle

image = np.load('data.npy')
angle = find_dominant_angle(image)

After finding the dominant angle of the rectangular grid, we can fit Gaussian-shaped blobs to the one-dimensional projection of the data. This allows us to extract the coordinates of all points on the rectangular grid.

import matplotlib.pyplot as plt
from gridfit.rect import fit_grid

grid = fit_grid(image, angle, min_rel_height=0.2)

plt.imshow(image)
for col in grid:
    for row in col:
        plt.plot(row[1], row[0], 'ro', mfc='none', ms=10)

Once the grid has been determined, we can use a set of ROIs to analyze the data.

from gridfit.roi import CircularROI, ROIDataset

rois = []
for point in grid.reshape(-1, 2):
    roi = CircularROI(point, 15)
    rois.append(roi)

ds = ROIDataset(image, rois)
ds.plot(show_center=False, imshow_kwargs=dict(cmap=plt.cm.Greys_r))

# determine sum across each ROI on the grid
summed = ds.sum().reshape(*grid.shape[:2], -1)

# determine centroid in each ROI (first moment)
centroids = ds.centroid(absolute=True).reshape(*grid.shape[:2], -1)

# determine rms size in each ROI (square root of the second moment)
rms_size = ds.rms_size().reshape(*grid.shape[:2], -1)

Development

Install requirements for development environment

$ pip install .[dev]

Run tests

$ pytest tests/

Generate coverage report

$ pytest --cov=gridfit --cov-report html tests/

About

Simple Python library for fitting a (rectangular) grid of shapes in an image to a set of points.

License:MIT License


Languages

Language:Python 100.0%