def plot_circles_on_raw_image(self): # Plots the circle on the raw image xunit = list() yunit = list() found_circle = True os.makedirs(self.circle_image_folder_name, exist_ok=True) image_count = -1 for image_name, image_path in utils.folder_reader(self.image_folder_name): image_count += 1 image_read = plt.imread(os.path.join(image_path, image_name)) for item in CirclePosition.features_selected_circle: if item[0] == image_count: if item[1]: number_of_points = np.linspace(0, 2 * np.pi, 200) xunit = item[3] * np.cos(number_of_points) + item[1] yunit = item[3] * np.sin(number_of_points) + item[2] else: found_circle = False if not found_circle: plt.imshow(image_read) plt.text(50, 50, 'No Circle Found') plt.savefig(os.path.join(self.circle_image_folder_name, image_name)) else: plt.imshow(image_read) plt.plot(xunit, yunit) plt.savefig(os.path.join(self.circle_image_folder_name, image_name)) plt.close() found_circle = True
def calculate_laplacian(self): for image_name, image_path in utils.folder_reader(self.frames_raw): image_read = io.imread(os.path.join(image_path, image_name)) gray = rgb2gray(image_read) gauss = gaussian(gray) fm = laplace(gauss, ksize=3) # Output self.fm_list.append(fm) self.variance_list.append(variance(fm) * 1000) self.max_list.append(np.amax(fm)) os.makedirs(self.frames_blur, exist_ok=True)
def detect_yellow(self, hsv_low, hsv_high): os.makedirs(self.frames_res, exist_ok=True) for image_name, image_path in utils.folder_reader(self.frames_raw): image_read = cv2.imread(os.path.join(image_path, image_name)) hsv = cv2.cvtColor(image_read, cv2.COLOR_BGR2HSV) yellow_lower = np.array(hsv_low, np.uint8) yellow_upper = np.array(hsv_high, np.uint8) yellow = cv2.inRange(hsv, yellow_lower, yellow_upper) res = cv2.bitwise_and(src1=image_read, src2=image_read, mask=yellow) cv2.imwrite(os.path.join(self.frames_res, image_name), res)
def main(): video_name = 'T20190823155414' raw_image_folder_name = os.path.join(str(root), 'data', video_name, 'raw') variance_index = 0 for image_name, image_directory in utils.folder_reader(raw_image_folder_name): variance_index += 1 image_name_path = os.path.join(image_directory, image_name) variance_image = Variance(image_name_path) variance_image.create_grayscale_from_rgb() variance_image.get_bright_and_dark_pixels() variance_image.calculate_variance() csv = WritingCSV(image_name_path) csv.write_plot_data_2_csv(variance_index)
def get_scatter_plot(self): # Plots scatter plots and writes them to scatter folder os.makedirs(self.scatter_image_folder_name, exist_ok=True) image_number = 0 for image_name, image_path in utils.folder_reader(self.res_image_folder_name): image_read = cv2.imread(os.path.join(image_path, image_name)) img = Image.fromarray(image_read).convert("L") grayscale_image_array = np.asarray(img) index_array_x_yellow, index_array_y_yellow, black_x_position, black_y_position = \ ScatterImages.get_bright_and_dark_pixels(grayscale_image_array, Images.pixel_value) plt.imshow(image_read) plt.scatter(black_x_position, black_y_position, c='black') plt.savefig(os.path.join(self.scatter_image_folder_name, image_name)) plt.close() ScatterImages.black_pixels_position.append([image_number, black_x_position, black_y_position]) print('Image number: ', image_number) image_number += 1
def get_edges(self): # Identifies the edges in the image contour_list = list() scatter_image_folder_name = self.scatter_image_folder_name os.makedirs(scatter_image_folder_name, exist_ok=True) EdgeDetection.total_contour_list = list() for image_name, image_path in utils.folder_reader(scatter_image_folder_name): raw_image = cv2.imread(os.path.join(image_path, image_name)) bilateral_filtered_image = cv2.bilateralFilter(raw_image, 5, 175, 175) edge_detected_image = cv2.Canny(bilateral_filtered_image, 75, 200) contours, _ = cv2.findContours(edge_detected_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) for contour in contours: approx = cv2.approxPolyDP(contour, 0.01 * cv2.arcLength(contour, True), True) area = cv2.contourArea(contour) if len(approx) > 8 and area > 25: contour_list.append(contour) updated_contour_list = list() for item in contour_list: if item.shape[0] > 30: updated_contour_list.append(item) EdgeDetection.total_contour_list.append(updated_contour_list) contour_list = list()
def main(video, **kwargs): start_time = time.time() blur_is_enabled = kwargs.get('blur', False) variance_is_enabled = kwargs.get('variance', False) circles_is_enabled = kwargs.get('circles', False) root = utils.get_project_root() frames_folder = os.path.join(str(root), 'data', 'frames') frames_creator = video_to_frames.FramesCreator(video, frames_folder, fps=1, crop=True) frames_creator.get_frame() video_name_no_extension, video_name_extension = os.path.splitext(video) frames_raw = os.path.join(str(root), 'data', 'frames', video_name_no_extension, 'raw') frames_res = os.path.join(str(root), 'data', 'frames', video_name_no_extension, 'res') if blur_is_enabled or variance_is_enabled: color_detector = color_detection.ColorDetector(frames_raw, frames_res) # HSV values yellow_low = [18, 25, 25] yellow_high = [30, 255, 255] color_detector.detect_yellow(yellow_low, yellow_high) if blur_is_enabled: frames_blur = os.path.join(str(root), 'data', 'frames', video_name_no_extension, 'blur') blur_detector = blur.BlurDetector(frames_raw, frames_blur) blur_detector.calculate_laplacian() blur_detector.blur_results() # Identify pixels if variance_is_enabled: variance_index = 0 for image_name, image_directory in utils.folder_reader(frames_raw): variance_index += 1 image_name_path = os.path.join(image_directory, image_name) variance_image = identify_pixels.Variance(image_name_path) variance_image.create_grayscale_from_rgb() variance_image.get_bright_and_dark_pixels() variance_image.calculate_variance() csv = identify_pixels.WritingCSV(image_name_path) csv.write_plot_data_2_csv(variance_index) # Identify circles if circles_is_enabled: yellow_low = [14, 25, 25] yellow_high = [30, 255, 255] scatter_images = identify_circles.ScatterImages( video_name_no_extension) scatter_images.get_res(yellow_low, yellow_high) scatter_images.get_scatter_plot() edge_detection = identify_circles.EdgeDetection( video_name_no_extension) edge_detection.get_edges() circle_pos = identify_circles.CirclePosition(video_name_no_extension) circle_pos.get_valid_radii() circle_pos.count_pixels_in_circle() circle_pos.plot_circles_on_raw_image() identify_circles.CirclePosition.features_selected_circle = list() print("Main program took", round(time.time() - start_time, 2), "seconds to run")