-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrop_circle.py
58 lines (52 loc) · 2.01 KB
/
crop_circle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import sys
import skimage
import skimage.filters
import skimage.transform
import skimage.feature
import skimage.draw
import numpy as np
def get_circle_params(image, radius_adjust=0, low=300, high=500):
thresh = skimage.filters.threshold_otsu(image)
binary = image >= thresh
edges = skimage.feature.canny(
skimage.img_as_ubyte(image),
sigma=3,
low_threshold=10,
high_threshold=50
)
hough_radii = np.arange(300, 500, 2)
hough_res = skimage.transform.hough_circle(edges, hough_radii)
[accums], [cx], [cy], [radius] = skimage.transform.hough_circle_peaks(hough_res, hough_radii, total_num_peaks=1)
return (cx, cy, radius)
def get_circle_coords(image, radius_adjust=0, low=300, high=500):
(cx, cy, radius) = get_circle_params(image, radius_adjust, low, high)
crr, ccc = skimage.draw.circle(cy, cx, radius + radius_adjust, image.shape)
return (crr, ccc)
def crop_circle(image, radius_adjust=0, low=300, high=500):
"""
Zero out all the pixels outside of the dish.
This method uses a Hough transform to automatically detect the location
and radius of the circle, though it needs bounds to speed up computation.
Parameters
----------
image : ndarray image
The original camera image
radius_adjust : int
How much to adjust the radius of the circle. Use this to, for instance,
delete additional space just within the boundaries of the dish to remove
artifacts
low : int
A lower bound on the radius of the circle, in pixels
high : int
An upper bound on the radius of the circle, in pixels
Returns
-------
new_image : ndarray image
A copy of the whole-dish image, with the pixels outside the circle
set to zero.
"""
(cx, cy, radius) = get_circle_params(image, radius_adjust, low, high)
crr, ccc = skimage.draw.circle(cy, cx, radius + radius_adjust, image.shape)
new_image = np.zeros_like(image)
new_image[crr, ccc] = image[crr, ccc]
return new_image