forked from matthiasnwt/feret
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5a555a
commit ef4814a
Showing
5 changed files
with
290 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2022 Matthias Nwt | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Feret | ||
|
||
Python module to calculate Feret maximum and minimum diameter. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
from scipy import ndimage | ||
import time, scipy.optimize | ||
|
||
|
||
|
||
class FeretDiameter(): | ||
|
||
def __init__(self, img, **kwargs): | ||
|
||
self.img = img | ||
|
||
if 'precision' in kwargs: | ||
self.precesion = kwargs['precision'] | ||
else: | ||
self.precesion = 1 | ||
|
||
if 'edge' in kwargs: | ||
self.edge = kwargs['edge'] | ||
else: | ||
self.edge = False | ||
|
||
|
||
self.degs = np.deg2rad(np.arange(0, 180+self.precesion, self.precesion)) | ||
self.ferets = np.zeros(len(self.degs)) | ||
self.ms = np.zeros(len(self.degs)) | ||
self.t_maxs = np.zeros(len(self.degs)) | ||
self.t_mins = np.zeros(len(self.degs)) | ||
self.p_maxs = np.zeros((len(self.degs), 2)) | ||
self.p_mins = np.zeros((len(self.degs), 2)) | ||
|
||
# start = time.perf_counter() | ||
self.find_points() | ||
# print('points', time.perf_counter() - start) | ||
self.calculate_center() | ||
self.calculate_ferets() | ||
# self.calculate_distance_matrix() | ||
|
||
self.minimize_feret() | ||
|
||
|
||
|
||
def find_points(self): | ||
""" | ||
Method find the points which are used to calcualte feret diameter | ||
""" | ||
|
||
if self.edge: | ||
ys, xs = np.nonzero(self.img) | ||
new_xs = np.concatenate((xs + 0.5, xs + 0.5, xs - 0.5, xs - 0.5, xs, xs + 0.5, xs - 0.5, xs, xs)) | ||
new_ys = np.concatenate((ys + 0.5, ys - 0.5, ys + 0.5, ys - 0.5, ys, ys, ys, ys + 0.5, ys - 0.5)) | ||
|
||
new_ys, new_xs = (new_ys * 2).astype(int), (new_xs * 2).astype(int) | ||
new_points = np.array([new_ys, new_xs]) | ||
|
||
self.contour = np.zeros((self.img.shape[0] * 2, self.img.shape[1] * 2)) | ||
self.contour[tuple(new_points)] = 1 | ||
|
||
edm = ndimage.morphology.distance_transform_edt(self.contour) | ||
self.contour[edm > 1] = 0 | ||
self.points = np.array(np.nonzero(self.contour)) | ||
else: | ||
self.contour = np.copy(self.img) | ||
edm = ndimage.morphology.distance_transform_edt(self.contour) | ||
self.contour[edm > 1] = 0 | ||
self.points = np.array(np.nonzero(self.contour)) | ||
|
||
|
||
|
||
def calculate_center(self): | ||
""" | ||
Method caluclates the center of the binary image. | ||
""" | ||
(self.y0, self.x0) = ndimage.measurements.center_of_mass(self.contour) | ||
|
||
|
||
def calculate_distances(self, angle): | ||
""" | ||
Method calculates the distance of two points at a givin angle. | ||
""" | ||
m = np.tan(angle) | ||
ds = np.cos(angle)*(self.y0-self.points[0])-np.sin(angle)*(self.x0-self.points[1]) | ||
max_i = np.argmax(ds) | ||
min_i = np.argmin(ds) | ||
|
||
t_max = self.points.T[max_i][0] - m * self.points.T[max_i][1] | ||
t_min = self.points.T[min_i][0] - m * self.points.T[min_i][1] | ||
|
||
feret = np.abs(t_max - t_min) / np.sqrt(1+m**2) | ||
|
||
return feret, m, t_max, t_min, self.points.T[max_i], self.points.T[min_i] | ||
|
||
|
||
def calculate_distances_func(self, angle, sign): | ||
""" | ||
Method calculates the distance of two points at a givin angle. | ||
""" | ||
m = np.tan(angle) | ||
ds = np.cos(angle)*(self.y0-self.points[0])-np.sin(angle)*(self.x0-self.points[1]) | ||
max_i = np.argmax(ds) | ||
min_i = np.argmin(ds) | ||
|
||
t_max = self.points.T[max_i][0] - m * self.points.T[max_i][1] | ||
t_min = self.points.T[min_i][0] - m * self.points.T[min_i][1] | ||
|
||
feret = np.abs(t_max - t_min) / np.sqrt(1+m**2) | ||
|
||
return sign * feret | ||
|
||
|
||
def minimize_feret(self): | ||
|
||
|
||
res_minferet = scipy.optimize.minimize( | ||
self.calculate_distances_func, | ||
x0=self.minferet_angle, | ||
args=(1, ), | ||
bounds=((0., np.pi),)) | ||
|
||
res_maxferet = scipy.optimize.minimize( | ||
self.calculate_distances_func, | ||
x0=self.maxferet_angle, | ||
args=(-1, ), | ||
bounds=((0., np.pi),)) | ||
|
||
self.maxferet = -res_maxferet.fun | ||
self.minferet = res_minferet.fun | ||
|
||
|
||
# plt.scatter(self.degs, self.ferets) | ||
# plt.axvline(self.minferet_angle, color='black') | ||
# plt.axvline(res.x, color='red') | ||
# plt.axhline(self.minferet, color='black') | ||
# plt.axhline(res.fun, color='red') | ||
|
||
# plt.show() | ||
|
||
|
||
def calculate_ferets(self): | ||
|
||
|
||
for i, angle in enumerate(self.degs): | ||
feret, m, t_max, t_min, p_max, p_min = self.calculate_distances(angle) | ||
|
||
self.ferets[i] = feret | ||
self.ms[i] = m | ||
self.t_maxs[i] = t_max | ||
self.t_mins[i] = t_min | ||
self.p_maxs[i] = p_max | ||
self.p_mins[i] = p_min | ||
|
||
|
||
self.maxferet_initial = np.max(self.ferets) | ||
self.minferet_initial = np.min(self.ferets) | ||
|
||
self.minferet_index = np.where(self.ferets == self.minferet_initial) | ||
self.maxferet_index = np.where(self.ferets == self.maxferet_initial) | ||
|
||
self.minferet_angle = self.degs[self.minferet_index] | ||
self.maxferet_angle = self.degs[self.maxferet_index] | ||
|
||
|
||
|
||
|
||
def calculate_distance_matrix(self): | ||
|
||
degs = np.deg2rad(np.arange(0, 180, self.precesion)) | ||
ms = np.tan(degs) | ||
|
||
|
||
coss = np.cos(degs).reshape(1, -1) | ||
sins = np.sin(degs).reshape(1, -1) | ||
|
||
ps0 = (self.y0-self.points[0]).reshape(-1, 1) | ||
ps1 = (self.x0-self.points[1]).reshape(-1, 1) | ||
|
||
ds = np.multiply(coss, ps0) - np.multiply(sins, ps1) | ||
|
||
max_is = np.argmax(ds, axis=0) | ||
min_is = np.argmin(ds, axis=0) | ||
|
||
t_maxs = self.points[0][max_is] - ms * self.points[1][max_is] | ||
t_mins = self.points[0][min_is] - ms * self.points[1][min_is] | ||
|
||
ferets = np.abs(t_maxs - t_mins) / np.sqrt(1+ms**2) | ||
|
||
self.ferets = ferets | ||
|
||
self.maxferet_initial = np.max(self.ferets) | ||
self.minferet_initial = np.min(self.ferets) | ||
|
||
self.minferet_index = np.where(self.ferets == self.minferet_initial) | ||
self.maxferet_index = np.where(self.ferets == self.maxferet_initial) | ||
|
||
self.minferet_angle = self.degs[self.minferet_index] | ||
self.maxferet_angle = self.degs[self.maxferet_index] | ||
|
||
|
||
|
||
|
||
def __call__(self): | ||
if self.edge: | ||
return self.maxferet / 2, self.minferet / 2 | ||
else: | ||
return self.maxferet, self.minferet | ||
|
||
|
||
def plot(self, distance=False): | ||
plt.imshow(self.contour) | ||
xs = np.linspace(0, self.contour.shape[1]) | ||
for m, t_max, t_min, p_max, p_min in zip(self.ms, self.t_maxs, self.t_mins, self.p_maxs, self.p_mins): | ||
ys = xs * m + t_max | ||
plt.plot(xs, ys, color='gray') | ||
|
||
ys = xs * m + t_min | ||
plt.plot(xs, ys) | ||
|
||
# plt.plot((p_max[1], p_min[1]), (p_max[0], p_min[0])) | ||
|
||
plt.scatter(self.x0, self.y0) | ||
plt.ylim(0, self.contour.shape[0]) | ||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from feret.FeretDiameter import FeretDiameter | ||
|
||
def calculate(img, **kwargs): | ||
|
||
feret = FeretDiameter(img, **kwargs) | ||
# feret.plot() | ||
return feret() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from setuptools import setup, find_packages | ||
|
||
import pathlib | ||
|
||
here = pathlib.Path(__file__).parent.resolve() | ||
# The text of the README file | ||
long_description = (here / "README.md").read_text(encoding="utf-8") | ||
|
||
# This call to setup() does all the work | ||
setup( | ||
name="feret", | ||
version="0.1.0", | ||
description="Calculates the maximum and minimum feret diameter", | ||
long_description=long_description, | ||
long_description_content_type="text/markdown", | ||
url="https://github.com/matthiasnwt/feret", | ||
author="Matthias Nwt", | ||
license="MIT", | ||
keywords="feret, feretdiameter, maxferet, minferet", | ||
classifiers=[ | ||
"License :: OSI Approved :: MIT License", | ||
"Programming Language :: Python :: 3", | ||
"Programming Language :: Python :: 3.9", | ||
], | ||
packages=find_packages(), | ||
include_package_data=True, | ||
install_requires=["numpy", "scipy", "matplotlib"], | ||
project_urls={ | ||
"Bug Reports": "https://github.com/matthiasnwt/feret/issues", | ||
"Source": "https://github.com/matthiasnwt/feret/", | ||
}, | ||
) |