Update morphocut.py

This commit is contained in:
tpollina 2020-01-30 21:52:12 -08:00 committed by GitHub
parent 9d72a1e8bb
commit 70bf0a9680
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -22,95 +22,148 @@ from morphocut.stat import RunningMedian
from morphocut.str import Format from morphocut.str import Format
from morphocut.stream import TQDM, Enumerate from morphocut.stream import TQDM, Enumerate
# import_path = "/data-ssd/mschroeder/Datasets/Pyrocystis_noctiluca/RAW" from skimage.feature import canny
import_path = "/home/pi/Desktop/PlanktonScope_acquisition/01_16_2020/afternoon/14_1" from skimage.color import rgb2gray, label2rgb
export_path = "/home/pi/Desktop/PlanktonScope_acquisition/01_16_2020/14_1_export" from skimage.morphology import disk
archive_fn = os.path.join(export_path, "14_1_morphocut_processed.zip") from skimage.morphology import erosion, dilation, closing
from skimage.measure import label, regionprops
import_path = "/media/tpollina/rootfs/home/pi/Desktop/PlanktonScope_acquisition/01_17_2020/RAW"
export_path = "/media/tpollina/rootfs/home/pi/Desktop/PlanktonScope_acquisition/01_17_2020/"
CLEAN = os.path.join(export_path, "CLEAN")
os.makedirs(CLEAN, exist_ok=True)
OBJECTS = os.path.join(export_path, "OBJECTS")
os.makedirs(OBJECTS, exist_ok=True)
archive_fn = os.path.join(export_path, "ecotaxa_export.zip")
# Meta data that is added to every object # Meta data that is added to every object
global_metadata = { global_metadata = {
"acq_instrument": "Planktoscope", "acq_instrument": "Planktoscope",
"process_datetime": datetime.datetime.now(), "process_datetime": datetime.datetime.now(),
"sample_project": "PlanktonScope Villefranche",
"sample_ship": "Kayak de Fabien",
"sample_operator": "Thibaut Pollina",
"sample_id": "Flowcam_PlanktonScope_comparison",
"sample_sampling_gear": "net",
"sample_time":150000,
"sample_date":16112020,
"object_lat": 43.696146,
"object_lon": 7.308359,
"acq_fnumber_objective": 16,
"acq_celltype": 200,
"process_pixel": 1.19,
"acq_camera": "Pi Camera V2.1",
"acq_instrument": "PlanktonScope V2.1",
"acq_software": "Node-RED Dashboard and raw python",
"acq_instrument_ID": "copepode",
"acq_volume": 24,
"acq_flowrate": "Unknown",
"acq_camera.resolution" : "(3280, 2464)",
"acq_camera.iso" : 60,
"acq_camera.shutter_speed" : 100,
"acq_camera.exposure_mode" : 'off',
"acq_camera.awb_mode" : 'off',
"acq_nb_frames" : 1000
} }
if __name__ == "__main__": # Define processing pipeline
print("Processing images under {}...".format(import_path)) with Pipeline() as p:
# Recursively find .jpg files in import_path.
# Sort to get consective frames.
abs_path = Find(import_path, [".jpg"], sort=True, verbose=True)
# Create export_path in case it doesn't exist # Extract name from abs_path
os.makedirs(export_path, exist_ok=True) name = Call(lambda p: os.path.splitext(os.path.basename(p))[0], abs_path)
# Define processing pipeline # Read image
with Pipeline() as p: img = ImageReader(abs_path)
# Recursively find .jpg files in import_path.
# Sort to get consective frames.
abs_path = Find(import_path, [".jpg"], sort=True, verbose=True)
# Extract name from abs_path # Apply running median to approximate the background image
name = Call(lambda p: os.path.splitext(os.path.basename(p))[0], abs_path) flat_field = RunningMedian(img, 10)
# Read image # Correct image
img = ImageReader(abs_path) img = img / flat_field
# Apply running median to approximate the background image # Rescale intensities and convert to uint8 to speed up calculations
flat_field = RunningMedian(img, 10) img = RescaleIntensity(img, in_range=(0, 1.1), dtype="uint8")
# Correct image # Convert image to uint8 gray
img = img / flat_field img_gray = RGB2Gray(img)
# Rescale intensities and convert to uint8 to speed up calculations img_gray = Call(img_as_ubyte, img_gray)
img = RescaleIntensity(img, in_range=(0, 1.1), dtype="uint8")
# Show progress bar for frames img_canny = Call(canny, img_gray, sigma=0.3)
TQDM(Format("Frame {name}", name=name))
# Convert image to uint8 gray img_dilate = Call(dilation, img_canny)
img_gray = RGB2Gray(img)
img_gray = Call(img_as_ubyte, img_gray)
# Apply threshold find objects img_closing = Call(closing, img_dilate)
threshold = 204 # Call(skimage.filters.threshold_otsu, img_gray)
mask = img_gray < threshold
# Write corrected frames mask = Call(erosion, img_closing)
frame_fn = Format(os.path.join(export_path, "{name}.jpg"), name=name)
ImageWriter(frame_fn, img)
# Find objects # Show progress bar for frames
regionprops = FindRegions( TQDM(Format("Frame {name}", name=name))
mask, img_gray, min_area=100, padding=10, warn_empty=name
)
# For an object, extract a vignette/ROI from the image # Apply threshold find objects
roi_orig = ExtractROI(img, regionprops, bg_color=255) #threshold = 204 # Call(skimage.filters.threshold_otsu, img_gray)
roi_gray = ExtractROI(img_gray, regionprops, bg_color=255) #mask = img_gray < threshold
# Generate an object identifier # Write corrected frames
i = Enumerate() frame_fn = Format(os.path.join(CLEAN, "{name}.jpg"), name=name)
object_id = Format("{name}_{i:d}", name=name, i=i) ImageWriter(frame_fn, img)
# Calculate features. The calculated features are added to the global_metadata. # Find objects
# Returns a Variable representing a dict for every object in the stream. regionprops = FindRegions(
meta = CalculateZooProcessFeatures( mask, img_gray, min_area=1000, padding=10, warn_empty=name
regionprops, prefix="object_", meta=global_metadata )
)
# If CalculateZooProcessFeatures is not used, we need to copy global_metadata into the stream:
# meta = Call(lambda: global_metadata.copy())
# https://github.com/morphocut/morphocut/issues/51
# Add object_id to the metadata dictionary # For an object, extract a vignette/ROI from the image
meta["object_id"] = object_id roi_orig = ExtractROI(img, regionprops, bg_color=255)
# Generate object filenames roi_orig
orig_fn = Format("{object_id}.jpg", object_id=object_id) # Generate an object identifier
gray_fn = Format("{object_id}-gray.jpg", object_id=object_id) i = Enumerate()
#Call(print,i)
# Write objects to an EcoTaxa archive: object_id = Format("{name}_{i:d}", name=name, i=i)
# roi image in original color, roi image in grayscale, metadata associated with each object #Call(print,object_id)
EcotaxaWriter(archive_fn, [(orig_fn, roi_orig), (gray_fn, roi_gray)], meta)
# Progress bar for objects object_fn = Format(os.path.join(OBJECTS, "{name}.jpg"), name=object_id)
TQDM(Format("Object {object_id}", object_id=object_id)) ImageWriter(object_fn, roi_orig)
# Execute pipeline
p.run()
# Calculate features. The calculated features are added to the global_metadata.
# Returns a Variable representing a dict for every object in the stream.
meta = CalculateZooProcessFeatures(
regionprops, prefix="object_", meta=global_metadata
)
# If CalculateZooProcessFeatures is not used, we need to copy global_metadata into the stream:
# meta = Call(lambda: global_metadata.copy())
# https://github.com/morphocut/morphocut/issues/51
# Add object_id to the metadata dictionary
meta["object_id"] = object_id
# Generate object filenames
orig_fn = Format("{object_id}.jpg", object_id=object_id)
# Write objects to an EcoTaxa archive:
# roi image in original color, roi image in grayscale, metadata associated with each object
EcotaxaWriter(archive_fn, (orig_fn, roi_orig), meta)
# Progress bar for objects
TQDM(Format("Object {object_id}", object_id=object_id))
import datetime
BEGIN = datetime.datetime.now()
# Execute pipeline
p.run()
END = datetime.datetime.now()
print("MORPHOCUT :"+str(END-BEGIN))