def __init__(self, start, end, rate, step_size=50): self.start = start self.end = end self.rate = rate now = datetime.datetime.now() self.log_direc = "/media/data/Data/Logs/{}_{}_{}_{}_{}/".format( now.year, now.month, now.day, now.hour, now.minute) try: os.mkdir(self.log_direc) except FileExistsError as e: print(e) self.i = 0 self.shaker = shaker.Shaker() self.shaker.change_duty(self.start) self.step_size = step_size cam_num = camera.guess_camera_number() port = STEPPER_CONTROL self.ard = arduino.Arduino(port) self.motors = stepper.Stepper(self.ard) self.motors.move_motor(1, 100, '+') self.motors.move_motor(2, 100, '+') self.motors.move_motor(1, 100, '-') self.motors.move_motor(2, 100, '-') self.cam = camera.Camera(cam_num=cam_num) im = self.cam.get_frame() self.hex, self.center, self.crop, self.mask = self.find_hexagon(im) im = images.crop_and_mask(im, self.crop, self.mask) self.im_shape = im.shape im = images.draw_polygon(im, self.hex) im = images.draw_circle(im, self.center[0], self.center[1], 3) images.display(im)
def get_circles(frame): red = frame[:, :, 0] - frame[:, :, 2] red = images.threshold(red, 65) opened = images.opening(red, (31, 31)) w = opened.shape[1] im1, im2 = opened[:, :w//2], opened[:, w//2:] images.display(im1) m1 = list(images.center_of_mass(im1)) m2 = list(images.center_of_mass(im2)) m2[0] += w//2 return np.array([[m1[0], m1[1], 50], [m2[0], m2[1], 50]])
def __init__(self): self.shaker = shaker.Shaker() self.shaker.change_duty(600) port = STEPPER_CONTROL self.ard = arduino.Arduino(port) self.motors = stepper.Stepper(self.ard) cam_num = camera.guess_camera_number() self.cam = camera.Camera(cam_num=cam_num) im = self.cam.get_frame() self.hex, self.center, self.crop, self.mask = self.find_hexagon(im) im = images.crop_and_mask(im, self.crop, self.mask) self.im_shape = im.shape im = images.draw_polygon(im, self.hex) im = images.draw_circle(im, self.center[0], self.center[1], 3) images.display(im)
from particletracking import dataframes from labvision import video, images import numpy as np import filehandling file_store = "/media/data/Data/FirstOrder/Hysterisis/FlatPlate/Trial2/0.2_up_1.hdf5" file_vid = "/media/data/Data/FirstOrder/Hysterisis/FlatPlate/Trial2/0.2_up_1.MP4" metadata = dataframes.load_metadata(file_store) vid = video.ReadVideo(file_vid) box = metadata['crop'] frame = vid.read_frame(vid.num_frames - 1) frame = images.crop(frame, box) xmin = 750 xmax = 1250 ymin = 750 ymax = 1250 vertices = np.array([[xmin, ymin], [xmin, ymax], [xmax, ymax], [xmax, ymin]]) frame = images.draw_polygon(frame, vertices, thickness=3) images.save( frame, "/media/data/Data/FirstOrder/Hysterisis/FlatPlate/Trial2/HexagonFigures/box_end.png" ) images.display(frame)
p1 = [333, 1318] p2 = [1784, 528] midpoint = np.array([p1[0] - p2[0], p1[1] - p2[1]]) h = h + midpoint[1] x = x + midpoint[0] m = (p2[1] - p1[1]) / (p2[0] - p1[0]) a = -atan(m) print(a) points = np.vstack((x, h)).T print(points.shape) points = rotate_points(points, midpoint, -a) print(points[0]) print(points[1]) points[:, 0] += -points[0, 0] + p1[0] points[:, 1] += -points[0, 1] + p1[1] points = np.int32(points) # points = points.reshape((points.shape[0], 1, points.shape[1])) # lines = [((a[0], b[0]), (a[1], b[1])) for a, b in zip(points[1:], points[:-1])] # print(lines) im = cv2.polylines(im, [points], False, images.YELLOW, 10) im = images.rotate(im, 30) # im = images.draw_contours(im, [points], images.YELLOW, 10) images.display(im) images.save(im, direc + 'boundary.jpeg') # plt.savefig(direc+'boundary.jpeg', quality=95, dpi=300) # plt.show()
from labvision import video, images vid = video.ReadVideo("/home/ppxjd3/Code/labvision/labvision/data/numbered_video.mp4", frame_range=(0, 100, 10)) for f in range(vid.num_frames): frame = vid.read_next_frame() images.display(frame, f"{f}")
def test(self): filepath = os.path.join(data_dir, "maxresdefault.jpg") im = images.read_img(filepath) im = images.rotate(im, 90) images.display(im)
def run(direc, lattice_spacing=5): files = filehandling.get_directory_filenames(direc + '/*.png') print(files) savename = direc + '/data.hdf5' N = len(files) # load images ims = [images.load(f, 0) for f in tqdm(files, 'Loading images')] images.display(ims[0]) # Find Circles ims = [images.bgr_to_gray(im) for im in ims] circles = [ images.find_circles(im, 27, 200, 7, 16, 16) for im in tqdm(ims, 'Finding Circles') ] # Save data data = dataframes.DataStore(savename, load=False) for f, info in tqdm(enumerate(circles), 'Adding Circles'): data.add_tracking_data(f, info, ['x', 'y', 'r']) # Calculate order parameter calc = statistics.PropertyCalculator(data) calc.order() # Get the course graining width cgw = get_cgw(data.df.loc[0]) / 2 # Create the lattice points x = np.arange(0, max(data.df.x), lattice_spacing) y = np.arange(0, max(data.df.y), lattice_spacing) x, y = np.meshgrid(x, y) # Calculate the coarse order fields fields = [ coarse_order_field(data.df.loc[f], cgw, x, y) for f in tqdm(range(N), 'Calculating Fields') ] # Calculate the field threshold field_threshold = get_field_threshold(fields, lattice_spacing, ims[0]) # Find the contours representing the boundary in each frame contours = [ find_contours(f, field_threshold) for f in tqdm(fields, 'Calculating contours') ] # Multiply the contours by the lattice spacing contours = [c * lattice_spacing for c in contours] # Find the angle of the image to rotate the boundary to the x-axis a, c, p1, p2 = get_angle(ims[0]) print(p1) print(p2) # Rotate the selection points and the contours by the angle p1 = rotate_points(np.array(p1), c, a) p2 = rotate_points(np.array(p2), c, a) contours = [rotate_points(contour.squeeze(), c, a) for contour in contours] xmin = int(p1[0]) xmax = int(p2[0]) h = int(p1[1]) # Get the heights of the fluctuations from the straight boundary hs = [ get_h(contour, ims[0].shape, xmin, xmax, h) for contour in tqdm(contours, 'Calculating heights') ] # Calculate the fourier transforms for all the frames L = xmax - xmin pixels_to_mms = 195 / L print("pixels_to_mms = ", pixels_to_mms) # convert to mm hs = [h * pixels_to_mms for h in hs] L = L * pixels_to_mms x = np.linspace(0, L, len(hs[0])) np.savetxt(direc + '/x.txt', x) np.savetxt(direc + '/hs.txt', hs) # k, yplot = get_fourier(hs, L) return k, yplot
from labvision import video, images vid = video.ReadVideo( "/home/ppxjd3/Code/labvision/labvision/data/numbered_video.mp4") frame_numbers = [0, 1, 2, 3, 10, 11, 20, 21, 20, 19, 18] for f in frame_numbers: frame1 = vid.read_frame(f) frame2 = vid.read_frame(f) frame = images.hstack(frame1, frame2) images.display(frame, title=f'{f}')