コード例 #1
0
    def get_edges(self):

        # get settings of combobox and fields
        param = self._csbox_edges.get_dict()

        # get the currently displayed image
        img = imgtools.project_data_to_img(imgtools.gray_image(
            self.get_obj().get_img(show=True)),
                                           dtype=np.uint8,
                                           factor=255)

        aperture_size = param["Aperture Size"]
        if (aperture_size % 2) == 0 or aperture_size < 3 or aperture_size > 7:
            raise ValueError("Aperture size should be odd between 3 and 7.")

        edges = cv2.Canny(img,
                          param["Threshold I"],
                          param["Threshold II"],
                          apertureSize=param["Aperture Size"])

        # set image in canvas and update histogram
        # self.get_obj().set_img(edges, clear_mask=False)
        # self.set_img()

        # open a topwindow with the edges of the currently displayed image computed via canny
        tw.TopWindow(self, title="Edges", dtype="img", value=[img, edges])
コード例 #2
0
    def compute_dimage(self, event=None):
        """Compute the difference image of the currently images in 'd_image'
        """
        
        # continue if two images are provided
        if len(self._dimage)<2:
            raise IndexError("There are not enough images available to compute the difference.")

        # compute the difference image of the currently images in 'd_image'
        img_a = self._dimage[-1]
        img_b = self._dimage[-2]

        # compute the difference image of the currently images in 'd_image'
        img = np.absolute(imgtools.gray_image(img_a.astype(np.float32))-imgtools.gray_image(img_b.astype(np.float32)))

        #check wheter the image is not empty
        if np.sum(img) == 0:
            raise ValueError("Sum of differences is zero.")

        img = imgtools.project_data_to_img(img)

        # set image in canvas and update histogram
        self.get_obj().set_img(img, clear_mask=False)
        self.set_img()

        # open a topwindow with images used for building the difference
        tw.TopWindow(self, title="Difference of images", dtype="img", value=[img, img_a, img_b])
コード例 #3
0
 def set_popup(self, **kwargs):
     t = tw.TopWindow(self._parent,
                      title="Image Canvas",
                      dtype="img",
                      value=self.get_img(show=True),
                      logger=self._logger)
     t.mainloop()
コード例 #4
0
    def gradient_image(self):
        """Calculate the horizontal and vertical gradients of the currently displayed image
        """

        # https://www.learnopencv.com/histogram-of-oriented-gradients/
        
        # get settings of combobox and fields 
        param = self._csbox_blur.get_dict()
        kernel_size = param["Kernel Size"]

        if (kernel_size%2)==0 or kernel_size>32:
            raise ValueError("Kernel size  must be odd and not larger than 31.")
        
        # get the currently displayed image
        img = imgtools.project_data_to_img(imgtools.gray_image(self.get_obj().get_img(show=True)))

        # calculate gradient
        gradient_x = cv2.Sobel(img, cv2.CV_32F, 1, 0, ksize=kernel_size)
        gradient_y = cv2.Sobel(img, cv2.CV_32F, 0, 1, ksize=kernel_size)
        
        # calculate gradient magnitude and direction (in degrees)
        magnitude, angle = cv2.cartToPolar(gradient_x, gradient_y, angleInDegrees=True)

        # set image in canvas and update histogram
        # self.get_obj().set_img(magnitude, clear_mask=False)
        # self.set_img()

        # open a topwindow with gradient images
        tw.TopWindow(self, title="Gradient Image", dtype="img", value=[img, magnitude, gradient_x, gradient_y])
コード例 #5
0
 def distance_transform(self, **kwargs):
     img_list, _ = self.image_segmentation(show=False, boundaries="find")
     img_list[2] = imgtools.get_distance_transform(img_list[1], label=1)
     self._img_tw = tw.TopWindow(self,
                                 title="Segmentation",
                                 dtype="img",
                                 value=img_list)
コード例 #6
0
    def image_segmentation(self, **kwargs):
        """Compute low-level segmentation methods like felzenswalb'efficient graph based segmentation or k-means based image segementation

        https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_segmentations.html#sphx-glr-auto-examples-segmentation-plot-segmentations-py
        """
        
        # get settings of combobox and fields 
        param = self._csbox_seg.get_dict()

        # get the currently displayed image
        img = self.get_obj().get_img(show=True)

        if param["Model"]=="SLIC" or param["Model"]=="Normalized Cuts":
            # https://scikit-image.org/docs/dev/api/skimage.segmentation.html#skimage.segmentation.slic
            # n_segments = the (approximate) number of labels in the segmented output image.
            # compactness: balances color proximity and space proximity.
            # max_iter: maximum number of iterations of k-means.
            seg_map = segmentation.slic(img, **self._csbox_slic.get_dict(), start_label=1, convert2lab=1)
            seg_map_bound = segmentation.mark_boundaries(img, seg_map)
            seg_map_color = color.label2rgb(seg_map, img, kind='avg', bg_label=0)

            img_list = [seg_map_bound, seg_map_color]

        if param["Model"]=="Normalized Cuts":
            # https://scikit-image.org/docs/stable/api/skimage.future.graph.html#skimage.future.graph.cut_normalized
            # https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_ncut.html
            g = graph.rag_mean_color(img, seg_map, mode='similarity')
            seg_map = graph.cut_normalized(seg_map, g)
            seg_map_bound = segmentation.mark_boundaries(img, seg_map)
            seg_map_color = color.label2rgb(seg_map, img, kind='avg', bg_label=0) 
            img_list.extend([seg_map_bound, seg_map_color])

        elif param["Model"]=="GrabCut":
            # https://docs.opencv.org/master/dd/dfc/tutorial_js_grabcut.html
            
            # get settings of combobox and fields 
            iterCount = self._csbox_grab.get_dict()["iterCount"]

            # get the region of interest
            roi = self.get_obj().get_roi()

            # raise error if the width and height of the roi is not defined
            if not sum(roi[2:4]):
                raise IndexError("There are no images available.")
            
            # allocate mask, background and foreground model
            mask = np.zeros(img.shape[:2],np.uint8)
            bgdModel = np.zeros((1,65),np.float64)
            fgdModel = np.zeros((1,65),np.float64)

            # implement the grabcut algorithm and assign the result of the algorithm to variable img_cut

            # define image list for visualization
            img_list = [img, img_cut, img[roi[1]:roi[1]+roi[3], roi[0]:roi[0]+roi[2], :]]  

        # open a topwindow with the segmentation results of the currently displayed image      
        tw.TopWindow(self, title="Segmentation", dtype="img", value=img_list)      
コード例 #7
0
    def show_dimage(self, event=None):        
        """Show list of difference images in a own topwindow   
        """

        if not len(self._dimage):
            raise IndexError("There are no images available.")

        # open a topwindow with images used for building the difference
        tw.TopWindow(self, title="Difference of images", dtype="img", value=self._dimage)
コード例 #8
0
    def image_segmentation(self, **kwargs):
        """Compute low-level segmentation methods like felzenswalb' efficient graph based segmentation or k-means based image segementation

        https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_segmentations.html#sphx-glr-auto-examples-segmentation-plot-segmentations-py
        """
        # get settings of combobox and fields 
        param = self._csbox_seg.get_dict()

        # get the currently displayed image
        img = self.get_obj().get_img()

        # define image list for visualization
        img_list = [img]

        if param["Model"]=="SLIC":
            # https://scikit-image.org/docs/dev/api/skimage.segmentation.html#skimage.segmentation.slic
            # n_segments = the (approximate) number of labels in the segmented output image.
            # compactness: balances color proximity and space proximity.
            # max_iter: maximum number of iterations of k-means.
            seg_map = segmentation.slic(img, **self._csbox_slic.get_dict(), start_label=1)
            seg_map_bound = segmentation.mark_boundaries(img, seg_map)
            seg_map_color = color.label2rgb(seg_map, img, kind='avg', bg_label=0)

            # define image list for visualization
            img_list.extend([seg_map_bound, seg_map_color])

        elif param["Model"]=="Felzenswalb":
            # https://scikit-image.org/docs/dev/api/skimage.segmentation.html#skimage.segmentation.felzenszwalb.
            seg_map = segmentation.felzenszwalb(img, **self._csbox_felz.get_dict())
            seg_map_bound = segmentation.mark_boundaries(img, seg_map)
            seg_map_color = color.label2rgb(seg_map, img, kind='avg', bg_label=0)

            # define image list for visualization
            img_list.extend([seg_map_bound, seg_map_color])
    
        elif param["Model"]=="Normalized Cuts":
            # https://scikit-image.org/docs/stable/api/skimage.future.graph.html#skimage.future.graph.cut_normalized
            # https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_ncut.html

            seg_map = segmentation.slic(img, **self._csbox_slic.get_dict(), start_label=1)
            seg_map_bound = segmentation.mark_boundaries(img, seg_map)
            seg_map_color = color.label2rgb(seg_map, img, kind='avg', bg_label=0)

            g = graph.rag_mean_color(img, seg_map, mode='similarity')
            seg_map = graph.cut_normalized(seg_map, g)
            seg_map_bound = segmentation.mark_boundaries(img, seg_map)
            seg_map_color = color.label2rgb(seg_map, img, kind='avg', bg_label=0) 
            
            # define image list for visualization
            img_list.extend([seg_map_bound, seg_map_color])

        # open a topwindow with the segmentation results of the currently displayed image      
        self._img_tw = tw.TopWindow(self, title="Segmentation", dtype="img", value=img_list)
        
        self._img_seg = img
        self._seg_map = seg_map
コード例 #9
0
    def shadow_orientation(self, event=None):
        labelimg = imgtools.get_mask_image(
            self.get_obj().get_img_from_label("{label}"),
            index=self.get_obj().get_class(value=False))

        # define the structuring element and apply the opening operation
        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (75, 75))
        labelimg = cv2.morphologyEx(labelimg, cv2.MORPH_ELLIPSE, kernel)

        # get the currently displayed image
        grayimg = imgtools.gray_image(self.get_obj().get_img(show=True))
        grayimg = cv2.medianBlur(grayimg, 7)
        grayimg_label = grayimg * labelimg

        # get settings of combobox and fields
        param = self._csbox_threshold.get_dict()
        thresh = param["Thresh"]
        method = cv2.THRESH_BINARY if param[
            "Thresh"] else cv2.THRESH_BINARY + cv2.THRESH_OTSU

        # implement thresholding and assign the result to the variable dst
        ret, dst = cv2.threshold(grayimg_label[grayimg_label != 0], thresh,
                                 255, method)
        dst = np.where(grayimg_label < ret, 0, 1)
        labelimg_skel = skeletonize(labelimg.astype(np.uint8))

        shdwimg = imgtools.gray_image(
            np.stack([
                imgtools.project_data_to_img(
                    labelimg, dtype=np.uint8, factor=255),
                np.zeros(dst.shape, dtype=np.uint8),
                imgtools.project_data_to_img(dst, dtype=np.uint8, factor=255)
            ],
                     axis=2))
        shdwimg = np.where(labelimg_skel > 0, 255, shdwimg)
        shdwimg = np.where(shdwimg == 0, 105, shdwimg)
        shdwimg = np.where(shdwimg == 29, 0, shdwimg)

        dispimg = self.get_obj().get_img(show=True)
        dispimg[:, :, 0] = np.where(labelimg_skel > 0, 0, dispimg[:, :, 0])
        dispimg[:, :, 1] = np.where(labelimg_skel > 0, 255, dispimg[:, :, 1])
        dispimg[:, :, 2] = np.where(labelimg_skel > 0, 0, dispimg[:, :, 2])
        for r in range(1, shdwimg.shape[0] - 1):
            for c in range(1, shdwimg.shape[1] - 1):
                if len(np.unique(shdwimg[r - 1:r + 2, c - 1:c + 2])) == 3:
                    cv2.circle(dispimg, (c, r), 3, 255, 2)

        tw.TopWindow(self,
                     title="Segmentation",
                     dtype="img",
                     value=[grayimg, shdwimg, grayimg_label, dispimg])
コード例 #10
0
    def set_popup(self, dtype="msg", histogram=False, **kwargs):
        kwargs.update({
            "dtype": dtype,
            "q_cmd": self.quit,
            "logger": self._logger
        })

        if dtype == "img":
            kwargs.update({
                "options": [
                    o for o in self._options
                ],  # if o["require"] in ["image", "basic", "label", "height"]],
                "canvas": {
                    "variables": self._variables
                }
            })

            if histogram:
                if self._cbox_test.get() == "Histogram":
                    t = twhist.TWHist(self._root, **kwargs)
                elif self._cbox_test.get() == "Normal":
                    t = twhnormal.TWHNormal(self._root, self._param["cloud"],
                                            **kwargs)
                elif self._cbox_test.get() == "Filter":
                    t = twhfilter.TWHFilter(self._root, **kwargs)
                elif self._cbox_test.get() == "Features":
                    t = twhfeatures.TWHFeatures(self._root, **kwargs)
                elif self._cbox_test.get() == "Lecture":
                    t = twhlecture.TWHLecture(self._root, **kwargs)
                elif self._cbox_test.get() == "Segmentation":
                    t = twhseg.TWHSeg(self._root, **kwargs)
                elif self._cbox_test.get() == "Segmentation (All)":
                    t = twseg.TWSeg(self._root, **kwargs)
            else:
                t = tw.TopWindow(self._root, **kwargs)
        else:
            t = tw.TopWindow(self._root, **kwargs)
        t.mainloop()
コード例 #11
0
    def image_segmentation_grabcut(self, **kwargs):
        """Compute low-level segmentation methods like felzenswalb' efficient graph based segmentation or k-means based image segementation

        https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_segmentations.html#sphx-glr-auto-examples-segmentation-plot-segmentations-py
        """
        # get settings of combobox and fields
        param = self._csbox_seg.get_dict()

        # get the currently displayed image
        img = self.get_obj().get_img()

        # define image list for visualization
        img_list = [img]

        # https://docs.opencv.org/master/dd/dfc/tutorial_js_grabcut.html

        # get the region of interest
        roi = self.get_obj().get_roi()

        # raise error if the width and height of the roi is not defined
        if not sum(roi[2:4]):
            raise IndexError("There are no images available.")

        # allocate mask, background and foreground model
        mask = np.zeros(img.shape[:2], np.uint8)
        bgdModel = np.zeros((1, 65), np.float64)
        fgdModel = np.zeros((1, 65), np.float64)

        # this modifies mask
        cv2.grabCut(img,
                    mask,
                    roi,
                    bgdModel,
                    fgdModel,
                    **self._csbox_grab.get_dict(),
                    mode=cv2.GC_INIT_WITH_RECT)

        # If mask==2 or mask== 1, mask2 get 0, other wise it gets 1 as 'uint8' type.
        seg_map = np.where((mask == 2) | (mask == 0), 0, 1).astype('bool')
        img_cut = img * seg_map[:, :, np.newaxis]

        # define image list for visualization
        img_list = [
            img, img_cut, img[roi[1]:roi[1] + roi[3],
                              roi[0]:roi[0] + roi[2], :]
        ]

        # open a topwindow with the segmentation results of the currently displayed image
        tw.TopWindow(self, title="Segmentation", dtype="img", value=img_list)
コード例 #12
0
    def image_segmentation(self, show=True, **kwargs):
        """Compute low-level segmentation methods like felzenswalb' efficient graph based segmentation or k-means based image segementation

        https://scikit-image.org/docs/dev/auto_examples/segmentation/plot_segmentations.html#sphx-glr-auto-examples-segmentation-plot-segmentations-py
        """
        # get settings of combobox and fields
        param_seg = self._csbox_seg.get_dict()

        # get the currently displayed image
        img = imgtools.project_and_stack(self.get_obj().get_img_from_label(
            param_seg["domain"], dtype=np.uint8, factor=255))
        # img = self.get_obj().get_img_from_label("height")
        # param_seg["reference"] = self.get_obj().get_img_from_label("height")
        param_seg["boundaries"] = "find"
        # define image list for visualization
        img_list = [img]

        mode = param_seg["mode"]
        if mode == "SLIC" or mode == "SLIC-0" or mode == "Normalized Cuts":
            param_model = self._csbox_slic.get_dict()
        elif mode == "Felzenswalb":
            param_model = self._csbox_felz.get_dict()
        elif mode == "KMeans":
            param_model = self._csbox_kmeans.get_dict()

        seg = rsvis.utils.imgseg.ImgSeg(**param_seg)
        seg.predict(img, **param_model)
        img_list.extend([
            seg.get_seg_map_color(),
            seg.get_seg_map_boundaries(img=imgtools.project_and_stack(
                self.get_obj().get_img_from_label(param_seg["reference"]),
                dtype=np.uint8,
                factor=255))
        ])

        print("Number of segments: {}".format(seg.get_num_label()))

        # open a topwindow with the segmentation results of the currently displayed image

        if show:
            self.get_obj().set_img(img_list[2], clear_mask=False)
            self.set_img()

            self._img_tw = tw.TopWindow(self,
                                        title="Segmentation",
                                        dtype="img",
                                        value=img_list)
        return img_list
コード例 #13
0
    def show_box(self, event=None):        
        """Show list of boxes 
        """

        # get the region of interest
        roi = self.get_obj().get_roi()

        # raise error if the width and height of the roi is not defined
        if not sum(roi[2:4]):
            raise IndexError("There are no images available.")
        
        # get the currently displayed image
        img = self.get_obj().get_img(show=True)

        # open a topwindow with images used for building the difference
        tw.TopWindow(self, title="Boxes", dtype="img", value=[img[roi[1]:roi[1]+roi[3], roi[0]:roi[0]+roi[2], :]])
コード例 #14
0
    def get_hough_transform(self, event=None):

        # get settings of combobox and fields
        param_edges = self._csbox_edges.get_dict()
        param_hough = self._csbox_hough.get_dict()

        # get the currently displayed image
        img = self.get_obj().get_img(show=True)
        grayimg = imgtools.gray_image(img)

        aperture_size = param_edges["Aperture Size"]
        if (aperture_size % 2) == 0 or aperture_size < 3 or aperture_size > 7:
            raise ValueError("Aperture size should be odd between 3 and 7.")

        edgeimg = cv2.Canny(grayimg,
                            param_edges["Threshold I"],
                            param_edges["Threshold II"],
                            apertureSize=param_edges["Aperture Size"])

        img_list = [img, edgeimg]

        lines = cv2.HoughLinesP(
            edgeimg,
            1,
            np.pi / 180,
            param_hough["Threshold"],
            minLineLength=param_hough["Minimum Line Length"],
            maxLineGap=param_hough["Maximum Line Gap"])

        if lines is not None:
            houghimg = img.copy()
            for line in lines:
                x1, y1, x2, y2 = line[0]
                cv2.line(houghimg, (x1, y1), (x2, y2), (0, 0, 128), 1)

            img_list.append(houghimg)

        # open a topwindow with the edges of the currently displayed image computed via hough transform
        tw.TopWindow(self,
                     title="Hough Transform",
                     dtype="img",
                     value=img_list)
コード例 #15
0
    def mtarsi_shadow(self, show=True, **kwargs):
        # get the currently displayed image
        img = self.get_obj().get_img()

        # param_resize = self._csbox_resize.get_dict()
        # img = imgtools.resize_image(img, param_resize["factor"])

        param_blur = self._csbox_blur.get_dict()
        img_filtered = cv2.bilateralFilter(img, param_blur["d"],
                                           param_blur["sigmaColor"],
                                           param_blur["sigmaSpace"])

        param_seg = self._csbox_seg.get_dict()
        param_seg["mode"] = "SLIC-0"
        param_model = self._csbox_slic.get_dict()
        seg = rsvis.utils.imgseg.ImgSeg(**param_seg)
        seg.predict(img_filtered, **param_model)

        seg_map_color = seg.get_seg_map_color()

        placeh, placei, placej, placex, grayimg = imgtools.get_placeholder(
            img,
            seg_map_color,
            min_size=self._csbox_shdw_mt.get_dict()["min_size"])

        img_list = [img, seg_map_color, grayimg]
        self._img_tw = tw.TopWindow(self,
                                    title="Segmentation",
                                    dtype="img",
                                    value=img_list)

        plt.plot(placex, placeh)
        plt.plot(placex, placei)
        plt.plot(placex, placej)
        # plt.plot(placex, placej)
        plt.ylabel('some numbers')
        plt.show()
コード例 #16
0
    def show_box(self, event=None):        
        """Show list of boxes 
        """

        # get the region of interest
        roi = self.get_obj().get_roi()

        # raise error if the width and height of the roi is not defined
        if not sum(roi[2:4]):
            raise IndexError("There are no images available.")
        
        # get the currently displayed image
        img = self.get_obj().get_img(show=True)

        # open a topwindow with images used for building the difference
        tw.TopWindow(self, title="Boxes", dtype="img", value=[img[roi[1]:roi[1]+roi[3], roi[0]:roi[0]+roi[2], :]])

    # #   CENTROIDS -----------------------------------------------------------
    # # -----------------------------------------------------------------------

        # # set combobox and settingsbox for kmeans
        # self._csbox_centroids = csbox.CSBox(self, bbox=[["Reset Centroids", "Set Centroids", "Compute Centroids (Color)", "Compute Centroids (Color+Space)"], [self.reset_centroids, self.set_centroids, self.get_centroids_color, self.get_centroids_color_space]], sbox=[["Centroids"], [3], ["int"]])
        # self._csbox_centroids.grid(row=4, column=1, rowspan=5, sticky=W+E)


    # #   method --------------------------------------------------------------
    # # -----------------------------------------------------------------------
    # def get_cmap(self, n, name='hsv'):
    #     '''Returns a function that maps each index in 0, 1, ..., n-1 to a distinct 
    #     RGB color; the keyword argument name must be a standard mpl colormap name.'''
    #     cmap = plt.cm.get_cmap(name, n)
    #     cmap = [list(cmap(c)[0:3]) for c in range(0, n)]

    #     return cmap

    # #   method --------------------------------------------------------------
    # # -----------------------------------------------------------------------
    # def get_centroids_color(self, event=None):
    #     img = self.get_obj().get_img(show=True).astype(np.float)
    #     self._centroids_img_shape = (img.shape[0], img.shape[1]) 

    #     data = whiten(img.reshape((-1,3)))
    #     self.get_centroids(data)

    # #   method --------------------------------------------------------------
    # # -----------------------------------------------------------------------
    # def get_centroids_color_space(self, event=None):
    #     img = self.get_obj().get_img(show=True).astype(np.float)
    #     self._centroids_img_shape = (img.shape[0], img.shape[1]) 

    #     grid = np.indices((self._centroids_img_shape), dtype=np.float)
    #     data = whiten(np.stack([img[...,0], img[...,1], img[...,2], grid[0], grid[1]], axis=2).reshape((-1,5)))
    #     self.get_centroids(data)

    # #   method --------------------------------------------------------------
    # # -----------------------------------------------------------------------
    # def get_centroids(self, data, event=None):     
    #     if not self._centroids:
    #         number = self._csbox_centroids.get_dict()["Centroids"]
    #         codes = number
    #         minit = "++"
    #     else:
    #         number = len(self._centroids)
    #         codes = np.stack(self._centroids, axis=0).astype(np.float)
    #         minit = "matrix"

    #     centroids, label = kmeans2(data, codes, minit=minit)
    #     label = label.reshape(self._centroids_img_shape)

    #     mask_list = [np.where(label==idx, 1, 0).astype(np.uint8) for idx in range(len(centroids))]
    #     mask_color = np.random.randint(0, 255, number*3, dtype=np.uint8).reshape((number,3)).tolist()
    #     mask_alpha = [150]*number
    #     mask_invert = [False]*number

    #     self.get_obj().set_mask(mask=mask_list, color=mask_color
    #     , invert=mask_invert, alpha=mask_alpha, show=True)

    # #   method --------------------------------------------------------------
    # # -----------------------------------------------------------------------
    # def reset_centroids(self, event=None): 
    #     self._centroids = list()   

    # #   method --------------------------------------------------------------
    # # -----------------------------------------------------------------------
    # def set_centroids(self, event=None):
    #     self._centroids.append(self.get_obj()._data_img[self.get_obj()._mouse_img[0], self.get_obj()._mouse_img[1], :])
コード例 #17
0
    def mtarsi_segmentation(self, show=True, **kwargs):
        # get the currently displayed image
        img = self.get_obj().get_img()

        param = gu.update_dict(self._csbox_seg_mt.get_dict(),
                               self._csbox_blur.get_dict())

        # define image list for visualization
        img_list = [img]
        cluster_list = list()

        new_shape = (math.ceil(img.shape[0] * param["factor"]),
                     math.ceil(img.shape[1] * param["factor"]))
        img = cv2.resize(img, (new_shape[1], new_shape[0]),
                         dst=cv2.CV_8UC3,
                         interpolation=cv2.INTER_CUBIC)

        for i in range(param["n_filters"]):
            img = cv2.bilateralFilter(img, param["Diameter"],
                                      param["Sigma Color"],
                                      param["Sigma Space"])

        img_list.append(img)

        slic = rsvis.utils.imgseg.segmentation_slic(
            img,
            **gu.update_dict(
                self._csbox_slic.get_dict(),
                {"n_segments": (int(img.shape[0] + img.shape[1]) * 4)}),
            **self._csbox_bound.get_dict(),
            slic_zero=True)
        img_list.append(slic[1])

        cluster = rsvis.utils.imgseg.segmentation_kmeans_color(
            img, **self._csbox_kmeans.get_dict(),
            **self._csbox_bound.get_dict())
        print(np.unique(cluster[0]))
        cluster_list.append(cluster[0])
        img_list.append(
            imgtools.project_data_to_img(cluster[0],
                                         dtype=np.uint8,
                                         factor=255))

        param_cluster = self._csbox_kmeans.get_dict()
        param_cluster["non_pos"] = 1
        cluster = rsvis.utils.imgseg.segmentation_kmeans_color_pos(
            img, **param_cluster, **self._csbox_bound.get_dict())
        print(np.unique(cluster[0]))
        cluster_list.append(cluster[0])
        img_list.append(
            imgtools.project_data_to_img(cluster[0],
                                         dtype=np.uint8,
                                         factor=255))

        param_cluster["non_pos"] = 0
        cluster = rsvis.utils.imgseg.segmentation_kmeans_color_pos(
            img, **param_cluster, **self._csbox_bound.get_dict())
        print(np.unique(cluster[0]))
        cluster_list.append(cluster[0])
        img_list.append(
            imgtools.project_data_to_img(cluster[0],
                                         dtype=np.uint8,
                                         factor=255))

        n_clusters = param_cluster["n_clusters"]

        img_new = np.zeros(img.shape[:2], dtype=np.uint8)

        clstr_img_src = cluster_list[0]
        clstr_img_dst = cluster_list[1]
        clstr_img_est = cluster_list[2]

        clstr_cmp_src_dst = np.zeros((n_clusters, 2))
        for clstr in range(n_clusters):
            # clstr_cmp = clstr_img_dst[clstr_img_src==clstr]
            # print(clstr_cmp.shape, np.unique(clstr_cmp))

            # print(clstr_img_src.shape,np.unique(clstr_img_src))
            # print(clstr_img_dst.shape,np.unique(clstr_img_dst))

            # print(clstr)
            clstr_src_dst = clstr_img_dst[(clstr_img_src == clstr).astype(
                np.bool)]
            # bbbb = np.where(clstr_img_src==clstr, clstr_img_dst, -1).astype(np.int8)

            # clstr_hist = np.histogram(bbbb, bins=range(0,n_clusters+1))
            # clstr_hist_norm = clstr_hist[0]/np.sum(clstr_hist[0])
            # print(clstr_hist_norm)
            clstr_hist = np.histogram(clstr_src_dst,
                                      bins=range(0, n_clusters + 1))
            clstr_hist_norm = clstr_hist[0] / np.sum(clstr_hist[0])
            print(clstr_hist_norm)
            hist_max = np.amax(clstr_hist_norm)
            print(hist_max)
            hist_max_idx = int(np.where(clstr_hist_norm == hist_max)[0])
            print("Max: {}, Idx-Max: {}, Stats: {}".format(
                hist_max, hist_max_idx, np.std(clstr_hist_norm)))
            clstr_cmp_src_dst = hist_max_idx

            cc = False
            if np.std(clstr_hist_norm) > 0.3:
                cc = True

            clstr_dst_src = clstr_img_src[clstr_img_dst == hist_max_idx]
            # bbbb = np.where(clstr_img_dst==hist_max_idx, clstr_img_src, -1).astype(np.int8)

            # clstr_hist = np.histogram(bbbb, bins=range(0,n_clusters+1))
            # clstr_hist_norm = clstr_hist[0]/np.sum(clstr_hist[0])
            # print(clstr_hist_norm)
            clstr_hist = np.histogram(clstr_dst_src,
                                      bins=range(0, n_clusters + 1))
            clstr_hist_norm = clstr_hist[0] / np.sum(clstr_hist[0])
            print(clstr_hist_norm)
            hist_max = np.amax(clstr_hist_norm)
            print(hist_max)
            hist_max_idx = int(np.where(clstr_hist_norm == hist_max)[0])
            print("Max: {}, Idx-Max: {}, Stats: {}".format(
                hist_max, hist_max_idx, np.std(clstr_hist_norm)))
            if clstr == hist_max_idx and cc:
                print("oberblubber")
                clstr_src_dst = clstr_img_est[(clstr_img_src == clstr).astype(
                    np.bool)]
                clstr_hist = np.histogram(clstr_src_dst,
                                          bins=range(0, n_clusters + 1))
                clstr_hist_norm = clstr_hist[0] / np.sum(clstr_hist[0])
                # print(clstr_hist_norm)
                hist_max = np.amax(clstr_hist_norm)
                # print(hist_max)
                hist_max_idx = int(np.where(clstr_hist_norm == hist_max)[0])
                # print("Max: {}, Idx-Max: {}, Stats: {}".format(hist_max, hist_max_idx, np.std(clstr_hist_norm)))
                clstr_cmp_src_est = hist_max_idx

                cc = False
                if np.std(clstr_hist_norm) > 0.3:
                    cc = True

                clstr_dst_src = clstr_img_src[clstr_img_est == hist_max_idx]
                clstr_hist = np.histogram(clstr_dst_src,
                                          bins=range(0, n_clusters + 1))
                clstr_hist_norm = clstr_hist[0] / np.sum(clstr_hist[0])
                # print(clstr_hist_norm)
                hist_max = np.amax(clstr_hist_norm)
                # print(hist_max)
                hist_max_idx = int(np.where(clstr_hist_norm == hist_max)[0])

                if clstr == hist_max_idx and cc:
                    print("blubber")
                    img_new += np.where(clstr_img_src == clstr, 1,
                                        0).astype(np.uint8)

        print(clstr_cmp_src_dst)

        img_list.append(img_new)

        # # # # seg_map_slic, seg_map_color_slic, seg_map_bound_slic = rsvis.utils.imgseg.segmentation_slic(img, **gu.update_dict(self._csbox_slic.get_dict(), {"n_segments":(int(img.shape[0]+img.shape[1])/2)}), **self._csbox_bound.get_dict(), slic_zero=True)

        # # # # seg_map_kmeans, seg_map_bound_kmeans = rsvis.utils.imgseg.segmentation_kmeans_color(img, **self._csbox_kmeans.get_dict(), **self._csbox_bound.get_dict())

        # # # # # define image list for visualization
        # # # # img_list.extend([seg_map_bound_kmeans, seg_map_bound_slic])
        # # # # seg_map_bound_kmeans = imgtools.project_data_to_img(seg_map_bound_kmeans,dtype=np.uint8, factor=255)
        # # # # seg_map_kmeans = seg_map_kmeans.astype(np.uint8)
        # # # # num = np.unique(seg_map_kmeans)
        # # # # seg_map_kmeans_new= np.zeros(seg_map_kmeans.shape, dtype=np.uint8)
        # # # # for i in np.unique(seg_map_slic):
        # # # #     mask = np.where(seg_map_slic==i, 1, 0).astype(np.ubyte)
        # # # #     hist = cv2.calcHist([seg_map_kmeans], [0], mask, [len(num)], [0,len(num)])
        # # # #     hist_max_index = np.where(hist == np.amax(hist))
        # # # #     if len(hist_max_index[0]) > 1:
        # # # #         hist_max_index = hist_max_index[0]
        # # # #     seg_map_kmeans_new += mask.astype(np.uint8)[:,:,np.newaxis]*np.uint8(hist_max_index[0])
        # # # # seg_map_kmeans_new_map = imgtools.project_data_to_img(seg_map_kmeans_new, factor=255, dtype=np.uint8)
        # # # # img_list.extend([seg_map_kmeans_new_map])

        # # # # hist = cv2.calcHist([seg_map_kmeans_new], [0], None, [len(num)], [0,len(num)])
        # # # # hist_max_count = np.where(hist == np.amax(hist))
        # # # # hist_list=[hist_max_count[0]]
        # # # # for i in num:
        # # # #     hist[i] = np.mean(img[np.concatenate([seg_map_kmeans_new]*3, axis=2)==i])
        # # # # hist_min_bright = np.where(hist == np.amin(hist))
        # # # # hist_list.append(hist_min_bright[0])
        # # # # print(hist_list)
        # # # # aba = [i for i in num if i not in hist_list]
        # # # # # allocate mask, background and foreground model
        # # # # seg_map_kmeans_new = seg_map_kmeans_new.astype(np.uint8)
        # # # # mask = np.zeros(img.shape[:2],dtype=np.uint8)
        # # # # mask += np.uint8(2)*np.where(np.squeeze(seg_map_kmeans_new, axis=2)==np.uint8(hist_max_count),1,0).astype(np.uint8)
        # # # # mask += np.uint8(3)*np.where(np.squeeze(seg_map_kmeans_new, axis=2)==np.uint8(hist_min_bright[0]),1,0).astype(np.uint8)
        # # # # mask += np.uint8(3)*np.where(np.squeeze(seg_map_kmeans_new, axis=2)==np.uint8(aba[0]),1,0).astype(np.uint8)
        # # # # bgdModel = np.zeros((1,65),np.float64)
        # # # # fgdModel = np.zeros((1,65),np.float64)
        # # # # w,h,d = img.shape
        # # # # roi = (0,0,w,h)
        # # # # # this modifies mask

        # # # # seg_map_slic, seg_map_color_slic, seg_map_bound_slic = rsvis.utils.imgseg.segmentation_slic(img, **gu.update_dict(self._csbox_slic.get_dict(), {"n_segments":(int(img.shape[0]+img.shape[1])*4)}), **self._csbox_bound.get_dict(), slic_zero=True)

        # # # # img_list.extend([imgtools.project_data_to_img(mask,factor=255, dtype=np.uint8)])
        # # # # # cv2.grabCut(cv2.bilateralFilter(img, d=5, sigmaColor=100, sigmaSpace=500), mask, roi, bgdModel, fgdModel, 10, mode=cv2.GC_INIT_WITH_MASK)

        # # # # cv2.grabCut(img, mask, roi, bgdModel, fgdModel, 40, mode=cv2.GC_INIT_WITH_MASK)

        # # # # # If mask==2 or mask== 1, mask2 get 0, other wise it gets 1 as 'uint8' type.
        # # # # seg_map = np.where((mask==2)|(mask==0), 0, 1).astype('bool')
        # # # # seg_map = img*seg_map[:,:,np.newaxis]
        # # # # img_list.extend([seg_map])
        # # # # seg_map = np.where((mask==2)|(mask==0), 1, 0).astype('bool')
        # # # # seg_map = img*seg_map[:,:,np.newaxis]
        # # # # img_list.extend([seg_map])

        # open a topwindow with the segmentation results of the currently displayed image
        if show:
            self._img_tw = tw.TopWindow(self,
                                        title="Segmentation",
                                        dtype="img",
                                        value=img_list)
コード例 #18
0
    def blubb(self, **kwargs):

        img = self.get_obj().get_img()

        param_blur = self._csbox_blur.get_dict()
        img_filtered = cv2.bilateralFilter(img, param_blur["d"],
                                           param_blur["sigmaColor"],
                                           param_blur["sigmaSpace"])

        seg_list_a = list()
        img_list_a = list()

        param_model = self._csbox_kmeans.get_dict()

        param_seg = self._csbox_seg.get_dict()
        param_seg["kind"] = "overlay"
        param_seg["mode"] = "KMeans"

        param_seg["color"] = 1
        param_seg["position"] = 0

        n_clusters = 3
        for i in range(3):
            param_model["n_clusters"] = n_clusters
            seg = rsvis.utils.imgseg.ImgSeg(**param_seg)
            seg.predict(img_filtered, **param_model)

            seg_list_a.append(seg.get_seg_map())
            img_list_a.append(seg.get_seg_map_color())
            n_clusters += 1

        param_seg["position"] = 1
        n_clusters = 3

        seg_list_b = list()
        img_list_b = list()
        n_clusters = 6
        for i in range(3):
            param_model["n_clusters"] = n_clusters
            seg = rsvis.utils.imgseg.ImgSeg(**param_seg)
            seg.predict(img_filtered, **param_model)

            seg_list_b.append(seg.get_seg_map())
            img_list_b.append(seg.get_seg_map_color())
            n_clusters += 1

        # param_seg["color"] = 0
        # param_seg["position"] = 1
        # n_clusters = 3

        # seg_list_c = list()
        # img_list_c = list()
        # n_clusters = 4
        # for i in range(3):
        #     param_model["n_clusters"] = n_clusters
        #     seg = rsvis.utils.imgseg.ImgSeg(**param_seg)
        #     seg.predict(img_filtered, **param_model)

        #     seg_list_c.append(seg.get_seg_map())
        #     img_list_c.append(seg.get_seg_map_color())
        #     n_clusters += 1

        img_list_a.extend(img_list_b)
        # img_list_a.extend(img_list_c)
        segcmp = rsvis.utils.imgsegcmp.ImgSegCmp()
        segcmp.predict(seg_list_a)
        segcmp.predict(seg_list_b)
        self._img_tw = tw.TopWindow(self,
                                    title="Segmentation",
                                    dtype="img",
                                    value=img_list_a)