Skip to content

Instantly share code, notes, and snippets.

@xmichaelx
Last active November 28, 2016 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xmichaelx/7b4d73d644eabf722095 to your computer and use it in GitHub Desktop.
Save xmichaelx/7b4d73d644eabf722095 to your computer and use it in GitHub Desktop.
import sys
from skimage import io, color
from scipy.ndimage.measurements import label, sum
from skimage.morphology import convex_hull_image
def main():
if len(sys.argv) < 4:
print("Input format: <filename> <minimum size of detected shape in pixels> <threshold between 0 and 1>")
return
filename = sys.argv[1]
minimum_pixel_size = int(sys.argv[2])
threshold = float(sys.argv[3])
image = io.imread(filename)
gray_image = color.rgb2gray(image)
binary = gray_image < threshold
segmented, num_segments = label(binary)
for i in range(1,num_segments+1):
total_pixels_in_shape = sum(segmented == i)
if total_pixels_in_shape > minimum_pixel_size:
convex_hull = convex_hull_image(segmented == i)
area = sum(convex_hull)
print("Shape number: " + str(i) + " shape area (in pixels): " + str(area))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment