def add_control_pane(self, fixed_width=220): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.ksize = 3 self.shape_id = 2 self.shapes = {"MORPH_RECT": 0, "MORPH_CROSS": 1, "MORPH_ELLIPSE": 2} self.shape = ZLabeledComboBox(self.vpane, "AdaptiveMethod") self.shape.add_items(list(self.shapes.keys())) self.shape.add_activated_callback(self.shape_activated) self.ksize_slider = ZLabeledSlider(self.vpane, "KernelSize", take_odd=True, minimum=1, maximum=33, value=self.ksize, fixed_width=200) self.ksize_slider.add_value_changed_callback(self.ksize_value_changed) self.vpane.add(self.shape) self.vpane.add(self.ksize_slider) self.set_right_dock(self.vpane)
def add_control_pane(self, fixed_width=200): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.contrast = 1 self.brightness = 11 self.contrast_slider = ZLabeledSlider(self.vpane, "Color", take_odd=False, minimum=1, maximum=3, value=self.contrast) self.contrast_slider.add_value_changed_callback( self.slider1_value_changed) self.vpane.add(self.contrast_slider) self.brightness_slider = ZLabeledSlider(self.vpane, "Contrast", take_odd=False, minimum=0, maximum=100, value=self.brightness) self.brightness_slider.add_value_changed_callback( self.slider2_value_changed) self.vpane.add(self.brightness_slider) self.set_right_dock(self.vpane)
def add_control_pane(self, fixed_width=200): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.ksize = 21 self.sigmaSize = 12 self.ksize_slider = ZLabeledSlider(self.vpane, "KernelSize", take_odd=True, minimum=0, maximum=31, value=self.ksize) self.ksize_slider.add_value_changed_callback( self.slider1_value_changed) self.vpane.add(self.ksize_slider) self.sigmaSize_slider = ZLabeledSlider(self.vpane, "SigmaSize", take_odd=False, minimum=0, maximum=30, value=self.sigmaSize) self.sigmaSize_slider.add_value_changed_callback( self.slider2_value_changed) self.vpane.add(self.sigmaSize_slider) self.set_right_dock(self.vpane)
def add_control_pane(self, fixed_width=280): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.csv_tableview = ZCSVTableView(self) self.vpane.add(self.csv_tableview) self.set_right_dock(self.vpane)
def add_control_pane(self, fixed_width=220): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.block_size = 11 self.adaptive_method_id = 0; self.threshold_type_id = 0; self.methods = {"ADAPTIVE_THRESH_MEAN_C": cv2.ADAPTIVE_THRESH_MEAN_C, "ADAPTIVE_THRESH_GAUSSIAN_C": cv2.ADAPTIVE_THRESH_GAUSSIAN_C} self.types = {"THRESH_BINARY": cv2.THRESH_BINARY , "THRESH_BINARY_INV": cv2.THRESH_BINARY_INV } self.adaptive_method = ZLabeledComboBox(self.vpane, "AdaptiveMethod") self.adaptive_method.add_items(list(self.methods.keys() )) self.adaptive_method.add_activated_callback(self.adaptive_method_activated) self.threshold_type = ZLabeledComboBox(self.vpane, "ThresholdType") self.threshold_type.add_items(list(self.types.keys()) ) self.threshold_type.add_activated_callback(self.threshold_type_activated) self.labeled_slider = ZLabeledSlider(self.vpane, "BlockSize", take_odd =True, minimum=3, maximum=43, value=self.block_size, fixed_width=200) self.labeled_slider.add_value_changed_callback(self.slider_value_changed) self.vpane.add(self.adaptive_method) self.vpane.add(self.threshold_type) self.vpane.add(self.labeled_slider) self.set_right_dock(self.vpane)
def add_control_pane(self, fixed_width=220): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.ksize = 3 self.shape_id = 2; self.type_id = 3 self.shapes = { "MORPH_RECT": 0, "MORPH_CROSS": 1, "MORPH_ELLIPSE":2} self.shape = ZLabeledComboBox(self.vpane, "MorphShape") self.shape.add_items(list(self.shapes.keys() )) self.shape.add_activated_callback(self.shape_activated) self.shape.set_current_text(self.shape_id) self.types = { "MORPH_OPEN": 0, "MORPH_CLOSE": 1, "MORPH_GRADIENT": 2, "MORPH_TOPHAT": 3, "MORPH_BLACKHAT": 4 } self.type = ZLabeledComboBox(self.vpane, "MorphType") self.type.add_items(list(self.types.keys() )) self.type.add_activated_callback(self.type_activated) self.type.set_current_text(self.type_id) self.ksize_slider = ZLabeledSlider(self.vpane, "KernelSize", take_odd =True, minimum=1, maximum=33, value=self.ksize, fixed_width=200) self.ksize_slider.add_value_changed_callback(self.ksize_value_changed) self.vpane.add(self.shape) self.vpane.add(self.type) self.vpane.add(self.ksize_slider) self.set_right_dock(self.vpane)
def add_control_pane(self, fixed_width=220): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.stride = 6 self.hog_descriptor_id = self.DEFAULT self.hog_descriptors = { "Default": self.DEFAULT, "Daimler": self.DAIMLER, "UserDefined": self.USER_DEFINED } self.hog_descriptor = ZLabeledComboBox(self.vpane, "HOG Descriptor") self.hog_descriptor.add_items(list(self.hog_descriptors.keys())) self.hog_descriptor.add_activated_callback( self.hog_descriptor_activated) self.hog_descriptor.set_current_text(self.hog_descriptor_id) self.stride_slider = ZLabeledSlider(self.vpane, "WinStride", take_odd=False, minimum=1, maximum=16, value=self.stride, fixed_width=180) self.stride_slider.add_value_changed_callback( self.stride_value_changed) self.vpane.add(self.hog_descriptor) self.vpane.add(self.stride_slider) self.set_right_dock(self.vpane)
def add_control_pane(self, fixed_width=200): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.threshold1 = 50 self.threshold2 = 100 self.threshold1_slider = ZLabeledSlider(self.vpane, "Threshold1", take_odd=True, minimum=0, maximum=300, value=self.threshold1) self.threshold1_slider.add_value_changed_callback( self.slider1_value_changed) self.vpane.add(self.threshold1_slider) self.threshold2_slider = ZLabeledSlider(self.vpane, "Threshold2", take_odd=True, minimum=0, maximum=300, value=self.threshold2) self.threshold2_slider.add_value_changed_callback( self.slider2_value_changed) self.vpane.add(self.threshold2_slider) self.set_right_dock(self.vpane)
def add_control_pane(self, fixed_width=220): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.hParameter = 21 self.hParameter_slider = ZLabeledSlider(self.vpane, "H Parameter", take_odd=False, minimum=1, maximum=self.H_PARAMETER_MAX, value=self.hParameter) self.hParameter_slider.add_value_changed_callback( self.hParameter_changed) self.hColorParameter = 27 self.hColorParameter_slider = ZLabeledSlider( self.vpane, "H ColorParameter", take_odd=False, minimum=1, maximum=self.HCOLOR_PARAMETER_MAX, value=self.hColorParameter) self.hColorParameter_slider.add_value_changed_callback( self.hColorParameter_changed) self.template_window_size = 15 self.template_window_size_slider = ZLabeledSlider( self.vpane, "TemplateWindowSize", take_odd=False, minimum=1, maximum=self.TEMPLATE_WINDOW_SIZE_MAX, value=self.template_window_size) self.template_window_size_slider.add_value_changed_callback( self.template_window_size_changed) self.search_window_size = 13 self.search_window_size_slider = ZLabeledSlider( self.vpane, "SearchWindowSize", take_odd=False, minimum=1, maximum=self.SEARCH_WINDOW_SIZE_MAX, value=self.search_window_size) self.search_window_size_slider.add_value_changed_callback( self.search_window_size_changed) self.vpane.add(self.hParameter_slider) self.vpane.add(self.hColorParameter_slider) self.vpane.add(self.template_window_size_slider) self.vpane.add(self.search_window_size_slider) self.set_right_dock(self.vpane)
def add_control_pane(self, fixed_width=200): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) # 1 Stitcher detector selection combobox self.detectors = { "AKAZEFeatureDetector": self.DETECTOR_AKAZE, "BRISKFeatureDetector": self.DETECTOR_BRISK, "ORBFeatureDetector": self.DETECTOR_ORB } self.detector_id = self.DETECTOR_AKAZE self.detector_combobox = ZLabeledComboBox(self.vpane, "FeatureDetector") self.detector_combobox.add_activated_callback(self.detector_changed) self.detector_combobox.add_items(self.detectors.keys()) self.detector_combobox.set_current_text(self.detector_id) self.best_top = { "10": 10, "20": 20, "30": 30, "40": 40, "50": 50, "60": 60, "70": 70, "80": 80, "90": 90, "100": 100 } self.best_top_value = 10 self.best_top_combobox = ZLabeledComboBox(self.vpane, "BestTopNumber") self.best_top_combobox.add_activated_callback(self.best_top_changed) self.best_top_combobox.add_items(self.best_top.keys()) self.best_top_combobox.set_current_text(0) # Match pushbutton self.match_button = QPushButton("Match", self.vpane) self.match_button.clicked.connect(self.match_button_clicked) self.spacer = QLabel("", self.vpane) self.matched_number = QLabel("", self.vpane) self.vpane.add(self.detector_combobox) self.vpane.add(self.best_top_combobox) self.vpane.add(self.match_button) self.vpane.add(self.spacer) self.vpane.add(self.matched_number) self.set_right_dock(self.vpane)
def add_control_pane(self, fixed_width=200): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.ksize = 3 self.ksize_slider = ZLabeledSlider(self.vpane, "GaussianBlur KernelSize", take_odd=True, minimum=0, maximum=31, value=self.ksize) self.ksize_slider.add_value_changed_callback(self.ksize_value_changed) self.vpane.add(self.ksize_slider) self.sigma = 2 self.sigma_slider = ZLabeledSlider(self.vpane, "GaussianBlur SigmaSize", take_odd=True, minimum=1, maximum=21, value=self.sigma) self.sigma_slider.add_value_changed_callback(self.sigma_value_changed) self.vpane.add(self.sigma_slider) self.threshold1 = 120 self.threshold2 = 50 self.threshold1_slider = ZLabeledSlider(self.vpane, "CannyEdgeThreshold1", take_odd=True, minimum=0, maximum=300, value=self.threshold1) self.threshold1_slider.add_value_changed_callback( self.slider1_value_changed) self.vpane.add(self.threshold1_slider) self.threshold2_slider = ZLabeledSlider(self.vpane, "ParamThreshold", take_odd=True, minimum=0, maximum=300, value=self.threshold2) self.threshold2_slider.add_value_changed_callback( self.slider2_value_changed) self.vpane.add(self.threshold2_slider) self.set_right_dock(self.vpane)
def add_control_pane(self, fixed_width=220): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.sigma = 30 self.sigma_slider = ZLabeledSlider(self.vpane, "Sigma", take_odd=False, minimum=0, maximum=100, value=self.sigma) self.sigma_slider.add_value_changed_callback(self.sigma_changed) self.vpane.add(self.sigma_slider) self.set_right_dock(self.vpane)
def add_control_pane(self, fixed_width=200): # Control pane widget self.ksize = 5 self.vpane = ZVerticalPane(self, fixed_width) self.labeled_slider = ZLabeledSlider(self.vpane, "KernelSize", take_odd=True, minimum=0, maximum=33, value=self.ksize) self.labeled_slider.add_value_changed_callback( self.slider_value_changed) self.vpane.add(self.labeled_slider) self.set_right_dock(self.vpane)
def add_control_pane(self, fixed_width=200): # Create a vertical control pane. self.vpane = ZVerticalPane(self, fixed_width) self.ksize = 11 self.labeled_slider = ZLabeledSlider(self.vpane, "KernelSize", take_odd=True, minimum=0, maximum=33, value=self.ksize) self.labeled_slider.add_value_changed_callback( self.slider_value_changed) self.vpane.add(self.labeled_slider) # Set the control_pane to the right docking area of this main window. self.set_right_dock(self.vpane)
def add_control_pane(self, fixed_width=220): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.blockSize = 7 self.kernelSize = 5 self.detectorThreshold = 100 self.blockSizeance_slider = ZLabeledSlider(self.vpane, "BlockSize", take_odd=False, minimum=2, maximum=100, value=self.blockSize, fixed_width=200) self.blockSizeance_slider.add_value_changed_callback( self.blockSizeance_value_changed) self.kernelSize_slider = ZLabeledSlider(self.vpane, "KernelSize", take_odd=True, minimum=3, maximum=31, value=self.kernelSize, fixed_width=200) self.kernelSize_slider.add_value_changed_callback( self.kernelSize_value_changed) self.detectorThreshold_slider = ZLabeledSlider( self.vpane, "DetectorThreshold", take_odd=False, minimum=100, maximum=240, value=self.detectorThreshold, fixed_width=200) self.detectorThreshold_slider.add_value_changed_callback( self.detectorThreshold_value_changed) self.vpane.add(self.blockSizeance_slider) self.vpane.add(self.kernelSize_slider) self.vpane.add(self.detectorThreshold_slider) self.set_right_dock(self.vpane)
def add_control_pane(self, fixed_width=200): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.reload_button = QPushButton("Reload", self.vpane) self.reload_button.clicked.connect(self.reload_button_clicked) self.vpane.add(self.reload_button) self.detect_button = QPushButton( "Detect", self.vpane, ) self.detect_button.clicked.connect(self.detect_button_clicked) self.vpane.add(self.detect_button) self.set_right_dock(self.vpane)
def add_control_pane(self, fixed_width=220): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.diameter = 4 self.sigma_color = 160 self.sigma_space = 40 self.diameter_slider = ZLabeledSlider(self.vpane, "Diameter", take_odd=False, minimum=0, maximum=5, value=self.diameter, fixed_width=200) self.diameter_slider.add_value_changed_callback( self.diameter_value_changed) self.sigma_color_slider = ZLabeledSlider(self.vpane, "SigmaColor", take_odd=False, minimum=0, maximum=255, value=self.sigma_color, fixed_width=200) self.sigma_color_slider.add_value_changed_callback( self.sigma_color_value_changed) self.sigma_space_slider = ZLabeledSlider(self.vpane, "SigmaSpace", take_odd=False, minimum=0, maximum=255, value=self.sigma_space, fixed_width=200) self.sigma_space_slider.add_value_changed_callback( self.sigma_space_value_changed) self.vpane.add(self.diameter_slider) self.vpane.add(self.sigma_color_slider) self.vpane.add(self.sigma_space_slider) self.set_right_dock(self.vpane)
def add_control_pane(self, fixed_width=220): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.minDist = 9 self.minArea = 15 self.maxArea = 131 self.minDistance_slider = ZLabeledSlider(self.vpane, "MinDistanceBetweenBlob", take_odd=False, minimum=5, maximum=100, value=self.minDist, fixed_width=200) self.minDistance_slider.add_value_changed_callback( self.minDistance_value_changed) self.minArea_slider = ZLabeledSlider(self.vpane, "MinArea", take_odd=False, minimum=1, maximum=100, value=self.minArea, fixed_width=200) self.minArea_slider.add_value_changed_callback( self.minArea_value_changed) self.maxArea_slider = ZLabeledSlider(self.vpane, "MaxArea", take_odd=False, minimum=100, maximum=200, value=self.maxArea, fixed_width=200) self.maxArea_slider.add_value_changed_callback( self.maxArea_value_changed) self.vpane.add(self.minDistance_slider) self.vpane.add(self.minArea_slider) self.vpane.add(self.maxArea_slider) self.set_right_dock(self.vpane)
def add_control_pane(self, fixed_width=130): # Control pane widget self.ksize = 11 self.vpane = ZVerticalPane(self, fixed_width) # 1 Stitcher mode selection combobox self.modes = {"PANORAM": 0, "SCANS": 1} self.labeled_combobox = ZLabeledComboBox(self.vpane, "Sticher Mode") self.labeled_combobox.add_activated_callback(self.mode_changed) self.labeled_combobox.add_items(self.modes.keys()) # Stitch pushbutton self.stitch_button = QPushButton("Stitch", self.vpane) self.stitch_button.clicked.connect(self.stitch) self.vpane.add(self.labeled_combobox) self.vpane.add(self.stitch_button) self.set_right_dock(self.vpane)
class MainView(ZApplicationView): # Inner classes #-------------------------------------------- class SourceImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) def load(self, filename): self.load_opencv_image(filename) self.update() class DetectedImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) def load(self, filename): source_image = self.load_opencv_image(filename) self.gray_image = cv2.cvtColor(source_image, cv2.COLOR_RGB2GRAY) #The following detect_cornert method is based on the code: #http:#www.swarthmore.edu/NatSci/mzucker1/opencv-2.4.10-docs/ # doc/tutorials/features2d/trackingmotion/harris_detector/harris_detector.html def detect(self, bsize, ksize, threshold): source_image = self.get_opencv_image() #blockSize: Neighborhood size. #ksize : Aperture parameter for the Sobel operator, # which should be odd. ksize = int(ksize) #k: Harris detector free parameter. k = float(0.04) w, h, c = source_image.shape dest_image = np.zeros([w, h], dtype=np.uint8) gray_image = cv2.cvtColor(source_image, cv2.COLOR_RGB2GRAY) dest_image = cv2.cornerHarris(gray_image, bsize, ksize, k, cv2.BORDER_DEFAULT) cv2.normalize(dest_image, dest_image, 0, 255, cv2.NORM_MINMAX) #We draw circles at the corners detected by cornerHarris on the originalImage # by using the destNormalized image data and the threshold parameter # which is used to draw the circles.. detected_image = source_image.copy() iw, ih = dest_image.shape for j in range(iw): for i in range(ih): if dest_image[j, i] > threshold: cv2.circle(detected_image, (i, j), 5, (0, 0, 255), 1, 8, 0) self.set_opencv_image(detected_image) self.update() #-------------------------------------------- # MainView Constructor def __init__(self, title, x, y, width, height): super(MainView, self).__init__(title, x, y, width, height) filename = "../images/Geometry.png" # 1 Create first imageview. self.source_image_view = self.SourceImageView(self) # 2 Create second imageview. self.detectd_image_view = self.DetectedImageView(self) # 3 Load the file self.load_file(filename) # 4 Add two image views to a main_layout of this main view. self.add(self.source_image_view) self.add(self.detectd_image_view) self.show() def add_control_pane(self, fixed_width=220): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.blockSize = 7 self.kernelSize = 5 self.detectorThreshold = 100 self.blockSizeance_slider = ZLabeledSlider(self.vpane, "BlockSize", take_odd=False, minimum=2, maximum=100, value=self.blockSize, fixed_width=200) self.blockSizeance_slider.add_value_changed_callback( self.blockSizeance_value_changed) self.kernelSize_slider = ZLabeledSlider(self.vpane, "KernelSize", take_odd=True, minimum=3, maximum=31, value=self.kernelSize, fixed_width=200) self.kernelSize_slider.add_value_changed_callback( self.kernelSize_value_changed) self.detectorThreshold_slider = ZLabeledSlider( self.vpane, "DetectorThreshold", take_odd=False, minimum=100, maximum=240, value=self.detectorThreshold, fixed_width=200) self.detectorThreshold_slider.add_value_changed_callback( self.detectorThreshold_value_changed) self.vpane.add(self.blockSizeance_slider) self.vpane.add(self.kernelSize_slider) self.vpane.add(self.detectorThreshold_slider) self.set_right_dock(self.vpane) def file_open(self): options = QFileDialog.Options() filename, _ = QFileDialog.getOpenFileName( self, "FileOpenDialog", "", "All Files (*);;Image Files (*.png;*jpg;*.jpeg)", options=options) if filename: self.load_file(filename) def load_file(self, filename): self.source_image_view.load(filename) self.detectd_image_view.load(filename) self.detectd_image_view.detect(self.blockSize, self.kernelSize, self.detectorThreshold) self.set_filenamed_title(filename) def blockSizeance_value_changed(self, value): self.blockSize = int(value) self.detectd_image_view.detect(self.blockSize, self.kernelSize, self.detectorThreshold) def kernelSize_value_changed(self, value): self.kernelSize = int(value) self.detectd_image_view.detect(self.blockSize, self.kernelSize, self.detectorThreshold) def detectorThreshold_value_changed(self, value): self.detectorThreshold = int(value) self.detectd_image_view.detect(self.blockSize, self.kernelSize, self.detectorThreshold)
class MainView(ZApplicationView): # Inner classes #-------------------------------------------- class SourceImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) def load(self, filename): self.load_openv_image(filename) self.update() class BlurredImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) def load(self, filename): self.load_openv_image(filename) def blur(self, ksize): ksize = int(ksize) original_image = self.get_opencv_image() blurred_image = cv2.medianBlur(original_image, ksize) self.set_opencv_image(blurred_image) self.update() #-------------------------------------------- # MainView Constructor def __init__(self, title, x, y, width, height): super(MainView, self).__init__(title, x, y, width, height) filename = "../images/flower.png" # 1 Create first imageview. self.source_image_view = self.SourceImageView(self) # 2 Create second imageview. self.blurred_image_view = self.BlurredImageView(self) # 3 Load the file self.load_file(filename) # 4 Add two image views to a main_layout of this main view. self.add(self.source_image_view) self.add(self.blurred_image_view) self.show() # Add control pane to MainView def add_control_pane(self, fixed_width=200): # Create a vertical control pane. self.vpane = ZVerticalPane(self, fixed_width) self.ksize = 11 self.labeled_slider = ZLabeledSlider(self.vpane, "KernelSize", take_odd=True, minimum=0, maximum=33, value=self.ksize) self.labeled_slider.add_value_changed_callback( self.slider_value_changed) self.vpane.add(self.labeled_slider) # Set the control_pane to the right docking area of this main window. self.set_right_dock(self.vpane) # Show FileOpenDialog and select an image file. def file_open(self): options = QFileDialog.Options() filename, _ = QFileDialog.getOpenFileName( self, "FileOpenDialog", "", "All Files (*);;Image Files (*.png;*jpg;*.jpeg)", options=options) if filename: self.load_file(filename) def load_file(self, filename): self.source_image_view.load_opencv_image(filename, cv2.IMREAD_COLOR) self.blurred_image_view.load_opencv_image(filename, cv2.IMREAD_COLOR) self.blurred_image_view.blur(int(self.ksize)) self.set_filenamed_title(filename) # Slider value changed callback. def slider_value_changed(self, value): self.ksize = int(value) if self.ksize % 2 == 0: self.ksize = (self.ksize * 2) / 2 + 1 # Kernel size should be odd. #print("slider_value_changed:{}".format(ksize)) self.blurred_image_view.blur(int(int(self.ksize)))
class MainView(ZApplicationView): # Inner classes #-------------------------------------------- class SourceImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) def load(self, filename): self.load_opencv_image(filename) self.update() class DetectedImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) def load(self, filename): source_image = self.load_opencv_image(filename) self.gray_image = cv2.cvtColor(source_image, cv2.COLOR_RGB2GRAY) def detect(self, adaptive_method_id, threshold_type_id, block_size, threshold1, threshold2): MAX_PIXEL_VALUE = 255 C = 9.0 adapted_image = cv2.adaptiveThreshold(self.gray_image, MAX_PIXEL_VALUE, adaptive_method_id, threshold_type_id, block_size, C) detected_image = cv2.Canny(adapted_image, threshold1, threshold2) self.set_opencv_image(detected_image) self.update() #-------------------------------------------- # MainView Constructor def __init__(self, title, x, y, width, height): super(MainView, self).__init__(title, x, y, width, height) filename = "../images/SuperCar2.png" # 1 Create first imageview. self.source_image_view = self.SourceImageView(self) # 2 Create second imageview. self.detected_image_view = self.DetectedImageView(self) # 3 Load the file self.load_file(filename) # 4 Add imageviews to the main_layout which is a horizontal layouter. self.add(self.source_image_view) self.add(self.detected_image_view) self.detected_image_view.detect(self.adaptive_method_id, self.threshold_type_id, self.block_size, self.threshold1, self.threshold2) self.show() def add_control_pane(self, fixed_width=220): # Control pane widget self.block_size = 11 self.vpane = ZVerticalPane(self, fixed_width) self.adaptive_method_id = 0 self.threshold_type_id = 0 self.methods = { "ADAPTIVE_THRESH_MEAN_C": cv2.ADAPTIVE_THRESH_MEAN_C, "ADAPTIVE_THRESH_GAUSSIAN_C": cv2.ADAPTIVE_THRESH_GAUSSIAN_C } self.types = { "THRESH_BINARY": cv2.THRESH_BINARY, "THRESH_BINARY_INV": cv2.THRESH_BINARY_INV } self.adaptive_method = ZLabeledComboBox(self.vpane, "AdaptiveMethod") self.adaptive_method.add_items(list(self.methods.keys())) self.adaptive_method.add_activated_callback( self.adaptive_method_activated) self.threshold_type = ZLabeledComboBox(self.vpane, "ThresholdType") self.threshold_type.add_items(list(self.types.keys())) self.threshold_type.add_activated_callback( self.threshold_type_activated) self.block_size_slider = ZLabeledSlider(self.vpane, "BlockSize", take_odd=True, minimum=3, maximum=43, value=self.block_size, fixed_width=200) self.block_size_slider.add_value_changed_callback( self.block_size_changed) self.vpane.add(self.adaptive_method) self.vpane.add(self.threshold_type) self.vpane.add(self.block_size_slider) self.threshold1 = 50 self.threshold2 = 100 self.threshold1_slider = ZLabeledSlider(self.vpane, "Threshold1", take_odd=True, minimum=0, maximum=300, value=self.threshold1) self.threshold1_slider.add_value_changed_callback( self.slider1_value_changed) self.threshold2_slider = ZLabeledSlider(self.vpane, "Threshold2", take_odd=True, minimum=0, maximum=300, value=self.threshold2) self.threshold2_slider.add_value_changed_callback( self.slider2_value_changed) self.vpane.add(self.threshold1_slider) self.vpane.add(self.threshold2_slider) self.set_right_dock(self.vpane) def file_open(self): options = QFileDialog.Options() filename, _ = QFileDialog.getOpenFileName( self, "FileOpenDialog", "", "All Files (*);;Image Files (*.png;*jpg;*.jpeg)", options=options) if filename: self.load_file(filename) def load_file(self, filename): self.source_image_view.load(filename) self.detected_image_view.load(filename) self.detected_image_view.detect(self.adaptive_method_id, self.threshold_type_id, self.block_size, self.threshold1, self.threshold2) self.set_filenamed_title(filename) def block_size_changed(self, value): self.block_size = int(value) if self.block_size % 2 == 0: # Block size should be odd. self.block_size = int((self.block_size * 2) / 2 + 1) #print("block_size_changed:{}".format(block_size)) self.detected_image_view.detect(self.adaptive_method_id, self.threshold_type_id, self.block_size, self.threshold1, self.threshold2) def adaptive_method_activated(self, text): self.adaptive_method_id = self.methods[text] #print("adaptive_method_activated:{}{}".format(text, self.adaptive_method_id)) self.detected_image_view.detect(self.adaptive_method_id, self.threshold_type_id, self.block_size, self.threshold1, self.threshold2) def threshold_type_activated(self, text): self.threshold_type_id = self.types[text] #print("threshold_type_activated:{}{}".format(text, self.threshold_type_id)) self.detected_image_view.detect(self.adaptive_method_id, self.threshold_type_id, self.block_size, self.threshold1, self.threshold2) def slider1_value_changed(self, value): self.threshold1 = int(value) #print("slider1_value_changed:{}".format(value)) self.detected_image_view.detect(self.adaptive_method_id, self.threshold_type_id, self.block_size, self.threshold1, self.threshold2) def slider2_value_changed(self, value): self.threshold2 = int(value) #print("slider2_value_changed:{}".format(value)) self.detected_image_view.detect(self.adaptive_method_id, self.threshold_type_id, self.block_size, self.threshold1, self.threshold2)
class MainView(ZApplicationView): # Inner classes #-------------------------------------------- class SourceImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) def load(self, filename): self.load_opencv_image(filename) self.update() class BinarizedImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) def load(self, filename): source_image = self.load_opencv_image(filename) image = cv2.GaussianBlur(source_image, (3,3), 0, 0, borderType = cv2.BORDER_DEFAULT ); self.gray_image = cv2.cvtColor(source_image, cv2.COLOR_RGB2GRAY) def binarize(self, adaptive_method_id, threshold_type_id, block_size): MAX_PIXEL_VALUE = 255 C = 9.0 binarizered_image = cv2.adaptiveThreshold(self.gray_image, MAX_PIXEL_VALUE, adaptive_method_id, threshold_type_id, block_size, C); self.set_opencv_image(binarizered_image) self.update() #-------------------------------------------- # MainView Constructor def __init__(self, title, x, y, width, height): super(MainView, self).__init__(title, x, y, width, height) filename = "images/flower.png" # 1 Create first imageview. self.source_image_view = self.SourceImageView(self) # 2 Create second imageview. self.binarized_image_view = self.BinarizedImageView(self) # 3 Load the file self.load_file(filename) # 4 Add two image views to a main_layout of this main view. self.add(self.source_image_view) self.add(self.binarized_image_view) self.show() def add_control_pane(self, fixed_width=220): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.block_size = 11 self.adaptive_method_id = 0; self.threshold_type_id = 0; self.methods = {"ADAPTIVE_THRESH_MEAN_C": cv2.ADAPTIVE_THRESH_MEAN_C, "ADAPTIVE_THRESH_GAUSSIAN_C": cv2.ADAPTIVE_THRESH_GAUSSIAN_C} self.types = {"THRESH_BINARY": cv2.THRESH_BINARY , "THRESH_BINARY_INV": cv2.THRESH_BINARY_INV } self.adaptive_method = ZLabeledComboBox(self.vpane, "AdaptiveMethod") self.adaptive_method.add_items(list(self.methods.keys() )) self.adaptive_method.add_activated_callback(self.adaptive_method_activated) self.threshold_type = ZLabeledComboBox(self.vpane, "ThresholdType") self.threshold_type.add_items(list(self.types.keys()) ) self.threshold_type.add_activated_callback(self.threshold_type_activated) self.labeled_slider = ZLabeledSlider(self.vpane, "BlockSize", take_odd =True, minimum=3, maximum=43, value=self.block_size, fixed_width=200) self.labeled_slider.add_value_changed_callback(self.slider_value_changed) self.vpane.add(self.adaptive_method) self.vpane.add(self.threshold_type) self.vpane.add(self.labeled_slider) self.set_right_dock(self.vpane) def file_open(self): options = QFileDialog.Options() filename, _ = QFileDialog.getOpenFileName(self,"FileOpenDialog", "", "All Files (*);;Image Files (*.png;*jpg;*.jpeg)", options=options) if filename: self.load_file(filename) def load_file(self, filename): self.source_image_view.load(filename) self.binarized_image_view.load(filename) self.binarized_image_view.binarize(self.adaptive_method_id, self.threshold_type_id, self.block_size) self.set_filenamed_title(filename) def slider_value_changed(self, value): self.block_size = int(value) if self.block_size % 2 == 0: # Block size should be odd. self.block_size = int((self.block_size * 2)/2 + 1) #print("slider_value_changed:{}".format(block_size)) self.binarized_image_view.binarize(self.adaptive_method_id, self.threshold_type_id, self.block_size) def adaptive_method_activated(self, text): #print("adaptive_method_activated:{}".format(text)) self.adaptive_method_id = self.methods[text] self.binarized_image_view.binarize(self.adaptive_method_id, self.threshold_type_id, self.block_size) def threshold_type_activated(self, text): #print("threshold_type_activated:{}".format(text)) self.threshold_type_id = self.types[text] self.binarized_image_view.binarize(self.adaptive_method_id, self.threshold_type_id, self.block_size)
class MainView(ZApplicationView): # Inner classes #-------------------------------------------- class SourceImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) def load(self, filename): self.load_opencv_image(filename) self.update() class BlurredImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) def load(self, filename): source_image = self.load_opencv_image(filename) self.gray_image = cv2.cvtColor(source_image, cv2.COLOR_RGB2GRAY) def blur(self, diameter, sigma_color, sigma_space): source_image = self.get_opencv_image() print("blur {} {} {}".format(diameter, sigma_color, sigma_space)) blurred_image = cv2.bilateralFilter(source_image, diameter, float(sigma_color), float(sigma_space)) self.set_opencv_image(blurred_image) self.update() #-------------------------------------------- # MainView Constructor def __init__(self, title, x, y, width, height): super(MainView, self).__init__(title, x, y, width, height) filename = "../images/MeshedNioh.png" # 1 Create first imageview. self.source_image_view = self.SourceImageView(self) # 2 Create second imageview. self.blurred_image_view = self.BlurredImageView(self) # 3 Load the file self.load_file(filename) # 4 Add two image views to a main_layout of this main view. self.add(self.source_image_view) self.add(self.blurred_image_view) self.show() def add_control_pane(self, fixed_width=220): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.diameter = 4 self.sigma_color = 160 self.sigma_space = 40 self.diameter_slider = ZLabeledSlider(self.vpane, "Diameter", take_odd=False, minimum=0, maximum=5, value=self.diameter, fixed_width=200) self.diameter_slider.add_value_changed_callback( self.diameter_value_changed) self.sigma_color_slider = ZLabeledSlider(self.vpane, "SigmaColor", take_odd=False, minimum=0, maximum=255, value=self.sigma_color, fixed_width=200) self.sigma_color_slider.add_value_changed_callback( self.sigma_color_value_changed) self.sigma_space_slider = ZLabeledSlider(self.vpane, "SigmaSpace", take_odd=False, minimum=0, maximum=255, value=self.sigma_space, fixed_width=200) self.sigma_space_slider.add_value_changed_callback( self.sigma_space_value_changed) self.vpane.add(self.diameter_slider) self.vpane.add(self.sigma_color_slider) self.vpane.add(self.sigma_space_slider) self.set_right_dock(self.vpane) def file_open(self): options = QFileDialog.Options() filename, _ = QFileDialog.getOpenFileName( self, "FileOpenDialog", "", "All Files (*);;Image Files (*.png;*jpg;*.jpeg)", options=options) if filename: self.load_file(filename) def load_file(self, filename): self.source_image_view.load(filename) self.blurred_image_view.load(filename) self.blurred_image_view.blur(self.diameter, self.sigma_color, self.sigma_space) self.set_filenamed_title(filename) def diameter_value_changed(self, value): self.diameter = int(value) self.blurred_image_view.blur(self.diameter, self.sigma_color, self.sigma_space) def sigma_color_value_changed(self, value): self.sigma_color = int(value) self.blurred_image_view.blur(self.diameter, self.sigma_color, self.sigma_space) def sigma_space_value_changed(self, value): self.sigma_space = int(value) self.blurred_image_view.blur(self.diameter, self.sigma_color, self.sigma_space)
class MainView(ZApplicationView): # Inner classes #-------------------------------------------- class SourceImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) def load(self, filename): self.load_opencv_image(filename) self.update() class DetectedImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) def load(self, filename): self.load_opencv_image(filename) self.update() def detect(self, hParameter, template_window_size, search_window_size): src_image = self.get_opencv_image() denoized_image = cv2.fastNlMeansDenoising(src_image, hParameter, template_window_size, search_window_size) self.set_opencv_image(denoized_image) self.update() #-------------------------------------------- # Class variables H_PARAMETER_MAX = 31 TEMPLATE_WINDOW_SIZE_MAX = 31 SEARCH_WINDOW_SIZE_MAX = 31 # MainView Constructor def __init__(self, title, x, y, width, height): super(MainView, self).__init__(title, x, y, width, height) filename = "../images/MeshedNioh.png" # 1 Create first imageview. self.source_image_view = self.SourceImageView(self) # 2 Create second imageview. self.detected_image_view = self.DetectedImageView(self) # 3 Load the file self.load_file(filename) # 4 Add imageviews to the main_layout which is a horizontal layouter. self.add(self.source_image_view) self.add(self.detected_image_view) #self.detected_image_view.detect(self.hParameter, # self.template_window_size, self.search_window_size) self.show() def add_control_pane(self, fixed_width=220): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.hParameter = 21 self.hParameter_slider = ZLabeledSlider(self.vpane, "H Parameter", take_odd=False, minimum=1, maximum=self.H_PARAMETER_MAX, value=self.hParameter) self.hParameter_slider.add_value_changed_callback( self.hParameter_changed) self.template_window_size = 15 self.template_window_size_slider = ZLabeledSlider( self.vpane, "TemplateWindowSize", take_odd=False, minimum=1, maximum=self.TEMPLATE_WINDOW_SIZE_MAX, value=self.template_window_size) self.template_window_size_slider.add_value_changed_callback( self.template_window_size_changed) self.search_window_size = 13 self.search_window_size_slider = ZLabeledSlider( self.vpane, "SearchWindowSize", take_odd=False, minimum=1, maximum=self.SEARCH_WINDOW_SIZE_MAX, value=self.search_window_size) self.search_window_size_slider.add_value_changed_callback( self.search_window_size_changed) self.vpane.add(self.hParameter_slider) self.vpane.add(self.template_window_size_slider) self.vpane.add(self.search_window_size_slider) self.set_right_dock(self.vpane) def file_open(self): options = QFileDialog.Options() filename, _ = QFileDialog.getOpenFileName( self, "FileOpenDialog", "", "All Files (*);;Image Files (*.png;*jpg;*.jpeg)", options=options) if filename: self.load_file(filename) def load_file(self, filename): self.source_image_view.load(filename) self.detected_image_view.load(filename) self.detected_image_view.detect(self.hParameter, self.template_window_size, self.search_window_size) self.set_filenamed_title(filename) def hParameter_changed(self, value): self.hParameter = int(value) print("slider1_value_changed:{}".format(value)) self.detected_image_view.detect(self.hParameter, self.template_window_size, self.search_window_size) def template_window_size_changed(self, value): self.template_window = int(value) print("slider1_value_changed:{}".format(value)) self.detected_image_view.detect(self.hParameter, self.template_window_size, self.search_window_size) def search_window_size_changed(self, value): self.search_window_size = int(value) print("slider1_value_changed:{}".format(value)) self.detected_image_view.detect(self.hParameter, self.template_window_size, self.search_window_size)
class MainView(ZApplicationView): # Inner classes #-------------------------------------------- class SourceImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) def load(self, filename): self.load_opencv_image(filename) self.update() class DetectedImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) self.gray_image = None def load(self, filename): self.load_opencv_image(filename) source_image = self.get_opencv_image() self.gray_image = cv2.cvtColor(source_image, cv2.COLOR_RGB2GRAY) def detect(self, threshold1, threshold2): if self.gray_image.all() != None: detected_image = cv2.Canny(self.gray_image, threshold1, threshold2) self.set_opencv_image(detected_image) self.update() #-------------------------------------------- # MainView Constructor def __init__(self, title, x, y, width, height): super(MainView, self).__init__(title, x, y, width, height) filename = "../images/Cloud.png" # 1 Create first imageview. self.source_image_view = self.SourceImageView(self) # 2 Create second imageview. self.detected_image_view = self.DetectedImageView(self) # 3 Load the file self.load_file(filename) # 4 Add two image views to a main_layout of this main view. self.add(self.source_image_view) self.add(self.detected_image_view) self.show() def add_control_pane(self, fixed_width=200): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.threshold1 = 50 self.threshold2 = 100 self.threshold1_slider = ZLabeledSlider(self.vpane, "Threshold1", take_odd=True, minimum=0, maximum=300, value=self.threshold1) self.threshold1_slider.add_value_changed_callback( self.slider1_value_changed) self.vpane.add(self.threshold1_slider) self.threshold2_slider = ZLabeledSlider(self.vpane, "Threshold2", take_odd=True, minimum=0, maximum=300, value=self.threshold2) self.threshold2_slider.add_value_changed_callback( self.slider2_value_changed) self.vpane.add(self.threshold2_slider) self.set_right_dock(self.vpane) def file_open(self): options = QFileDialog.Options() filename, _ = QFileDialog.getOpenFileName( self, "FileOpenDialog", "", "All Files (*);;Image Files (*.png;*jpg;*.jpeg)", options=options) if filename: self.load_file(filename) def load_file(self, filename): self.source_image_view.load(filename) self.detected_image_view.load(filename) # Apply detect method to the detected image view. self.detected_image_view.detect(self.threshold1, self.threshold2) self.set_filenamed_title(filename) def slider1_value_changed(self, value): self.threshold1 = int(value) self.detected_image_view.detect(int(self.threshold1), int(self.threshold2)) def slider2_value_changed(self, value): self.threshold2 = int(value) self.detected_image_view.detect(int(self.threshold1), int(self.threshold2))
class MainView(ZApplicationView): # Inner classes #-------------------------------------------- class SourceImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) def load(self, filename): self.load_opencv_image(filename) self.update() def detect(self, xml_classifier): image = self.get_opencv_image() try: cascade_classifier = cv2.CascadeClassifier() cascade_classifier.load(xml_classifier) faces = cascade_classifier.detectMultiScale( image, 1.3, 4) #scaleFactor=1.1, minNeighbors=1, minSize=(20,20)) print if len(faces) == 0: QMessageBox.information( self, "ObjectDector", "Not found objects by CascadeClassifier. detectMultiScale" ) else: for face in faces: x, y, w, h = face print("{} {} {} {}".format(x, y, w, h)) cv2.rectangle(image, (x, y), (x + w, y + h), (0, 200, 0), 3) ##, CV_AA); self.set_opencv_image(image) self.update() except: traceback.print_exc() #-------------------------------------------- # MainView Constructor def __init__(self, title, x, y, width, height): super(MainView, self).__init__(title, x, y, width, height) self.filename = "../images/WafukuGirl.png" # 1 Create first imageview. self.source_image_view = self.SourceImageView(self) # 3 Load the file self.load_file(self.filename) # 4 Add two image views to a main_layout of this main view. self.add(self.source_image_view) # 5 Add a labeled file combobox to top dock area self.add_classifier_combobox() self.show() # Add control pane to MainView def add_control_pane(self, fixed_width=200): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.reload_button = QPushButton("Reload", self.vpane) self.reload_button.clicked.connect(self.reload_button_clicked) self.vpane.add(self.reload_button) self.detect_button = QPushButton( "Detect", self.vpane, ) self.detect_button.clicked.connect(self.detect_button_clicked) self.vpane.add(self.detect_button) self.set_right_dock(self.vpane) def add_classifier_combobox(self): self.classifier_combobox = ZLabeledFileComboBox(self, "Classfier") self.classifier_combobox.listup_files( "C:/opencv/build/etc/haarcascades/*.xml") self.set_top_dock(self.classifier_combobox) # Show FileOpenDialog and select an image file. def file_open(self): options = QFileDialog.Options() filename, _ = QFileDialog.getOpenFileName( self, "FileOpenDialog", "", "All Files (*);;Image Files (*.png;*jpg;*.jpeg)", options=options) if filename: self.load_file(filename) def load_file(self, filename): self.filename = filename self.source_image_view.load(filename) self.set_filenamed_title(filename) def detect_button_clicked(self): xml_classifier = self.classifier_combobox.get_current_text_as_fullpath( ) print("detect_button_clicked {}".format(xml_classifier)) self.source_image_view.detect(xml_classifier) def reload_button_clicked(self): self.load_file(self.filename)
class MainView(ZApplicationView): # Inner classes #-------------------------------------------- class SourceImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) def load(self, filename): self.load_opencv_image(filename) self.update() class NoisedImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) self.noised_image = None def load(self, filename): self.load_opencv_image(filename) self.update() def inject_noise(self, sault, pepper): src_image = self.get_opencv_image() injector = ZSaultPepperNoiseInjector(sault/100.0) self.noised_image = injector.inject_to(src_image) self.set_opencv_image(self.noised_image) self.update() def get_noised_image(self): return self.noised_image #-------------------------------------------- # MainView Constructor def __init__(self, title, x, y, width, height): super(MainView, self).__init__(title, x, y, width, height) self.filename = "../images/SlantCatImage.png" self.filename = os.path.abspath(self.filename) # 1 Create first imageview. self.source_image_view = self.SourceImageView(self) # 2 Create second imageview. self.noised_image_view = self.NoisedImageView(self) # 3 Load the file self.load_file(self.filename) # 4 Add imageviews to the main_layout which is a horizontal layouter. self.add(self.source_image_view) self.add(self.noised_image_view) self.show() def add_control_pane(self, fixed_width=220): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.sault = 30 self.sault_slider = ZLabeledSlider(self.vpane, "Sault", take_odd =False, minimum=0, maximum=100, value=self.sault) self.sault_slider.add_value_changed_callback(self.sault_changed) self.pepper = 30 self.pepper_slider = ZLabeledSlider(self.vpane, "Pepper", take_odd =False, minimum=0, maximum=100, value=self.sault) self.pepper_slider.add_value_changed_callback(self.pepper_changed) self.vpane.add(self.sault_slider) self.vpane.add(self.pepper_slider) self.set_right_dock(self.vpane) def file_open(self): options = QFileDialog.Options() self.filename, _ = QFileDialog.getOpenFileName(self,"FileOpenDialog", "", "All Files (*);;Image Files (*.png;*jpg;*.jpeg)", options=options) if self.filename: self.load_file(self.filename) def file_save(self): options = QFileDialog.Options() dir, name = os.path.split(self.filename) filename = "Noised" + name save_filename, _ = QFileDialog.getSaveFileName(self,"FileSaveDialog-NoisedImage", filename, "All Files (*);;Image Files (*.png;*jpg;*.jpeg)", options=options) if save_filename: noised_image = self.noised_image_view.get_noised_image() cv2.imwrite(save_filename, noised_image) def load_file(self, filename): self.source_image_view.load(filename) self.noised_image_view.load(filename) self.noised_image_view.inject_noise(self.sault, self.pepper) self.set_filenamed_title(filename) def sault_changed(self, value): self.sault = int(value) #print("slider1_value_changed:{}".format(value)) self.noised_image_view.inject_noise(self.sault, self.pepper) def pepper_changed(self, value): self.pepper = int(value) #print("slider1_value_changed:{}".format(value)) self.noised_image_view.inject_noise(self.sault, self.pepper)
class MainView(ZApplicationView): # Inner classes #-------------------------------------------- class SourceImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) def load(self, filename): self.load_opencv_image(filename) self.update() class DetectedImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) self.gray_image = None def load(self, filename): self.load_opencv_image(filename) source_image = self.get_opencv_image() self.gray_image = cv2.cvtColor(source_image, cv2.COLOR_RGB2GRAY) def detect(self, ksize, sigma, threshold1, paramThreshold): ksize = int(ksize) source_image = self.get_opencv_image() #1 Apply cv::GaussianBlur to the source_image. blurred_image = cv2.GaussianBlur(self.gray_image, (ksize, ksize), sigma, sigma) #2 Apply cv::HoughCircles to the grayImage to detect circles. w, h = blurred_image.shape circles = cv2.HoughCircles(blurred_image, cv2.HOUGH_GRADIENT, 1, minDist=w / 8, param1=threshold1, param2=paramThreshold, minRadius=0, maxRadius=0) if circles is not None: detected_image = source_image.copy() for circle in circles[0, :]: #3 Draw the center of the circle of radius = 3 cv2.circle(detected_image, (circle[0], circle[1]), 3, (0, 255, 0), -1, 8, 0) #4 Draw the outline of the circle of radius cv2.circle(detected_image, (circle[0], circle[1]), circle[2], (0, 0, 255), 3, 8, 0) self.set_opencv_image(detected_image) self.update() #-------------------------------------------- # MainView Constructor def __init__(self, title, x, y, width, height): super(MainView, self).__init__(title, x, y, width, height) filename = "../images/Geometry3.png" # 1 Create first imageview. self.source_image_view = self.SourceImageView(self) # 2 Create second imageview. self.detected_image_view = self.DetectedImageView(self) # 3 Load the file self.load_file(filename) # 4 Add two image views to a main_layout of this main view. self.add(self.source_image_view) self.add(self.detected_image_view) self.show() # Add control pane to MainView def add_control_pane(self, fixed_width=200): # Control pane widget self.vpane = ZVerticalPane(self, fixed_width) self.ksize = 3 self.ksize_slider = ZLabeledSlider(self.vpane, "GaussianBlur KernelSize", take_odd=True, minimum=0, maximum=31, value=self.ksize) self.ksize_slider.add_value_changed_callback(self.ksize_value_changed) self.vpane.add(self.ksize_slider) self.sigma = 2 self.sigma_slider = ZLabeledSlider(self.vpane, "GaussianBlur SigmaSize", take_odd=True, minimum=1, maximum=21, value=self.sigma) self.sigma_slider.add_value_changed_callback(self.sigma_value_changed) self.vpane.add(self.sigma_slider) self.threshold1 = 120 self.threshold2 = 50 self.threshold1_slider = ZLabeledSlider(self.vpane, "CannyEdgeThreshold1", take_odd=True, minimum=0, maximum=300, value=self.threshold1) self.threshold1_slider.add_value_changed_callback( self.slider1_value_changed) self.vpane.add(self.threshold1_slider) self.threshold2_slider = ZLabeledSlider(self.vpane, "ParamThreshold", take_odd=True, minimum=0, maximum=300, value=self.threshold2) self.threshold2_slider.add_value_changed_callback( self.slider2_value_changed) self.vpane.add(self.threshold2_slider) self.set_right_dock(self.vpane) # Show FileOpenDialog and select an image file. def file_open(self): options = QFileDialog.Options() filename, _ = QFileDialog.getOpenFileName( self, "FileOpenDialog", "", "All Files (*);;Image Files (*.png;*jpg;*.jpeg)", options=options) if filename: self.load_file(filename) def load_file(self, filename): self.source_image_view.load(filename) self.detected_image_view.load(filename) self.detected_image_view.detect(int(self.ksize), int(self.sigma), self.threshold1, self.threshold2) self.set_filenamed_title(filename) # Slider value changed callback. def ksize_value_changed(self, value): self.ksize = int(value) if self.ksize % 2 == 0: self.ksize = (self.ksize * 2) / 2 + 1 # Kernel size should be odd. #print("slider_value_changed:{}".format(ksize)) self.detected_image_view.detect(int(self.ksize), int(self.sigma), self.threshold1, self.threshold2) # Slider value changed callback. def sigma_value_changed(self, value): self.sigma = int(value) #print("slider_value_changed:{}".format(ksize)) self.detected_image_view.detect(int(self.ksize), int(self.sigma), self.threshold1, self.threshold2) def slider1_value_changed(self, value): self.threshold1 = int(value) self.detected_image_view.detect(int(self.ksize), int(self.sigma), self.threshold1, self.threshold2) def slider2_value_changed(self, value): self.threshold2 = int(value) self.detected_image_view.detect(int(self.ksize), int(self.sigma), self.threshold1, self.threshold2)
class MainView(ZApplicationView): # Inner classes #-------------------------------------------- class SourceImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) def load(self, filename): self.load_opencv_image(filename) self.update() class DetectedImageView(ZOpenCVImageView): def __init__(self, parent): ZOpenCVImageView.__init__(self, parent) def load(self, filename): source_image = self.load_opencv_image(filename) self.gray_image = cv2.cvtColor(source_image, cv2.COLOR_RGB2GRAY) def detect(self, ksize): ksize = int(ksize) gradX = cv2.Sobel(self.gray_image, -1, 1, 0, ksize=ksize) gradY = cv2.Sobel(self.gray_image, -1, 0, 1, ksize=ksize) absGradX = cv2.convertScaleAbs(gradX) absGradY = cv2.convertScaleAbs(gradY) detected_image = cv2.addWeighted(absGradX, 0.5, absGradY, 0.5, 0) self.set_opencv_image(detected_image) self.update() #-------------------------------------------- # MainView Constructor def __init__(self, title, x, y, width, height): super(MainView, self).__init__(title, x, y, width, height) filename = "../images/ClassicCar.png" # 1 Create first imageview. self.source_image_view = self.SourceImageView(self) # 2 Create second imageview. self.detected_image_view = self.DetectedImageView(self) # 3 Load the file self.load_file(filename) # 4 Add two image views to a main_layout of this main view. self.add(self.source_image_view) self.add(self.detected_image_view) self.show() # Add control pane to MainView def add_control_pane(self, fixed_width=200): # Control pane widget self.ksize = 5 self.vpane = ZVerticalPane(self, fixed_width) self.labeled_slider = ZLabeledSlider(self.vpane, "KernelSize", take_odd=True, minimum=0, maximum=33, value=self.ksize) self.labeled_slider.add_value_changed_callback( self.slider_value_changed) self.vpane.add(self.labeled_slider) self.set_right_dock(self.vpane) # Show FileOpenDialog and select an image file. def file_open(self): options = QFileDialog.Options() filename, _ = QFileDialog.getOpenFileName( self, "FileOpenDialog", "", "All Files (*);;Image Files (*.png;*jpg;*.jpeg)", options=options) if filename: self.load_file(filename) def load_file(self, filename): self.source_image_view.load(filename) self.detected_image_view.load(filename) self.detected_image_view.detect(int(self.ksize)) self.set_filenamed_title(filename) # Slider value changed callback. def slider_value_changed(self, value): self.ksize = int(value) if self.ksize % 2 == 0: self.ksize = (self.ksize * 2) / 2 + 1 # Kernel size should be odd. print("slider_value_changed:{}".format(self.ksize)) self.detected_image_view.detect(int(int(self.ksize)))