Example #1
0
    def sgm(self, imgL, imgR):
        """ Semi global matching funciton, for more details on what this function does check the original paper
        https://elib.dlr.de/73119/1/180Hirschmueller.pdf
        
        :param imgL: Left image. Type: blender image type object.
        :param imgR: Right image. Type: blender image type object.
        :return: depth, disparity
         """
        window_size = self.config.get_int("window_size", 7)
        if window_size % 2 == 0:
            raise Exception("Window size must be an odd number")

        numDisparities = self.config.get_int("num_disparities", 32)
        if not (numDisparities > 0 and numDisparities % 16 == 0):
            raise Exception(
                "Number of disparities must be > 0 and divisible by 16")

        left_matcher = cv2.StereoSGBM_create(
            minDisparity=self.config.get_int("min_disparity", 0),
            numDisparities=numDisparities,
            blockSize=5,
            P1=8 * 3 * window_size**2,
            P2=32 * 3 * window_size**2,
            disp12MaxDiff=-1,
            uniquenessRatio=15,
            speckleWindowSize=0,
            speckleRange=2,
            preFilterCap=63,
            # mode=cv2.STEREO_SGBM_MODE_SGBM_3WAY
            mode=cv2.StereoSGBM_MODE_HH)

        if self.config.get_bool("disparity_filter", True):
            right_matcher = cv2.ximgproc.createRightMatcher(left_matcher)

            lmbda = 80000
            sigma = 1.2

            wls_filter = cv2.ximgproc.createDisparityWLSFilter(
                matcher_left=left_matcher)
            wls_filter.setLambda(lmbda)
            wls_filter.setSigmaColor(sigma)

            dispr = right_matcher.compute(imgR, imgL)

        displ = left_matcher.compute(imgL, imgR)

        filteredImg = None
        if self.config.get_bool("disparity_filter", True):
            filteredImg = wls_filter.filter(displ, imgL, None,
                                            dispr).astype(np.float32)
            filteredImg = cv2.normalize(src=filteredImg,
                                        dst=filteredImg,
                                        beta=0,
                                        alpha=255,
                                        norm_type=cv2.NORM_MINMAX)

        disparity_to_be_written = filteredImg if self.config.get_bool(
            "disparity_filter", True) else displ
        disparity = np.float64(np.copy(disparity_to_be_written)) / 16.0

        # Crop and resize, due to baseline, a part of the image on the left can't be matched with the one on the right
        disparity = resize(disparity[:, numDisparities:],
                           (self.width, self.height))

        # Triangulation
        depth = (1.0 / disparity) * self.baseline * self.focal_length

        # Clip from depth map to 25 meters
        depth[depth > self.depth_max] = self.depth_max
        depth[depth < 0] = 0.0

        if self.config.get_bool("depth_completion", True):
            depth = fill_in_fast(depth, self.depth_max)

        return depth, disparity_to_be_written
Example #2
0
    def sgm(self, imgL, imgR):
        window_size = self.config.get_int("window_size", 7)
        if window_size % 2 == 0:
            raise Exception("Window size must be an odd number")

        numDisparities = self.config.get_int("num_disparities", 32)
        if not (numDisparities > 0 and numDisparities % 16 == 0):
            raise Exception(
                "Number of disparities must be > 0 and divisible by 16")

        left_matcher = cv2.StereoSGBM_create(
            minDisparity=self.config.get_int("min_disparity", 0),
            numDisparities=numDisparities,
            blockSize=5,
            P1=8 * 3 * window_size**2,
            P2=32 * 3 * window_size**2,
            disp12MaxDiff=-1,
            uniquenessRatio=15,
            speckleWindowSize=0,
            speckleRange=2,
            preFilterCap=63,
            # mode=cv2.STEREO_SGBM_MODE_SGBM_3WAY
            mode=cv2.StereoSGBM_MODE_HH)

        if self.config.get_bool("disparity_filter", True):
            right_matcher = cv2.ximgproc.createRightMatcher(left_matcher)

            lmbda = 80000
            sigma = 1.2

            wls_filter = cv2.ximgproc.createDisparityWLSFilter(
                matcher_left=left_matcher)
            wls_filter.setLambda(lmbda)
            wls_filter.setSigmaColor(sigma)

            dispr = right_matcher.compute(imgR, imgL)
            dispr = np.int16(dispr)

        displ = left_matcher.compute(imgL, imgR)
        displ = np.int16(displ)

        filteredImg = None
        if self.config.get_bool("disparity_filter", True):
            filteredImg = wls_filter.filter(displ, imgL, None,
                                            dispr).astype(np.float32)
            filteredImg = cv2.normalize(src=filteredImg,
                                        dst=filteredImg,
                                        beta=0,
                                        alpha=255,
                                        norm_type=cv2.NORM_MINMAX)

        disparity = np.float64(filteredImg) / 16.0 if self.config.get_bool("disparity_filter", True) else \
            np.float64(displ) / 16.0

        # Crop and resize, due to baseline, a part of the image on the left can't be matched with the one on the right
        disparity = resize(disparity[:, numDisparities:],
                           (self.width, self.height))

        # Triangulation
        depth = (1.0 / disparity) * self.baseline * self.focal_length

        # Clip from depth map to 25 meters
        depth[depth > self.depth_max] = self.depth_max
        depth[depth < 0] = 0.0

        if self.config.get_bool("depth_completion", True):
            depth = fill_in_fast(depth, self.depth_max)

        disparity = np.int16(disparity)
        return depth, disparity