def ok(): if variable.get() == 'Sobel': oper = 1 if (int(e.get()) % 2) != 0: size = int(e.get()) else: size = 3 Gy = ed.EdgeDetection(input_file, oper, size)[0] Gx = ed.EdgeDetection(input_file, oper, size)[1] G = ed.EdgeDetectionNormalized(Gy, Gx) Edge = ed.EdgeDetectionAngle(Gy, Gx, G) elif variable.get() == 'Prewitt': oper = 2 if (int(e.get()) % 2) != 0: size = int(e.get()) else: size = 3 Gy = ed.EdgeDetection(input_file, oper, size)[0] Gx = ed.EdgeDetection(input_file, oper, size)[1] G = ed.EdgeDetectionNormalized(Gy, Gx) Edge = ed.EdgeDetectionAngle(Gy, Gx, G) elif variable.get() == 'Compass': if (int(e.get()) % 2) != 0: size = int(e.get()) else: size = 3 Edge = co.Compass(input_file, size, op.Sobel(size)[0]) elif variable.get() == 'Nevatia Babu': if (int(e.get()) % 2) != 0: size = int(e.get()) else: size = 3 Edge = nbo.Nevatia(input_file, size, op.Sobel(size)[0]) img = ImageTk.PhotoImage(image=Image.fromarray(np.uint8(Edge))) #result = tk.Label(root, image = img) result.image = img result.config(image = result.image)
def find_edge_features(image, padding): edgeDetector = EdgeDetection.EdgeDetector() edgeFeatures = edgeDetector.find_features(image, padding) features = [] for i in edgeFeatures: features.append([i.x, i.y]) FD = FeatureDescription.FeatureDescription() descriptors = FD.SIFT(features, image) return features, descriptors
def predict(self, image): orig_width = image.shape[1] orig_height = image.shape[0] if self.use_hed: edge_image = self.hed.get_edge_image(image, orig_width, orig_height, normalized=False) edge_image = np.reshape( edge_image, (edge_image.shape[0], edge_image.shape[1], 1)) else: edge_image = ed.auto_canny(image, self.use_multichannel) if not self.use_multichannel: edge_image = np.reshape( edge_image, (edge_image.shape[0], edge_image.shape[1], 1)) # Zero padding to make square if edge_image.shape[0] > edge_image.shape[1]: x_padding = edge_image.shape[0] - edge_image.shape[1] x_padding_start = round(x_padding / 2.0) x_end = x_padding_start + edge_image.shape[1] new_edge_image = np.zeros( (edge_image.shape[0], edge_image.shape[1] + x_padding, edge_image.shape[2])) new_edge_image[:, x_padding_start:x_end, :] = edge_image[:, :, :] edge_image = new_edge_image elif edge_image.shape[1] > edge_image.shape[0]: y_padding = edge_image.shape[1] - edge_image.shape[0] y_padding_top = round(y_padding / 2.0) y_end = y_padding_top + edge_image.shape[0] new_edge_image = np.zeros( (edge_image.shape[0] + y_padding, edge_image.shape[1], edge_image.shape[2])) new_edge_image[y_padding_top:y_end, :, :] = edge_image[:, :, :] edge_image = new_edge_image # Run exhaustive sliding window result = [] model = self.__get_model() model.load_weights(self.weight_file) window_width = self.image_width window_height = self.image_height scales = range(1, 6) overlap = 0.5 for scale in scales: image_width = round(scale * window_width) image_height = round(scale * window_height) resize_x = float(orig_width) / float(image_width) resize_y = float(orig_height) / float(image_height) steps = int(((scale / overlap) - 1)) for x in range(0, steps): x_offset = x * (window_width * overlap) for y in range(0, steps): y_offset = y * (window_height * overlap) resized_image = np.array(cv2.resize( edge_image, (image_width, image_height)), dtype=np.float) if self.use_hed: resized_image = np.reshape(resized_image, (resized_image.shape[0], resized_image.shape[1], 1)) xmin = int(x_offset) xmax = int(x_offset + window_width) ymin = int(y_offset) ymax = int(y_offset + window_height) window = resized_image[ymin:ymax, xmin:xmax] window = np.reshape( window, (1, window.shape[0], window.shape[1], window.shape[2])) window = window / 255.0 prediction = model.predict(window, 1)[0] true_xmin = int(x_offset * resize_x) true_ymin = int(y_offset * resize_y) true_xmax = true_xmin + int(window_width * resize_x) true_ymax = true_ymin + int(window_height * resize_y) window_result = [ true_xmin, true_ymin, true_xmax, true_ymax, prediction ] result.append(window_result) return result
croppedbase=ito.cropper2(noforce,cropside) croppedsidechecks=[ito.cropper2(extreme1[0],cropside),ito.cropper2(extreme2[0],cropside)] croppedtopchecks=[ito.cropper2(extreme1[1],croptop),ito.cropper2(extreme2[1],croptop)] pixelsize=0.75e-6 #Cross correlation cutpoint=20 # y pixel to use for cross correlation guassfitl=20 # Number of data points to each side to use for guass fit #Side Edge detection sideimaparam=[-1*np.max(croppedsidechecks[0])/3,40,.05] #[threshval,obsSize,cannysigma] sidebackground=False sidethreshtest=ede.edgedetector(croppedsidechecks[0],sidebackground,*sideimaparam) #Top Edge detection topimaparam=[-1*np.max(croppedtopchecks[0])/1.7,40,.05] #[threshval,obsSize,cannysigma] topbackground=False topthreshtest=ede.edgedetector(croppedtopchecks[0],topbackground,*topimaparam) topthreshtest2=ede.edgedetector(croppedtopchecks[1],topbackground,*topimaparam) ax1 = fig.add_subplot(gs[:2, :2]) ax2 = fig.add_subplot(gs[0, 2:]) ax3 = fig.add_subplot(gs[1, 2:]) ax4 = fig.add_subplot(gs[1:, :2]) ax5 = fig.add_subplot(gs[1:, 2:])
def __data_generation(self, labels_temp): x = np.empty((self.batch_size, self.image_height, self.image_width, self.channels)) y = np.empty((self.batch_size, self.label_dim)) for i, f in enumerate(labels_temp): image = cv2.imread(os.path.join(self.images_dir, f + ".jpg")) orig_width = int(image.shape[1]) orig_height = int(image.shape[0]) # Generate edge image result = ed.auto_canny(image, self.multi_channel) if not self.multi_channel: result = np.reshape(result, (result.shape[0], result.shape[1], 1)) # Read ground truth bboxes = [] tree = et.parse(os.path.join(self.labels_dir, f + ".xml")) root = tree.getroot() for obj in root.findall("./object"): label_name = obj.find('name').text label = self.class_labels.index(label_name) bndbox = obj.find('bndbox') xmin = int(bndbox.find('xmin').text) ymin = int(bndbox.find('ymin').text) xmax = int(bndbox.find('xmax').text) ymax = int(bndbox.find('ymax').text) bboxes.append((xmin, ymin, xmax, ymax, label)) if np.random.randint(0, 100) <= 50: # Create none class window # Create label vector label = 0.0 # Mask out any objects with white noise for box in bboxes: result[box[1]:box[3], box[0]:box[2]] = np.zeros( (box[3] - box[1], box[2] - box[0], self.channels)) # Cut out random window scale = np.random.randint(1, 6) rescaled_width = scale * orig_width rescaled_height = scale * orig_height while self.image_height >= rescaled_height or self.image_width >= rescaled_width: scale += 1 rescaled_width = scale * orig_width rescaled_height = scale * orig_height result = cv2.resize(result, (rescaled_width, rescaled_height)) xmin = np.random.randint(0, rescaled_width - self.image_width) ymin = np.random.randint(0, rescaled_height - self.image_height) xmax = xmin + self.image_width ymax = ymin + self.image_height window = result[ymin:ymax, xmin:xmax] else: # Create object class window # Select random target object target_box = random.choice(bboxes) bboxes.remove(target_box) # Create label vector label = 1.0 # Find image region with object (bbox + margin) if target_box[2] - target_box[0] > target_box[3] - target_box[ 1]: margin = int(float(target_box[2] - target_box[0]) * 0.2) window_xmin = max(target_box[0] - margin, 0) window_xmax = min(target_box[2] + margin, orig_width) window_ymin = max(target_box[1] - margin, 0) window_ymax = min(target_box[3] + margin, orig_height) cutout = result[window_ymin:window_ymax, window_xmin:window_xmax] else: margin = int(float(target_box[3] - target_box[1]) * 0.2) window_ymin = max(target_box[1] - margin, 0) window_ymax = min(target_box[3] + margin, orig_height) window_xmin = max(target_box[0] - margin, 0) window_xmax = min(target_box[2] + margin, orig_width) cutout = result[window_ymin:window_ymax, window_xmin:window_xmax] if self.use_augmentation: # Augment 50% of data points if np.random.randint(0, 100) <= 50: rand_val = np.random.randint(0, 3) # Resize to min 50% of size if rand_val == 0: aspect_ration = float(cutout.shape[1]) / float( cutout.shape[0]) new_height = float( cutout.shape[0]) * np.random.uniform(0.5, 1.0) new_width = new_height * aspect_ration cutout = cv2.resize( cutout, (int(new_width), int(new_height))) # Rotate by max +/- 90° elif rand_val == 1: angle = round(np.random.uniform(-1, 1) * 90.0) image_center = tuple( np.array(cutout.shape[1::-1]) / 2) rot_mat = cv2.getRotationMatrix2D( image_center, angle, 1.0) cutout = cv2.warpAffine(cutout, rot_mat, cutout.shape[1::-1], flags=cv2.INTER_LINEAR) # Flip image vertically else: cutout = cv2.flip(cutout, 1) if not self.multi_channel: cutout = np.reshape(cutout, (cutout.shape[0], cutout.shape[1], 1)) # Place image region centred on square black background if cutout.shape[1] > cutout.shape[0]: window = np.zeros( (cutout.shape[1], cutout.shape[1], self.channels)) margin = int((window.shape[0] - cutout.shape[0]) / 2) window[margin:(margin + cutout.shape[0]), :, :] = cutout else: window = np.zeros( (cutout.shape[0], cutout.shape[0], self.channels)) margin = int((window.shape[1] - cutout.shape[1]) / 2) window[:, margin:(margin + cutout.shape[1]), :] = cutout window = cv2.resize(window, (self.image_width, self.image_height)) window = np.array(window, dtype=np.float) if not self.multi_channel: window = np.reshape(window, (window.shape[0], window.shape[1], 1)) window /= 255.0 x[i] = window y[i] = label return x, y
#%% #Cross correlation cutpoint = 50 # y pixel to use for cross correlation guassfitl = 20 # Number of data points to each side to use for guass fit #Edge detection imaparam = [-20, 20, .01] #[threshval,obsSize,cannysigma] fitfunc = df.pol2ndorder #function ie def(x,a,b) to fit to find properties fitguess = [0, 1, 1] clinyguess = 214 #Guess at the center line (helpful if parts of pipette are further than droplet) pixrange = [60, 25, 25] #xy bounding box to use in fit #Specify an image to use as a background (needs same dim as images being analysed) #Or can set to False background = False testedge = ede.edgedetector(croppedex1, background, *imaparam) fig = plt.figure(figsize=(8, 4)) plt.imshow(croppedex1, cmap=plt.cm.gray) plt.plot(testedge[:, 0], testedge[:, 1], 'b.', markersize=1) croppedforfit = testedge[(testedge[:, 1] < yanalysisc[1]) & (testedge[:, 1] > yanalysisc[0])] testfit = df.datafitter(croppedforfit, False, pixrange, 1, fitfunc, fitguess) xvals = np.arange(0, 10) yvals = df.pol2ndorder(xvals, *testfit[-2]) plt.plot(xvals + testfit[0], yvals + testfit[1], 'r-') plt.ylim(np.min(testedge[:, 1]), np.max(testedge[:, 1])) #%% specfolder = "E:/SpeedScan/5umreturn_1/" allimages = ito.omestackimport(specfolder) allimages = ede.cropper(allimages, *croppoints) #%%
#%% #Import the image imagestack = ito.stackimport(dataDR + r"\1ums.tif") #%% #Select the minimum (1s) and maximum (2s) crop locations x1c = 300 x2c = 900 y1c = 400 y2c = 1000 croppoints = [x1c, x2c, y1c, y2c] fig, ax = plt.subplots(nrows=2, ncols=2) testimage1 = imagestack[0] testimage2 = imagestack[-1] croptest1 = ede.cropper(testimage1, *croppoints) croptest2 = ede.cropper(testimage2, *croppoints) ax[0, 0].imshow(testimage1) ax[0, 1].imshow(testimage2) ax[1, 0].imshow(croptest1) ax[1, 1].imshow(croptest2) #%% #Crop all of the images and plot a cut at a y value to test correlation shift croppedimages = ede.cropper(imagestack, *croppoints) cutpixely = -50 a = croppedimages[0, cutpixely]
#Cross correlation cutpoint = 5 # y pixel to use for cross correlation guassfitl = 20 # Number of data points to each side to use for guass fit #Edge detection imaparam = [-90, 20, .05] #[threshval,obsSize,cannysigma] fitfunc = df.pol2ndorder #function ie def(x,a,b) to fit to find properties fitguess = [0, 1, 1] pixrange = [ 60, 60, 25 ] #first two are xy bounding box for fit, last is where to search for droplet tip #Specify an image to use as a background (needs same dim as images being analysed) #Or can set to False background = False threshtest = ede.edgedetector(croppedex1, background, *imaparam) fig, ax = plt.subplots(1) ax.imshow(croppedex1, cmap=plt.cm.gray) ax.plot(threshtest[:, 0], threshtest[:, 1], 'r.', markersize=1) ax.axhline(cutpoint, ls='--') ax.axis('off') scalebar = ScaleBar(0.75e-6, frameon=False, location='upper right') # 1 pixel = 0.2 meter ax.add_artist(scalebar) #%% folderpaths, foldernames, dropProp = ito.foldergen(os.getcwd()) #%%
#Set working directory to data location os.chdir(dataDR) #%% allimages = ito.stackimport(dataDR + "\Translate1ums5xob.tif") #Select the minimum (1s) and maximum (2s) crop locations x1c = 9 x2c = 750 y1c = 715 y2c = 898 croppoints = [x1c, x2c, y1c, y2c] fig, ax = plt.subplots(nrows=2, ncols=2) testimage1 = allimages[0] testimage2 = allimages[-1] croptest1 = ede.cropper(testimage1, *croppoints) croptest2 = ede.cropper(testimage2, *croppoints) ax[0, 0].imshow(testimage1) ax[0, 1].imshow(testimage2) ax[1, 0].imshow(croptest1) ax[1, 1].imshow(croptest2) #%% a = croppedimages[0, -10] b = croppedimages[-1, -10] plt.plot(a) plt.plot(b) #%% alldat = np.zeros([croppedimages.shape[0], croppedimages.shape[2] * 2 - 1, 2])
#Or just one file #%% plt.imshow(imageframes[100]) croppoints = (np.floor(plt.ginput(2))) croppoints = croppoints.T.flatten().astype(int) imtest = ito.cropper(imageframes[100], *croppoints) plt.imshow(imtest) #%% #Edge detection plt.imshow(imtest, cmap=plt.cm.gray) imaparam = [-100, 20, .05] #[threshval,obsSize,cannysigma] #Specify an image to use as a background (needs same dim as images being analysed) #Or can set to False background = False threshtest = ede.edgedetector(imtest, background, *imaparam) plt.plot(threshtest[:, 0], threshtest[:, 1], 'g.') comloc = np.mean(threshtest, axis=0) plt.plot(*comloc, 'ro') lengtharr = imageframes.shape[0] #%% serieslength = imageframes.shape[0] croppedimages = ito.cropper(imageframes, *croppoints) #%% edgevalsdust = ede.seriesedgedetect(croppedimages, background, *imaparam) comlocs = np.zeros([serieslength, 2]) for i in range(serieslength): comlocs[i] = np.mean(edgevalsdust[i], axis=0) #%%
def predict(self, image, use_refinement=True): orig_width = image.shape[1] orig_height = image.shape[0] if self.use_hed: edge_image = self.hed.get_edge_image(image, orig_width, orig_height, normalized=False) edge_image = np.reshape( edge_image, (edge_image.shape[0], edge_image.shape[1], 1)) else: edge_image = ed.auto_canny(image, self.use_multichannel) if not self.use_multichannel: edge_image = np.reshape( edge_image, (edge_image.shape[0], edge_image.shape[1], 1)) # Zero padding to make square if edge_image.shape[0] > edge_image.shape[1]: x_padding = edge_image.shape[0] - edge_image.shape[1] x_padding_start = round(x_padding / 2.0) x_end = x_padding_start + edge_image.shape[1] new_edge_image = np.zeros( (edge_image.shape[0], edge_image.shape[1] + x_padding, edge_image.shape[2])) new_edge_image[:, x_padding_start:x_end, :] = edge_image[:, :, :] edge_image = new_edge_image elif edge_image.shape[1] > edge_image.shape[0]: y_padding = edge_image.shape[1] - edge_image.shape[0] y_padding_top = round(y_padding / 2.0) y_end = y_padding_top + edge_image.shape[0] new_edge_image = np.zeros( (edge_image.shape[0] + y_padding, edge_image.shape[1], edge_image.shape[2])) new_edge_image[y_padding_top:y_end, :, :] = edge_image[:, :, :] edge_image = new_edge_image # Run exhaustive sliding window result = [] model = self.__get_model() model.load_weights(self.weight_file) window_width = self.image_width window_height = self.image_height mask_width = int(float(self.image_width) * 0.25) mask_steps_x = 4 mask_height = int(float(self.image_height) * 0.25) mask_steps_y = 4 scales = range(1, 6) overlap = 0.5 for scale in scales: image_width = round(scale * window_width) image_height = round(scale * window_height) resized_image = np.array(cv2.resize(edge_image, (image_width, image_height)), dtype=np.float) if self.use_hed: resized_image = np.reshape( resized_image, (resized_image.shape[0], resized_image.shape[1], 1)) resize_x = float(orig_width) / float(image_width) resize_y = float(orig_height) / float(image_height) steps = int(((scale / overlap) - 1)) for x in range(0, steps): x_offset = x * (window_width * overlap) for y in range(0, steps): y_offset = y * (window_height * overlap) xmin = int(x_offset) xmax = int(x_offset + window_width) ymin = int(y_offset) ymax = int(y_offset + window_height) window = resized_image[ymin:ymax, xmin:xmax] if self.use_multichannel: window = np.reshape(window, (1, window.shape[0], window.shape[1], window.shape[2])) else: window = np.reshape( window, (1, window.shape[0], window.shape[1], 1)) window = window / 255.0 mask_x_min = 0 mask_x_max = 0 mask_y_min = 0 mask_y_max = 0 original_prediction = model.predict(window, 1)[0] # Mask out areas and reevaluate for box refinement if use_refinement and original_prediction >= 0.75: for i in range(mask_steps_x): new_window = self.__mask_out_region( window, 0, 0, i * mask_width, self.image_height) new_prediction = model.predict(new_window, 1)[0] if original_prediction < new_prediction: window = new_window mask_x_min = i * mask_width else: break for i in range(mask_steps_x): new_window = self.__mask_out_region( window, self.image_width - i * mask_width, 0, self.image_width, self.image_height) new_prediction = model.predict(new_window, 1)[0] if original_prediction < new_prediction: window = new_window mask_x_max = i * mask_width else: break for i in range(mask_steps_y): new_window = self.__mask_out_region( window, 0, 0, self.image_width, i * mask_height) new_prediction = model.predict(new_window, 1)[0] if original_prediction < new_prediction: window = new_window mask_y_min = i * mask_height else: break for i in range(mask_steps_y): new_window = self.__mask_out_region( window, 0, self.image_height - i * mask_height, self.image_width, self.image_height) new_prediction = model.predict(new_window, 1)[0] if original_prediction < new_prediction: window = new_window mask_y_max = i * mask_height else: break true_xmin = int((x_offset + mask_x_min) * resize_x) true_ymin = int((y_offset + mask_y_min) * resize_y) true_xmax = true_xmin + int( (window_width - mask_x_max) * resize_x) true_ymax = true_ymin + int( (window_height - mask_y_max) * resize_y) window_result = [ true_xmin, true_ymin, true_xmax, true_ymax, original_prediction ] result.append(window_result) return result
# Retrieve various data about the video nb_of_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) fps = cap.get(cv2.CAP_PROP_FPS) image_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) image_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) final_width = args.width if args.width != 0 else nb_of_frames # Prepare the final image final_image = numpy.zeros([800, final_width, 3], dtype=numpy.uint8) # Sample color image sample_image = numpy.zeros([200, 200, 3], dtype=numpy.uint8) # Edge Detection x1, y1, x2, y2 = ED.detect_black_edges(cap) # Set to the correct frame according to the timestamp fpms = fps/1000 cap.set(cv2.CAP_PROP_POS_FRAMES, args.timestamp*fpms) # Determine time warp warp = (nb_of_frames-(args.timestamp*fpms))/final_width print "Taking one frame every " + str(warp) # For each Frame for i in tqdm(range(final_width)): # Let's warp cap.set(cv2.CAP_PROP_POS_FRAMES, (i+args.timestamp)*warp) ret, frame = cap.read()
#%% #Import images imagestack = ito.stackimport(dataDR + r"\1ums.tif") #%% #Select the minimum (1s) and maximum (2s) crop locations x1c = 300 x2c = 900 y1c = 400 y2c = 1000 croppoints = [x1c, x2c, y1c, y2c] fig, ax = plt.subplots(nrows=2, ncols=2) testimage1 = imagestack[0] testimage2 = imagestack[-1] croptest1 = ede.cropper(testimage1, *croppoints) croptest2 = ede.cropper(testimage2, *croppoints) ax[0, 0].imshow(testimage1) ax[0, 1].imshow(testimage2) ax[1, 0].imshow(croptest1) ax[1, 1].imshow(croptest2) #%% #check that edge detection is working properly #Create a zero background or could import one and crop background = np.zeros(croptest1.shape) #[threshval,obsSize,cannysigma]
importlib.reload(ede) import PlateauAnalysis as planl importlib.reload(planl) #Remove to avoid cluttering path sys.path.remove('./Tools') #Remove tools from path #Set working directory to data location os.chdir(dataDR) #%% testimage = imageio.imread('main.tif') edges = ede.edgedetector(testimage,False,-20,500,2) plt.imshow(testimage,cmap='gray') #plt.plot(edges[:,0],edges[:,1],'r.') #%% oneslice=testimage[:,19] from scipy.signal import savgol_filter, butter, filtfilt from scipy.signal import filtfilt def butter_lowpass(cutoff, fs, order=5): nyq = 0.5 * fs normal_cutoff = cutoff / nyq b, a = butter(order, normal_cutoff, btype='low', analog=False)
import EdgeDetection from PIL import Image, ImageDraw import KernelsDictionary filterName = input('mode(sobel, previt):') ImageAddrass = input('image:') image = Image.open(ImageAddrass) kernel = KernelsDictionary.edgeDetection_Dict[filterName] EdgeDetection.process(image, kernel) image.save('edgeDetection.jpg', "JPEG")