img_to_path / outline.pyon commit binary grating code (3ae49cb)
   1import cv2
   2import numpy as np
   3import sys
   4
   5SPEED = 100         # um/s
   6REAL_DIM = 100.0    # um
   7Z = 0.0             # um
   8
   9np.set_printoptions(threshold=sys.maxsize)
  10
  11def row(x, y, z, on, t):
  12    return "1\t{:.3f}\t{:.3f}\t{:.3f}\t{:.3f}\t{:d}\n".format(x, y, z, on, t)
  13
  14
  15im = cv2.imread('test_img/test_img_1.png', cv2.IMREAD_GRAYSCALE)
  16dim = im.shape
  17
  18if (dim[0] != dim[1]):
  19    print("Error: image must be square")
  20    sys.exit()
  21
  22output = ""
  23
  24# Go to origin point (laser off)
  25output += row(0.0, 0.0, Z, 0.0, SPEED)
  26
  27result_fill = np.ones(dim, np.uint8) * 255
  28result_borders = np.zeros(dim, np.uint8)
  29
  30# the '[:-1]' is used to skip the contour at the outer border of the image
  31contours = cv2.findContours(im, cv2.RETR_LIST,
  32                            cv2.CHAIN_APPROX_SIMPLE)[0][:-1]
  33
  34print(len(contours))
  35print(contours[-1][:10])
  36print(contours[0].shape)
  37
  38cv2.drawContours(result_fill, contours, -1, 0, -1)
  39cv2.drawContours(result_borders, contours, -1, 255, 1)
  40
  41for contour in contours:
  42    for i, point in enumerate(contour):
  43        point_real = point[0] / dim[0] * REAL_DIM
  44        laser_on = 0.0 if i == 0 else 1.0
  45        output += row(point_real[0], point_real[1], Z, laser_on, SPEED)
  46        
  47
  48# xor the filled result and the borders to recreate the original image
  49#result = result_fill ^ result_borders
  50#
  51#cv2.imshow('image', result_borders)
  52#cv2.waitKey(0)
  53
  54output += row(0.0, 0.0, Z, 0.0, SPEED)
  55
  56with open('path.txt', 'w') as f:
  57    f.write(output)
  58
  59
  60# Questions
  61# Thickness of lines
  62# Stage uncertainty/increment
  63# Sample dimensions
  64# Dwell time