Ejemplo n.º 1
0
 def create_and_warp_mask(self, size, camera, aspect=1):
     warper = cv.PyRotationWarper(self.warper_type, self.scale * aspect)
     mask = 255 * np.ones((size[1], size[0]), np.uint8)
     _, warped_mask = warper.warp(mask, Warper.get_K(camera,
                                                     aspect), camera.R,
                                  cv.INTER_NEAREST, cv.BORDER_CONSTANT)
     return warped_mask
Ejemplo n.º 2
0
    def getAllMasksWarped(self):
        warper = cv.PyRotationWarper(
            self.warp_type, self.warped_image_scale *
            self.seam_work_aspect)  # warper could be nullptr?

        num_images = self.images.count
        for idx in range(0, num_images):
            K = self.all_cameras[idx].K().astype(np.float32)
            swa = self.seam_work_aspect
            K[0, 0] *= swa
            K[0, 2] *= swa
            K[1, 1] *= swa
            K[1, 2] *= swa
            corner, image_wp = warper.warp(self.images[idx], K,
                                           self.all_cameras[idx].R,
                                           cv.INTER_LINEAR, cv.BORDER_REFLECT)
            self.corners.append(corner)
            self.sizes.append((image_wp.shape[1], image_wp.shape[0]))
            self.images_warped.append(image_wp)
            p, mask_wp = warper.warp(self.masks[idx], K,
                                     self.all_cameras[idx].R, cv.INTER_NEAREST,
                                     cv.BORDER_CONSTANT)
            self.masks_warped.append(mask_wp.get())

            for img in self.images_warped:
                imgf = img.astype(np.float32)
                self.images_warped_f.append(imgf)
Ejemplo n.º 3
0
def restaurer_configuration():
    try:
        fic_config = cv.FileStorage("WebCamPanoramique.yml", cv.FILE_STORAGE_READ)
    except IOError:
        print("file is empty or does not exist")
        return False, ParamPano()
    except (cv.error,SystemError):
        print("invalid file")
        return False, ParamPano()
    if not fic_config.isOpened():
        return False, ParamPano()
    pano = ParamPano()
    pano.init = fic_config.getNode("init")
    pano.fct_couture = fic_config.getNode("fct_couture").string()
    pano.surface_compo = fic_config.getNode("surface_composition").string()
    pano.fct_cout = fic_config.getNode("fct_cout").string()
    pano.correction_exposition = int(fic_config.getNode("correction_exposition").real())
    pano.force_melange = fic_config.getNode("force_melange").real()
    pano.seuil_confiance = fic_config.getNode("seuil_confiance").real()
    pano.seuil_appariement = fic_config.getNode("seuil_appariement").real()
    taille = int(fic_config.getNode("taille").real())
    pano.focale_moyenne = fic_config.getNode("focale_moyenne").real()
    pano.indices = fic_config.getNode("indices").mat()
    for i in range(taille):
        taille = fic_config.getNode("tailleImage" + str(i)).mat()
        focale = fic_config.getNode("focal" + str(i)).real()
        pano.focales.append(focale)
        pos_coin = fic_config.getNode("coin" + str(i)).mat()
        pos_coin = pos_coin.astype(int)
        pano.liste_pos_coin.append(tuple(pos_coin.transpose()[0].tolist()))
        taille_masque = fic_config.getNode("liste_taille_masque" + str(i)).mat()
        taille_masque = taille_masque.astype(np.uint32)
        pano.liste_taille_masque.append(tuple(taille_masque.transpose()[0].tolist()))
        img_masque = fic_config.getNode("masque" + str(i)).mat()
        pano.liste_masque_compo.append(cv.UMat(img_masque))
        camera = cv.detail_CameraParams()
        camera.aspect = 1
        camera.t = np.zeros((3, 1), np.float64)
        camera.R = fic_config.getNode("cameraRot" + str(i)).mat()
        camera.focal = fic_config.getNode("cameraFocal" + str(i)).real()
        camera.ppx = fic_config.getNode("cameraPPX" + str(i)).real()
        camera.ppy = fic_config.getNode("cameraPPY" + str(i)).real()
        pano.cameras.append(camera)
    taille = int(fic_config.getNode("gainsize").real())
    pano.gains = []
    for i in range(taille):
        node = fic_config.getNode("gain" + str(i))
        if node.isReal() or node.isInt():
            img_gain = np.array([node.real()])
        else:
            img_gain = node.mat()
        pano.gains.append(img_gain)
    pano.composition = cv.PyRotationWarper(pano.surface_compo, pano.focale_moyenne)
    pano.algo_correct_expo = cv.detail.ExposureCompensator_createDefault(pano.correction_exposition)
    pano.algo_correct_expo.setMatGains(pano.gains)
    pano.algo_correct_expo.setUpdateGain(False)
    return True, pano
Ejemplo n.º 4
0
 def applyBlender(self):
     for idx, full_img in enumerate(self.images):
         if not self.is_compose_scale_set:
             if self.compose_megapix > 0:
                 compose_scale = min(
                     1.0,
                     np.sqrt(self.compose_megapix * 1e6 /
                             (full_img.shape[0] * full_img.shape[1])))
             self.is_compose_scale_set = True
             compose_work_aspect = self.compose_scale / self.work_scale
             self.warped_image_scale *= compose_work_aspect
             # a new warper
             warper = cv.PyRotationWarper(self.warp_type,
                                          self.warped_image_scale)
             for i in range(0, len(img_names)):
                 cameras[i].focal *= compose_work_aspect
                 cameras[i].ppx *= compose_work_aspect
                 cameras[i].ppy *= compose_work_aspect
                 sz = (full_img_sizes[i][0] * compose_scale,
                       full_img_sizes[i][1] * compose_scale)
                 K = cameras[i].K().astype(np.float32)
                 roi = warper.warpRoi(sz, K, cameras[i].R)
                 corners.append(roi[0:2])
                 sizes.append(roi[2:4])
Ejemplo n.º 5
0
 def update_scale(self, scale):
     if scale is not None and scale != self.scale:
         self.warper = cv.PyRotationWarper(
             self.warper_type, scale
         )  # setScale not working: https://docs.opencv.org/master/d5/d76/classcv_1_1PyRotationWarper.html#a90b000bb75f95294f9b0b6ec9859eb55
         self.scale = scale
Ejemplo n.º 6
0
 def __init__(self, warper_type=DEFAULT_WARP_TYPE, scale=1):
     self.warper_type = warper_type
     self.warper = cv.PyRotationWarper(warper_type, scale)
     self.scale = scale
Ejemplo n.º 7
0
def stitch_fast(data, use_kaze=False):
    """waaay faster than the other stitch() method. The results are slightly worse, but for one set of 45 images it
    took 33 seconds using this stitch_fast() compared to 607 seconds using stitch()"""
    use_gpu = False
    work_megapix = -1
    seam_megapix = 0.1
    compose_megapix = -1
    wave_correct = "horiz"  # "vert"
    warp_type = "cylindrical"  # "spherical"  #"mercator"  #"cylindrical"
    match_conf = 0.3
    blend_type = "multiband"  # feather  # multiband #for no blending at all put any other string, like "no"
    blend_strength = 5

    if use_kaze:
        finder = cv2.KAZE.create()
    else:
        finder = cv2.ORB.create()

    seam_work_aspect = 1
    features = []
    images = []
    print("getting image features and scaling images...")
    work_scale = -1
    seam_scale = -1
    for i in range(len(data[0][1])):
        full_img = data[0][1][i]

        if work_megapix < 0:
            img = full_img
            work_scale = 1
        else:
            if work_scale == -1:  # if it hasn't been set yet
                work_scale = min(
                    1.0,
                    np.sqrt(work_megapix * 1e6 /
                            (full_img.shape[0] * full_img.shape[1])))
            img = cv2.resize(src=full_img,
                             dsize=None,
                             fx=work_scale,
                             fy=work_scale,
                             interpolation=cv2.INTER_LINEAR_EXACT)
        if seam_scale == -1:  # if it hasn't been set yet
            seam_scale = min(
                1.0,
                np.sqrt(seam_megapix * 1e6 /
                        (full_img.shape[0] * full_img.shape[1])))
            seam_work_aspect = seam_scale / work_scale
        features.append(cv2.detail.computeImageFeatures2(
            finder, img))  # gets image features
        images.append(
            cv2.resize(src=full_img,
                       dsize=None,
                       fx=seam_scale,
                       fy=seam_scale,
                       interpolation=cv2.INTER_LINEAR_EXACT))

    print("getting matches info...")
    matcher = cv2.detail.BestOf2NearestMatcher_create(use_gpu, match_conf)

    # setting the matching mask makes it a lot faster because it tells it the order of images:
    # https://software.intel.com/sites/default/files/Fast%20Panorama%20Stitching.pdf
    match_mask = np.zeros((len(features), len(features)), np.uint8)
    for i in range(len(data[0][1]) - 1):
        match_mask[i, i + 1] = 1

    matches_info = matcher.apply2(features, match_mask)
    matcher.collectGarbage()

    num_images = len(data[0][1])

    # get camera params
    print("finding camera params...")
    estimator = cv2.detail_HomographyBasedEstimator()
    b, cameras = estimator.apply(features, matches_info, None)
    if not b:
        print("Homography estimation failed.")
        exit()
    for cam in cameras:
        cam.R = cam.R.astype(np.float32)

    # adjust camera params
    print("adjusting camera params...")
    adjuster = cv2.detail_BundleAdjusterRay()
    adjuster.setConfThresh(1)
    b, cameras = adjuster.apply(features, matches_info, cameras)
    if not b:
        print("Camera parameters adjusting failed.")
        exit()

    # get warped image scale
    print("getting warped image scale...")
    focals = []
    for cam in cameras:
        focals.append(cam.focal)
    sorted(focals)
    if len(focals) % 2 == 1:
        warped_image_scale = focals[len(focals) // 2]
    else:
        warped_image_scale = (focals[len(focals) // 2] +
                              focals[len(focals) // 2 - 1]) / 2

    # wave correct. see section 5 of this paper: http://matthewalunbrown.com/papers/ijcv2007.pdf
    print("wave correction...")
    rmats = []
    for cam in cameras:
        rmats.append(np.copy(cam.R))

    if wave_correct == "horiz":
        rmats = cv2.detail.waveCorrect(rmats, cv2.detail.WAVE_CORRECT_HORIZ)
    elif wave_correct == "vert":
        rmats = cv2.detail.waveCorrect(rmats, cv2.detail.WAVE_CORRECT_VERT)

    for i in range(len(cameras)):
        cameras[i].R = rmats[i]

    masks_warped = []
    images_warped = []
    masks = []

    # create masks
    for i in range(num_images):
        um = cv2.UMat(255 * np.ones(
            (images[i].shape[0], images[i].shape[1]), np.uint8))
        masks.append(um)

    # warp images and masks
    print("warping...")
    warper = cv2.PyRotationWarper(warp_type,
                                  warped_image_scale * seam_work_aspect)
    print()

    corners = []
    for i in range(num_images):
        K = cameras[i].K().astype(np.float32)
        K[0, 0] *= seam_work_aspect
        K[0, 2] *= seam_work_aspect
        K[1, 1] *= seam_work_aspect
        K[1, 2] *= seam_work_aspect

        corner, image_wp = warper.warp(images[i], K, cameras[i].R,
                                       cv2.INTER_LINEAR, cv2.BORDER_REFLECT)
        images_warped.append(image_wp)
        corners.append(corner)

        p, mask_wp = warper.warp(masks[i], K, cameras[i].R, cv2.INTER_NEAREST,
                                 cv2.BORDER_CONSTANT)
        masks_warped.append(mask_wp.get())

    # convert type
    images_warped_f = []
    for img in images_warped:
        imgf = img.astype(np.float32)
        images_warped_f.append(imgf)

    # blends each type of image and saves them
    for res_name, imgs in data:
        # compensate for exposure -- NOTE it doesn't do this
        # but see https://docs.opencv.org/4.1.0/d2/d37/classcv_1_1detail_1_1ExposureCompensator.html for options
        compensator = cv2.detail.ExposureCompensator_createDefault(
            cv2.detail.ExposureCompensator_NO)
        compensator.feed(corners=corners,
                         images=images_warped,
                         masks=masks_warped)

        # find seams in the images -- NOTE just as with exposure this doesn't actually do anything
        # but there are other possibilities here: https://docs.opencv.org/4.1.0/d7/d09/classcv_1_1detail_1_1SeamFinder.html#aaefc003adf1ebec13867ad9203096f6fa55b2503305e94168c0b36c4531f288d7
        seam_finder = cv2.detail.SeamFinder_createDefault(
            cv2.detail.SeamFinder_NO)
        seam_finder.find(images_warped_f, corners, masks_warped)

        sizes = []
        blender = None
        compose_scale = -1

        for i in range(num_images):
            full_img = imgs[i]

            if compose_scale == -1:  # if it hasn't been set yet
                corners = []
                if compose_megapix > 0:
                    compose_scale = min(
                        1.0,
                        np.sqrt(compose_megapix * 1e6 /
                                (full_img.shape[0] * full_img.shape[1])))
                else:
                    compose_scale = 1
                compose_work_aspect = compose_scale / work_scale
                warped_image_scale *= compose_work_aspect

                warper = cv2.PyRotationWarper(warp_type, warped_image_scale)
                for c in range(len(data[0][1])):
                    cameras[c].focal *= compose_work_aspect
                    cameras[c].ppx *= compose_work_aspect
                    cameras[c].ppy *= compose_work_aspect

                    sz = (data[0][1][c].shape[1] * compose_scale,
                          data[0][1][c].shape[0] * compose_scale)
                    K = cameras[c].K().astype(np.float32)
                    roi = warper.warpRoi(sz, K, cameras[c].R)
                    corners.append(roi[0:2])
                    sizes.append(roi[2:4])

            if abs(compose_scale - 1) > 1e-1:
                img = cv2.resize(src=full_img,
                                 dsize=None,
                                 fx=compose_scale,
                                 fy=compose_scale,
                                 interpolation=cv2.INTER_LINEAR_EXACT)
            else:
                img = full_img

            K = cameras[i].K().astype(np.float32)
            corner, image_warped = warper.warp(img, K, cameras[i].R,
                                               cv2.INTER_LINEAR,
                                               cv2.BORDER_REFLECT)
            mask = 255 * np.ones((img.shape[0], img.shape[1]), np.uint8)
            p, mask_warped = warper.warp(mask, K, cameras[i].R,
                                         cv2.INTER_NEAREST,
                                         cv2.BORDER_CONSTANT)
            compensator.apply(i, corners[i], image_warped, mask_warped)
            image_warped_s = image_warped.astype(np.int16)

            dilated_mask = cv2.dilate(masks_warped[i], None)
            seam_mask = cv2.resize(
                dilated_mask, (mask_warped.shape[1], mask_warped.shape[0]), 0,
                0, cv2.INTER_LINEAR_EXACT)
            mask_warped = cv2.bitwise_and(seam_mask, mask_warped)

            # setup blender -- this sets up the part that combines the images by laying them on top of each other
            if blender is None:
                blender = cv2.detail.Blender_createDefault(
                    cv2.detail.Blender_NO)
                dst_sz = cv2.detail.resultRoi(corners=corners, sizes=sizes)
                blend_width = np.sqrt(
                    dst_sz[2] * dst_sz[3]) * blend_strength / 100
                if blend_width < 1:
                    print("no blend")
                    blender = cv2.detail.Blender_createDefault(
                        cv2.detail.Blender_NO)
                elif blend_type == "multiband":  # I think this is generally better
                    print(blend_type)
                    blender = cv2.detail_MultiBandBlender()
                elif blend_type == "feather":  # mixes images at borders
                    print(blend_type)
                    blender = cv2.detail_FeatherBlender()
                    blender.setSharpness(1.0 / blend_width)
                blender.prepare(dst_sz)

            blender.feed(image_warped_s, mask_warped, corners[i])

        result = None
        result_mask = None
        print("blending..." + res_name)
        result, result_mask = blender.blend(result, result_mask)
        print("SIZE:", result.shape)
        cv2.imwrite(res_name, result)
Ejemplo n.º 8
0
            rmats.append(np.copy(cam.R))
        rmats = cv.detail.waveCorrect(rmats, cv.detail.WAVE_CORRECT_HORIZ)
        for idx, cam in enumerate(cameras):
            cam.R = rmats[idx]
    corners = []
    mask = []
    masks_warped = []
    images_warped = []
    sizes = []
    masks = []
    for i in range(0, num_images):
        um = cv.UMat(255 * np.ones(
            (images[i].shape[0], images[i].shape[1]), np.uint8))
        masks.append(um)

    warper = cv.PyRotationWarper(warp_type, warped_image_scale *
                                 seam_work_aspect)  # warper peut etre nullptr?
    for idx in range(0, num_images):
        K = cameras[idx].K().astype(np.float32)
        swa = seam_work_aspect
        K[0, 0] *= swa
        K[0, 2] *= swa
        K[1, 1] *= swa
        K[1, 2] *= swa
        corner, image_wp = warper.warp(images[idx], K, cameras[idx].R,
                                       cv.INTER_LINEAR, cv.BORDER_REFLECT)
        corners.append(corner)
        sizes.append((image_wp.shape[1], image_wp.shape[0]))
        images_warped.append(image_wp)

        p, mask_wp = warper.warp(masks[idx], K, cameras[idx].R,
                                 cv.INTER_NEAREST, cv.BORDER_CONSTANT)
Ejemplo n.º 9
0
 def warp_roi(self, size, camera, aspect=1):
     warper = cv.PyRotationWarper(self.warper_type, self.scale * aspect)
     K = Warper.get_K(camera, aspect)
     return warper.warpRoi(size, K, camera.R)
Ejemplo n.º 10
0
def main():
    args = parser.parse_args()
    img_names = args.img_names
    print(img_names)
    work_megapix = args.work_megapix
    seam_megapix = args.seam_megapix
    compose_megapix = args.compose_megapix
    conf_thresh = args.conf_thresh
    ba_refine_mask = args.ba_refine_mask
    wave_correct = args.wave_correct
    if wave_correct == 'no':
        do_wave_correct = False
    else:
        do_wave_correct = True
    if args.save_graph is None:
        save_graph = False
    else:
        save_graph = True
    warp_type = args.warp
    blend_type = args.blend
    blend_strength = args.blend_strength
    result_name = args.output
    if args.timelapse is not None:
        timelapse = True
        if args.timelapse == "as_is":
            timelapse_type = cv.detail.Timelapser_AS_IS
        elif args.timelapse == "crop":
            timelapse_type = cv.detail.Timelapser_CROP
        else:
            print("Bad timelapse method")
            exit()
    else:
        timelapse = False
    finder = FEATURES_FIND_CHOICES[args.features]()
    seam_work_aspect = 1
    full_img_sizes = []
    features = []
    images = []
    is_work_scale_set = False
    is_seam_scale_set = False
    is_compose_scale_set = False
    for name in img_names:
        full_img = cv.imread(cv.samples.findFile(name))
        if full_img is None:
            print("Cannot read image ", name)
            exit()
        full_img_sizes.append((full_img.shape[1], full_img.shape[0]))
        if work_megapix < 0:
            img = full_img
            work_scale = 1
            is_work_scale_set = True
        else:
            if is_work_scale_set is False:
                work_scale = min(
                    1.0,
                    np.sqrt(work_megapix * 1e6 /
                            (full_img.shape[0] * full_img.shape[1])))
                is_work_scale_set = True
            img = cv.resize(src=full_img,
                            dsize=None,
                            fx=work_scale,
                            fy=work_scale,
                            interpolation=cv.INTER_LINEAR_EXACT)
        if is_seam_scale_set is False:
            seam_scale = min(
                1.0,
                np.sqrt(seam_megapix * 1e6 /
                        (full_img.shape[0] * full_img.shape[1])))
            seam_work_aspect = seam_scale / work_scale
            is_seam_scale_set = True
        img_feat = cv.detail.computeImageFeatures2(finder, img)
        features.append(img_feat)
        img = cv.resize(src=full_img,
                        dsize=None,
                        fx=seam_scale,
                        fy=seam_scale,
                        interpolation=cv.INTER_LINEAR_EXACT)
        images.append(img)

    matcher = get_matcher(args)
    p = matcher.apply2(features)
    matcher.collectGarbage()

    if save_graph:
        with open(args.save_graph, 'w') as fh:
            fh.write(cv.detail.matchesGraphAsString(img_names, p, conf_thresh))

    indices = cv.detail.leaveBiggestComponent(features, p, 0.3)
    img_subset = []
    img_names_subset = []
    full_img_sizes_subset = []
    for i in range(len(indices)):
        img_names_subset.append(img_names[indices[i, 0]])
        img_subset.append(images[indices[i, 0]])
        full_img_sizes_subset.append(full_img_sizes[indices[i, 0]])
    images = img_subset
    img_names = img_names_subset
    full_img_sizes = full_img_sizes_subset
    num_images = len(img_names)
    if num_images < 2:
        print("Need more images")
        exit()

    estimator = ESTIMATOR_CHOICES[args.estimator]()
    b, cameras = estimator.apply(features, p, None)
    if not b:
        print("Homography estimation failed.")
        exit()
    for cam in cameras:
        cam.R = cam.R.astype(np.float32)

    adjuster = BA_COST_CHOICES[args.ba]()
    adjuster.setConfThresh(1)
    refine_mask = np.zeros((3, 3), np.uint8)
    if ba_refine_mask[0] == 'x':
        refine_mask[0, 0] = 1
    if ba_refine_mask[1] == 'x':
        refine_mask[0, 1] = 1
    if ba_refine_mask[2] == 'x':
        refine_mask[0, 2] = 1
    if ba_refine_mask[3] == 'x':
        refine_mask[1, 1] = 1
    if ba_refine_mask[4] == 'x':
        refine_mask[1, 2] = 1
    adjuster.setRefinementMask(refine_mask)
    b, cameras = adjuster.apply(features, p, cameras)
    if not b:
        print("Camera parameters adjusting failed.")
        exit()
    focals = []
    for cam in cameras:
        focals.append(cam.focal)
    focals.sort()
    if len(focals) % 2 == 1:
        warped_image_scale = focals[len(focals) // 2]
    else:
        warped_image_scale = (focals[len(focals) // 2] +
                              focals[len(focals) // 2 - 1]) / 2
    if do_wave_correct:
        rmats = []
        for cam in cameras:
            rmats.append(np.copy(cam.R))
        rmats = cv.detail.waveCorrect(rmats, cv.detail.WAVE_CORRECT_HORIZ)
        for idx, cam in enumerate(cameras):
            cam.R = rmats[idx]
    corners = []
    masks_warped = []
    images_warped = []
    sizes = []
    masks = []
    for i in range(0, num_images):
        um = cv.UMat(255 * np.ones(
            (images[i].shape[0], images[i].shape[1]), np.uint8))
        masks.append(um)

    warper = cv.PyRotationWarper(warp_type, warped_image_scale *
                                 seam_work_aspect)  # warper could be nullptr?
    for idx in range(0, num_images):
        K = cameras[idx].K().astype(np.float32)
        swa = seam_work_aspect
        K[0, 0] *= swa
        K[0, 2] *= swa
        K[1, 1] *= swa
        K[1, 2] *= swa
        corner, image_wp = warper.warp(images[idx], K, cameras[idx].R,
                                       cv.INTER_LINEAR, cv.BORDER_REFLECT)
        corners.append(corner)
        sizes.append((image_wp.shape[1], image_wp.shape[0]))
        images_warped.append(image_wp)
        p, mask_wp = warper.warp(masks[idx], K, cameras[idx].R,
                                 cv.INTER_NEAREST, cv.BORDER_CONSTANT)
        masks_warped.append(mask_wp.get())

    images_warped_f = []
    for img in images_warped:
        imgf = img.astype(np.float32)
        images_warped_f.append(imgf)

    compensator = get_compensator(args)
    compensator.feed(corners=corners, images=images_warped, masks=masks_warped)

    seam_finder = SEAM_FIND_CHOICES[args.seam]
    seam_finder.find(images_warped_f, corners, masks_warped)
    compose_scale = 1
    corners = []
    sizes = []
    blender = None
    timelapser = None
    # https://github.com/opencv/opencv/blob/master/samples/cpp/stitching_detailed.cpp#L725 ?
    for idx, name in enumerate(img_names):
        full_img = cv.imread(name)
        if not is_compose_scale_set:
            if compose_megapix > 0:
                compose_scale = min(
                    1.0,
                    np.sqrt(compose_megapix * 1e6 /
                            (full_img.shape[0] * full_img.shape[1])))
            is_compose_scale_set = True
            compose_work_aspect = compose_scale / work_scale
            warped_image_scale *= compose_work_aspect
            warper = cv.PyRotationWarper(warp_type, warped_image_scale)
            for i in range(0, len(img_names)):
                cameras[i].focal *= compose_work_aspect
                cameras[i].ppx *= compose_work_aspect
                cameras[i].ppy *= compose_work_aspect
                sz = (full_img_sizes[i][0] * compose_scale,
                      full_img_sizes[i][1] * compose_scale)
                K = cameras[i].K().astype(np.float32)
                roi = warper.warpRoi(sz, K, cameras[i].R)
                corners.append(roi[0:2])
                sizes.append(roi[2:4])
        if abs(compose_scale - 1) > 1e-1:
            img = cv.resize(src=full_img,
                            dsize=None,
                            fx=compose_scale,
                            fy=compose_scale,
                            interpolation=cv.INTER_LINEAR_EXACT)
        else:
            img = full_img
        _img_size = (img.shape[1], img.shape[0])
        K = cameras[idx].K().astype(np.float32)
        corner, image_warped = warper.warp(img, K, cameras[idx].R,
                                           cv.INTER_LINEAR, cv.BORDER_REFLECT)
        mask = 255 * np.ones((img.shape[0], img.shape[1]), np.uint8)
        p, mask_warped = warper.warp(mask, K, cameras[idx].R, cv.INTER_NEAREST,
                                     cv.BORDER_CONSTANT)
        compensator.apply(idx, corners[idx], image_warped, mask_warped)
        image_warped_s = image_warped.astype(np.int16)
        dilated_mask = cv.dilate(masks_warped[idx], None)
        seam_mask = cv.resize(dilated_mask,
                              (mask_warped.shape[1], mask_warped.shape[0]), 0,
                              0, cv.INTER_LINEAR_EXACT)
        mask_warped = cv.bitwise_and(seam_mask, mask_warped)
        if blender is None and not timelapse:
            blender = cv.detail.Blender_createDefault(cv.detail.Blender_NO)
            dst_sz = cv.detail.resultRoi(corners=corners, sizes=sizes)
            blend_width = np.sqrt(dst_sz[2] * dst_sz[3]) * blend_strength / 100
            if blend_width < 1:
                blender = cv.detail.Blender_createDefault(cv.detail.Blender_NO)
            elif blend_type == "multiband":
                blender = cv.detail_MultiBandBlender()
                blender.setNumBands(
                    (np.log(blend_width) / np.log(2.) - 1.).astype(np.int))
            elif blend_type == "feather":
                blender = cv.detail_FeatherBlender()
                blender.setSharpness(1. / blend_width)
            blender.prepare(dst_sz)
        elif timelapser is None and timelapse:
            timelapser = cv.detail.Timelapser_createDefault(timelapse_type)
            timelapser.initialize(corners, sizes)
        if timelapse:
            ma_tones = np.ones(
                (image_warped_s.shape[0], image_warped_s.shape[1]), np.uint8)
            timelapser.process(image_warped_s, ma_tones, corners[idx])
            pos_s = img_names[idx].rfind("/")
            if pos_s == -1:
                fixed_file_name = "fixed_" + img_names[idx]
            else:
                fixed_file_name = img_names[
                    idx][:pos_s + 1] + "fixed_" + img_names[idx][pos_s + 1:]
            cv.imwrite(fixed_file_name, timelapser.getDst())
        else:
            blender.feed(cv.UMat(image_warped_s), mask_warped, corners[idx])
    if not timelapse:
        result = None
        result_mask = None
        result, result_mask = blender.blend(result, result_mask)
        cv.imwrite(result_name, result)
        zoom_x = 600.0 / result.shape[1]
        dst = cv.normalize(src=result,
                           dst=None,
                           alpha=255.,
                           norm_type=cv.NORM_MINMAX,
                           dtype=cv.CV_8U)
        dst = cv.resize(dst, dsize=None, fx=zoom_x, fy=zoom_x)
        cv.imshow(result_name, dst)
        cv.waitKey()

    print("Done")
Ejemplo n.º 11
0
            continue
        i = i + 1

        if not ret:  # 제대로 못읽었을 경우
            break
        else:  # 제대로 읽었을 경우 그레이로 바꾸기
            j += 1

            if frame.shape[1] > 720:
                frame = imutils.resize(frame, width=720)

            tm.reset()
            tm.start()
            t1 = time.time()

            warper = cv2.PyRotationWarper('spherical', float(f))
            corner, frame = warper.warp(frame, K, R, cv2.INTER_LINEAR, cv2.BORDER_CONSTANT)

            tm.stop()
            ms = tm.getTimeSec()  # 밀리 초 단위 시간을 받아옴
            meanTime1 += ms
            print('spherical warp mean time : {}s.'.format(meanTime1 / j))

            frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

            tm.reset()
            tm.start()
            t1 = time.time()
            #detection
            frame = object_detector.detectObject(frame)
            tm.stop()
Ejemplo n.º 12
0
 def _getWarper(self):
     self.warper = cv.PyRotationWarper(
         self.warp_type, self.warped_image_scale * self.seam_work_aspect)
def init_panorama(liste_images, pano):
    nb_images = len(liste_images)
    if nb_images < 2:
        print("Il faut au moins 2 images")
        return False, pano
    algo_descripteur = cv.ORB.create(pano.nb_point_cle)
    descripteurs = cv.detail.computeImageFeatures(algo_descripteur,
                                                  liste_images)
    if pano.matcher_type == "affine":
        algo_apparier = cv.detail_AffineBestOf2NearestMatcher(
            True, pano.try_cuda, pano.seuil_appariement)
    else:
        algo_apparier = cv.detail_AffineBestOf2NearestMatcher(
            False, pano.try_cuda, pano.seuil_appariement)
    appariement_image = algo_apparier.apply2(descripteurs)
    pano.indices = cv.detail.leaveBiggestComponent(descripteurs,
                                                   appariement_image,
                                                   pano.seuil_confiance)
    nb_images = len(pano.indices)
    if nb_images < 2:
        print("Echec de l'appariement")
        return False, pano
    pano.remise_a_zero()
    if pano.type_estimateur == "affine":
        estimateur = cv.detail_AffineBasedEstimator()
    else:
        estimateur = cv.detail_HomographyBasedEstimator()
    ret, pano.cameras = estimateur.apply(descripteurs, appariement_image, None)
    if not ret:
        print("Echec de l'estimation des modeles.")
        return False, pano
    for cam in pano.cameras:
        cam.R = cam.R.astype(np.float32)
    if pano.fct_cout not in pano.cout:
        print("Fonction de cout inconnue: ", pano.fct_cout)
        return False, pano
    ajuster = pano.cout[pano.fct_cout]()
    ajuster.setConfThresh(pano.seuil_confiance)
    refine_mask = np.ones((3, 3), np.uint8)
    ajuster.setRefinementMask(refine_mask)

    ret, pano.cameras = ajuster.apply(descripteurs, appariement_image,
                                      pano.cameras)
    if not ret:
        print("Echec de l'ajustement des parametres.")
        return False, pano
    for cam in pano.cameras:
        pano.focales.append(cam.focal)
    sorted(pano.focales)

    if len(pano.focales) % 2 == 1:
        pano.focale_moyenne = pano.focales[len(pano.focales) // 2]
    else:
        pano.focale_moyenne = (pano.focales[len(pano.focales) // 2] +
                               pano.focales[len(pano.focales) // 2 - 1]) / 2
    images_projetees = []
    images_projetees_float = []

    pano.composition = cv.PyRotationWarper(pano.surface_compo,
                                           pano.focale_moyenne)
    for i in range(nb_images):
        idx = pano.indices[i][0]
        cam_int = pano.cameras[idx].K().astype(np.float32)
        coins, image_wp = pano.composition.warp(liste_images[idx], cam_int,
                                                pano.cameras[idx].R,
                                                cv.INTER_LINEAR,
                                                cv.BORDER_REFLECT)
        pano.liste_pos_coin.append(coins)
        pano.liste_taille_masque.append((image_wp.shape[1], image_wp.shape[0]))
        images_projetees.append(image_wp)
        umat = cv.UMat(255 * np.ones(
            (liste_images[pano.indices[i][0]].shape[0],
             liste_images[pano.indices[i][0]].shape[1]), np.uint8))

        _, mask_wp = pano.composition.warp(umat, cam_int, pano.cameras[idx].R,
                                           cv.INTER_NEAREST,
                                           cv.BORDER_CONSTANT)
        pano.liste_masque_compo.append(mask_wp)

    for img in images_projetees:
        imgf = img.astype(np.float32)
        images_projetees_float.append(imgf)
    pano.algo_correct_expo = cv.detail.ExposureCompensator_createDefault(
        pano.correction_exposition)
    pano.algo_correct_expo.feed(pano.liste_pos_coin, images_projetees,
                                pano.liste_masque_compo)

    if pano.fct_couture not in pano.liste_couture:
        print("type de couture inconnue :", pano.fct_couture)
        return False, pano
    pano.couture[pano.fct_couture].find(images_projetees_float,
                                        pano.liste_pos_coin,
                                        pano.liste_masque_compo)
    if len(pano.indices) == len(liste_images):
        sauver_configuration(pano)
    return True, pano
Ejemplo n.º 14
0
    for idx, cam in enumerate(cameras):
        cam.R = rmats[idx]

corners = []
mask = []
masks_warped = []
images_warped = []
sizes = []
masks = []

for i in range(0, num_images):
    um = cv2.UMat(255 * np.ones(
        (images[i].shape[0], images[i].shape[1]), np.uint8))
    masks.append(um)

warper = cv2.PyRotationWarper(warp_type, warped_image_scale * seam_work_aspect)

for idx in range(0, num_images):
    K = cameras[idx].K().astype(np.float32)
    swa = seam_work_aspect
    K[0, 0] *= swa
    K[0, 2] *= swa
    K[1, 1] *= swa
    K[1, 2] *= swa
    corner, image_wp = warper.warp(images[idx], K, cameras[idx].R,
                                   cv2.INTER_LINEAR, cv2.BORDER_REFLECT)
    corners.append(corner)
    sizes.append((image_wp.shape[1], image_wp.shape[0]))
    images_warped.append(image_wp)

    p, mask_wp = warper.warp(masks[idx], K, cameras[idx].R, cv2.INTER_NEAREST,
Ejemplo n.º 15
0
    #                              cv2.INTER_NEAREST, cv2.BORDER_CONSTANT)

    #     # masks_warped.append(mask_wp.get())

    # images_warped_f = [img.astype(np.float32) for im in images_warped]

    # compensator = cv2.detail.ExposureCompensator_createDefault(
    #     cv2.detail.ExposureCompensator_NO)
    # compensator.feed(corners=corners, images=images_warped, masks=masks_warped)

    # seam_finder = cv2.detail.SeamFinder_createDefault(cv2.detail.SeamFinder_NO)
    # seam_finder.find(images_warped_f, corners, masks_warped)

    stitch_sizes, stitch_corners = [], []

    warper = cv2.PyRotationWarper('plane', warped_image_scale)
    for i, img in enumerate(conn_images):
        sz = img.shape[1], img.shape[0]
        K = cameras[i].K().astype(np.float32)
        roi = warper.warpRoi(sz, K, cameras[i].R)
        stitch_corners.append(roi[0:2])
        stitch_sizes.append(roi[2:4])

    canvas_size = cv2.detail.resultRoi(corners=stitch_corners,
                                       sizes=stitch_sizes)

    blend_width = np.sqrt(canvas_size[2] * canvas_size[3]) * 5 / 100
    if blend_width < 1:
        blender = cv2.detail.Blender_createDefault(cv2.detail.Blender_NO)
    else:
        blender = cv2.detail_MultiBandBlender()
Ejemplo n.º 16
0
def main():
    args = parser.parse_args()
    img_names=args.img_names
    print(img_names)
    preview = args.preview
    try_cuda = args.try_cuda
    work_megapix = args.work_megapix
    seam_megapix = args.seam_megapix
    compose_megapix = args.compose_megapix
    conf_thresh = args.conf_thresh
    features_type = args.features
    matcher_type = args.matcher
    estimator_type = args.estimator
    ba_cost_func = args.ba
    ba_refine_mask = args.ba_refine_mask
    wave_correct = args.wave_correct
    if wave_correct=='no':
        do_wave_correct= False
    else:
        do_wave_correct=True
    if args.save_graph is None:
        save_graph = False
    else:
        save_graph =True
        save_graph_to = args.save_graph
    warp_type = args.warp
    if args.expos_comp=='no':
        expos_comp_type = cv.detail.ExposureCompensator_NO
    elif  args.expos_comp=='gain':
        expos_comp_type = cv.detail.ExposureCompensator_GAIN
    elif  args.expos_comp=='gain_blocks':
        expos_comp_type = cv.detail.ExposureCompensator_GAIN_BLOCKS
    elif  args.expos_comp=='channel':
        expos_comp_type = cv.detail.ExposureCompensator_CHANNELS
    elif  args.expos_comp=='channel_blocks':
        expos_comp_type = cv.detail.ExposureCompensator_CHANNELS_BLOCKS
    else:
        print("Bad exposure compensation method")
        exit()
    expos_comp_nr_feeds = args.expos_comp_nr_feeds
    expos_comp_nr_filtering = args.expos_comp_nr_filtering
    expos_comp_block_size = args.expos_comp_block_size
    match_conf = args.match_conf
    seam_find_type = args.seam
    blend_type = args.blend
    blend_strength = args.blend_strength
    result_name = args.output
    if args.timelapse is not None:
        timelapse = True
        if args.timelapse=="as_is":
            timelapse_type = cv.detail.Timelapser_AS_IS
        elif args.timelapse=="crop":
            timelapse_type = cv.detail.Timelapser_CROP
        else:
            print("Bad timelapse method")
            exit()
    else:
        timelapse= False
    range_width = args.rangewidth
    if features_type=='orb':
        finder= cv.ORB.create()
    elif features_type=='surf':
        finder= cv.xfeatures2d_SURF.create()
    elif features_type=='sift':
        finder= cv.xfeatures2d_SIFT.create()
    else:
        print ("Unknown descriptor type")
        exit()
    seam_work_aspect = 1
    full_img_sizes=[]
    features=[]
    images=[]
    is_work_scale_set = False
    is_seam_scale_set = False
    is_compose_scale_set = False;
    for name in img_names:
        full_img = cv.imread(cv.samples.findFile(name))
        if full_img is None:
            print("Cannot read image ", name)
            exit()
        full_img_sizes.append((full_img.shape[1],full_img.shape[0]))
        if work_megapix < 0:
            img = full_img
            work_scale = 1
            is_work_scale_set = True
        else:
            if is_work_scale_set is False:
                work_scale = min(1.0, np.sqrt(work_megapix * 1e6 / (full_img.shape[0]*full_img.shape[1])))
                is_work_scale_set = True
            img = cv.resize(src=full_img, dsize=None, fx=work_scale, fy=work_scale, interpolation=cv.INTER_LINEAR_EXACT)
        if is_seam_scale_set is False:
            seam_scale = min(1.0, np.sqrt(seam_megapix * 1e6 / (full_img.shape[0]*full_img.shape[1])))
            seam_work_aspect = seam_scale / work_scale
            is_seam_scale_set = True
        imgFea= cv.detail.computeImageFeatures2(finder,img)
        features.append(imgFea)
        img = cv.resize(src=full_img, dsize=None, fx=seam_scale, fy=seam_scale, interpolation=cv.INTER_LINEAR_EXACT)
        images.append(img)
    if matcher_type== "affine":
        matcher = cv.detail_AffineBestOf2NearestMatcher(False, try_cuda, match_conf)
    elif range_width==-1:
        matcher = cv.detail.BestOf2NearestMatcher_create(try_cuda, match_conf)
    else:
        matcher = cv.detail.BestOf2NearestRangeMatcher_create(range_width, try_cuda, match_conf)
    p=matcher.apply2(features)
    matcher.collectGarbage()
    if save_graph:
        f = open(save_graph_to,"w")
        f.write(cv.detail.matchesGraphAsString(img_names, p, conf_thresh))
        f.close()
    indices=cv.detail.leaveBiggestComponent(features,p,0.3)
    img_subset =[]
    img_names_subset=[]
    full_img_sizes_subset=[]
    num_images=len(indices)
    for i in range(len(indices)):
        img_names_subset.append(img_names[indices[i,0]])
        img_subset.append(images[indices[i,0]])
        full_img_sizes_subset.append(full_img_sizes[indices[i,0]])
    images = img_subset;
    img_names = img_names_subset;
    full_img_sizes = full_img_sizes_subset;
    num_images = len(img_names)
    if num_images < 2:
        print("Need more images")
        exit()

    if estimator_type == "affine":
        estimator = cv.detail_AffineBasedEstimator()
    else:
        estimator = cv.detail_HomographyBasedEstimator()
    b, cameras =estimator.apply(features,p,None)
    if not b:
        print("Homography estimation failed.")
        exit()
    for cam in cameras:
        cam.R=cam.R.astype(np.float32)

    if ba_cost_func == "reproj":
        adjuster = cv.detail_BundleAdjusterReproj()
    elif ba_cost_func == "ray":
        adjuster = cv.detail_BundleAdjusterRay()
    elif ba_cost_func == "affine":
        adjuster = cv.detail_BundleAdjusterAffinePartial()
    elif ba_cost_func == "no":
        adjuster = cv.detail_NoBundleAdjuster()
    else:
        print( "Unknown bundle adjustment cost function: ", ba_cost_func )
        exit()
    adjuster.setConfThresh(1)
    refine_mask=np.zeros((3,3),np.uint8)
    if ba_refine_mask[0] == 'x':
        refine_mask[0,0] = 1
    if ba_refine_mask[1] == 'x':
        refine_mask[0,1] = 1
    if ba_refine_mask[2] == 'x':
        refine_mask[0,2] = 1
    if ba_refine_mask[3] == 'x':
        refine_mask[1,1] = 1
    if ba_refine_mask[4] == 'x':
        refine_mask[1,2] = 1
    adjuster.setRefinementMask(refine_mask)
    b,cameras = adjuster.apply(features,p,cameras)
    if not b:
        print("Camera parameters adjusting failed.")
        exit()
    focals=[]
    for cam in cameras:
        focals.append(cam.focal)
    sorted(focals)
    if len(focals)%2==1:
        warped_image_scale = focals[len(focals) // 2]
    else:
        warped_image_scale = (focals[len(focals) // 2]+focals[len(focals) // 2-1])/2
    if do_wave_correct:
        rmats=[]
        for cam in cameras:
            rmats.append(np.copy(cam.R))
        rmats	=	cv.detail.waveCorrect(	rmats,  cv.detail.WAVE_CORRECT_HORIZ)
        for idx,cam in enumerate(cameras):
            cam.R = rmats[idx]
    corners=[]
    mask=[]
    masks_warped=[]
    images_warped=[]
    sizes=[]
    masks=[]
    for i in range(0,num_images):
        um=cv.UMat(255*np.ones((images[i].shape[0],images[i].shape[1]),np.uint8))
        masks.append(um)

    warper = cv.PyRotationWarper(warp_type,warped_image_scale*seam_work_aspect) # warper peut etre nullptr?
    for idx in range(0,num_images):
        K = cameras[idx].K().astype(np.float32)
        swa = seam_work_aspect
        K[0,0] *= swa
        K[0,2] *= swa
        K[1,1] *= swa
        K[1,2] *= swa
        corner,image_wp =warper.warp(images[idx],K,cameras[idx].R,cv.INTER_LINEAR, cv.BORDER_REFLECT)
        corners.append(corner)
        sizes.append((image_wp.shape[1],image_wp.shape[0]))
        images_warped.append(image_wp)

        p,mask_wp =warper.warp(masks[idx],K,cameras[idx].R,cv.INTER_NEAREST, cv.BORDER_CONSTANT)
        masks_warped.append(mask_wp.get())
    images_warped_f=[]
    for img in images_warped:
        imgf=img.astype(np.float32)
        images_warped_f.append(imgf)
    if cv.detail.ExposureCompensator_CHANNELS == expos_comp_type:
        compensator = cv.detail_ChannelsCompensator(expos_comp_nr_feeds)
    #    compensator.setNrGainsFilteringIterations(expos_comp_nr_filtering)
    elif cv.detail.ExposureCompensator_CHANNELS_BLOCKS == expos_comp_type:
        compensator=cv.detail_BlocksChannelsCompensator(expos_comp_block_size, expos_comp_block_size,expos_comp_nr_feeds)
    #    compensator.setNrGainsFilteringIterations(expos_comp_nr_filtering)
    else:
        compensator=cv.detail.ExposureCompensator_createDefault(expos_comp_type)
    compensator.feed(corners=corners, images=images_warped, masks=masks_warped)
    if seam_find_type == "no":
        seam_finder = cv.detail.SeamFinder_createDefault(cv.detail.SeamFinder_NO)
    elif seam_find_type == "voronoi":
        seam_finder = cv.detail.SeamFinder_createDefault(cv.detail.SeamFinder_VORONOI_SEAM);
    elif seam_find_type == "gc_color":
        seam_finder = cv.detail_GraphCutSeamFinder("COST_COLOR")
    elif seam_find_type == "gc_colorgrad":
        seam_finder = cv.detail_GraphCutSeamFinder("COST_COLOR_GRAD")
    elif seam_find_type == "dp_color":
        seam_finder = cv.detail_DpSeamFinder("COLOR")
    elif seam_find_type == "dp_colorgrad":
        seam_finder = cv.detail_DpSeamFinder("COLOR_GRAD")
    if seam_finder is None:
        print("Can't create the following seam finder ",seam_find_type)
        exit()
    seam_finder.find(images_warped_f, corners,masks_warped )
    imgListe=[]
    compose_scale=1
    corners=[]
    sizes=[]
    images_warped=[]
    images_warped_f=[]
    masks=[]
    blender= None
    timelapser=None
    compose_work_aspect=1
    for idx,name in enumerate(img_names): # https://github.com/opencv/opencv/blob/master/samples/cpp/stitching_detailed.cpp#L725 ?
        full_img  = cv.imread(name)
        if not is_compose_scale_set:
            if compose_megapix > 0:
                compose_scale = min(1.0, np.sqrt(compose_megapix * 1e6 / (full_img.shape[0]*full_img.shape[1])))
            is_compose_scale_set = True;
            compose_work_aspect = compose_scale / work_scale;
            warped_image_scale *= compose_work_aspect
            warper =  cv.PyRotationWarper(warp_type,warped_image_scale)
            for i in range(0,len(img_names)):
                cameras[i].focal *= compose_work_aspect
                cameras[i].ppx *= compose_work_aspect
                cameras[i].ppy *= compose_work_aspect
                sz = (full_img_sizes[i][0] * compose_scale,full_img_sizes[i][1]* compose_scale)
                K = cameras[i].K().astype(np.float32)
                roi = warper.warpRoi(sz, K, cameras[i].R);
                corners.append(roi[0:2])
                sizes.append(roi[2:4])
        if abs(compose_scale - 1) > 1e-1:
            img =cv.resize(src=full_img, dsize=None, fx=compose_scale, fy=compose_scale, interpolation=cv.INTER_LINEAR_EXACT)
        else:
            img = full_img;
        img_size = (img.shape[1],img.shape[0]);
        K=cameras[idx].K().astype(np.float32)
        corner,image_warped =warper.warp(img,K,cameras[idx].R,cv.INTER_LINEAR, cv.BORDER_REFLECT)
        mask =255*np.ones((img.shape[0],img.shape[1]),np.uint8)
        p,mask_warped =warper.warp(mask,K,cameras[idx].R,cv.INTER_NEAREST, cv.BORDER_CONSTANT)
        compensator.apply(idx,corners[idx],image_warped,mask_warped)
        image_warped_s = image_warped.astype(np.int16)
        image_warped=[]
        dilated_mask = cv.dilate(masks_warped[idx],None)
        seam_mask = cv.resize(dilated_mask,(mask_warped.shape[1],mask_warped.shape[0]),0,0,cv.INTER_LINEAR_EXACT)
        mask_warped = cv.bitwise_and(seam_mask,mask_warped)
        if blender==None and not timelapse:
            blender = cv.detail.Blender_createDefault(cv.detail.Blender_NO)
            dst_sz = cv.detail.resultRoi(corners=corners,sizes=sizes)
            blend_width = np.sqrt(dst_sz[2]*dst_sz[3]) * blend_strength / 100
            if blend_width < 1:
                blender = cv.detail.Blender_createDefault(cv.detail.Blender_NO)
            elif blend_type == "multiband":
                blender = cv.detail_MultiBandBlender()
                blender.setNumBands((np.log(blend_width)/np.log(2.) - 1.).astype(np.int))
            elif blend_type == "feather":
                blender = cv.detail_FeatherBlender()
                blender.setSharpness(1./blend_width)
            blender.prepare(dst_sz)
        elif timelapser==None  and timelapse:
            timelapser = cv.detail.Timelapser_createDefault(timelapse_type)
            timelapser.initialize(corners, sizes)
        if timelapse:
            matones=np.ones((image_warped_s.shape[0],image_warped_s.shape[1]), np.uint8)
            timelapser.process(image_warped_s, matones, corners[idx])
            pos_s = img_names[idx].rfind("/");
            if pos_s == -1:
                fixedFileName = "fixed_" + img_names[idx];
            else:
                fixedFileName = img_names[idx][:pos_s + 1 ]+"fixed_" + img_names[idx][pos_s + 1: ]
            cv.imwrite(fixedFileName, timelapser.getDst())
        else:
            blender.feed(cv.UMat(image_warped_s), mask_warped, corners[idx])
    if not timelapse:
        result=None
        result_mask=None
        result,result_mask = blender.blend(result,result_mask)
        cv.imwrite(result_name,result)
        zoomx = 600.0 / result.shape[1]
        dst=cv.normalize(src=result,dst=None,alpha=255.,norm_type=cv.NORM_MINMAX,dtype=cv.CV_8U)
        dst=cv.resize(dst,dsize=None,fx=zoomx,fy=zoomx)
        cv.imshow(result_name,dst)
        cv.waitKey()

    print('Done')
Ejemplo n.º 17
0
 def warp_image(self, img, camera, aspect=1):
     warper = cv.PyRotationWarper(self.warper_type, self.scale * aspect)
     _, warped_image = warper.warp(img, Warper.get_K(camera,
                                                     aspect), camera.R,
                                   cv.INTER_LINEAR, cv.BORDER_REFLECT)
     return warped_image
Ejemplo n.º 18
0
def main():
    img_names = [r'C:\Scratch\IPA_Data\FullRes\a0_nor.tif',
                 r'C:\Scratch\IPA_Data\FullRes\a1_nor.tif',
                 r'C:\Scratch\IPA_Data\FullRes\a2_nor.tif',
                 r'C:\Scratch\IPA_Data\FullRes\a3_nor.tif',
                 r'C:\Scratch\IPA_Data\FullRes\a4_nor.tif',
                 r'C:\Scratch\IPA_Data\FullRes\a5_nor.tif',
                 r'C:\Scratch\IPA_Data\FullRes\a6_nor.tif',
                 r'C:\Scratch\IPA_Data\FullRes\a7_nor.tif',
                 r'C:\Scratch\IPA_Data\FullRes\a8_nor.tif',
                 r'C:\Scratch\IPA_Data\FullRes\a9_nor.tif']
                 # r'C:\Scratch\IPA_Data\FullRes\b0_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\b1_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\b2_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\b3_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\b4_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\b5_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\b6_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\b7_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\b8_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\b9_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\c0_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\c1_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\c2_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\c3_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\c4_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\c5_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\c6_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\c7_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\c8_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\c9_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\d0_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\d1_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\d2_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\d3_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\d4_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\d5_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\d6_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\d7_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\d8_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\d9_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\e0_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\e1_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\e2_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\e3_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\e4_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\e5_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\e6_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\e7_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\e8_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\e9_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\f0_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\f1_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\f2_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\f3_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\f4_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\f5_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\f6_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\f7_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\f8_nor.tif',
                 # r'C:\Scratch\IPA_Data\FullRes\f9_nor.tif']
    print(img_names)

    # ================ DEFINE ALL PARAMETERS ================
    # Flags
    try_cuda = False
    work_megapix = 8
    features_type = "surf"
    matcher_type = "affine"
    estimator_type = "affine"
    match_conf = 0.75
    conf_thresh = 1.0

    ba_cost_func = "affine"
    ba_refine_mask = "xxxxx"
    wave_correct = "vert"
    save_graph_var = None

    # Compositing Flags
    warp_type = "affine"
    seam_megapix = 2.0
    seam_find_type = "gc_color"
    compose_megapix = -1
    expos_comp = "no"
    expos_comp_nr_feeds = 1
    # expos_comp_nr_filtering = 2
    expos_comp_block_size = 32
    blend_type = "multiband"
    blend_strength = 5
    result_name = "test_result_3.png"
    timelapse_name = None
    range_width = 8

    # Check if there is to be wave correction, then set the boolean check value
    if wave_correct == 'no':
        do_wave_correct = False
    else:
        do_wave_correct = True

    # Check if there is to be a graph file created
    if save_graph_var is None:
        save_graph = False
    else:
        save_graph = True
        save_graph_to = save_graph_var

    # Check if the exposure is to be compensated, if so define which compensator to use
    if expos_comp == 'no':
        expos_comp_type = cv.detail.ExposureCompensator_NO
    elif expos_comp == 'gain':
        expos_comp_type = cv.detail.ExposureCompensator_GAIN
    elif expos_comp == 'gain_blocks':
        expos_comp_type = cv.detail.ExposureCompensator_GAIN_BLOCKS
    elif expos_comp == 'channel':
        expos_comp_type = cv.detail.ExposureCompensator_CHANNELS
    elif expos_comp == 'channel_blocks':
        expos_comp_type = cv.detail.ExposureCompensator_CHANNELS_BLOCKS
    else:
        print("Bad exposure compensation method")
        exit()

    # Check if the timelapse is to be output. AKA the intermediate layers
    if timelapse_name is not None:
        timelapse = True
        if timelapse_name == "as_is":
            timelapse_type = cv.detail.Timelapser_AS_IS
        elif timelapse_name == "crop":
            timelapse_type = cv.detail.Timelapser_CROP
        else:
            print("Bad timelapse method")
            exit()
    else:
        timelapse = False

    # Check the feature type to be used and create the finder class that will be used
    # TODO - See if there other feature detectors which are more suitable
    if features_type == 'orb':
        finder = cv.ORB.create(500, 1.1, 8, 50, 0, 2, 0, 50, 20)
    elif features_type == 'surf':
        finder = cv.xfeatures2d_SURF.create(100, 8, 4, False, False)
    elif features_type == 'sift':
        finder = cv.xfeatures2d_SIFT.create()
    else:
        print("Unknown descriptor type")
        exit()

    # Pre-allocate other variables to work with
    seam_work_aspect = 1    # Seam aspect ratio
    full_img_sizes = []     # Size of full images
    features = []           # Array for storing features
    images = []             # Array for storing information about images

    is_work_scale_set = False       # Bool for working image scaling
    is_seam_scale_set = False       # Bool for seams scaling
    is_compose_scale_set = False    # Bool for composition image scaling

    # Iterate through the image names
    for name in img_names:
        # Reads the image into a numpy array
        full_img = cv.imread(cv.samples.findFile(name))

        # Check if the file could be read successfully
        if full_img is None:
            print("Cannot read image ", name)
            exit()

        # Add image size to the list ...
        # TODO Could change this to be constant...
        full_img_sizes.append((full_img.shape[1], full_img.shape[0]))

        # Define the working scale the images should be used based on the number of megapixel entered
        if work_megapix < 0:
            # If a negative value entered, use its true scale
            img = full_img
            work_scale = 1
            is_work_scale_set = True
        else:
            if is_work_scale_set is False:
                work_scale = min(1.0, np.sqrt(work_megapix * 1e6 / (full_img.shape[0]*full_img.shape[1])))
                is_work_scale_set = True
            img = cv.resize(src=full_img, dsize=None, fx=work_scale, fy=work_scale, interpolation=cv.INTER_LINEAR_EXACT)

        # Define the scale for the seams that they will be processed
        if is_seam_scale_set is False:
            seam_scale = min(1.0, np.sqrt(seam_megapix * 1e6 / (full_img.shape[0]*full_img.shape[1])))
            seam_work_aspect = seam_scale / work_scale
            is_seam_scale_set = True

        # Get the image features for this image
        img_fea = cv.detail.computeImageFeatures2(finder, img)

        test_feat_img = cv.drawKeypoints(img, img_fea.getKeypoints(), None, (255, 0, 0), 4)
        cv.namedWindow('image', cv.WINDOW_NORMAL)
        cv.imshow('image', test_feat_img)
        cv.resizeWindow('image', int(full_img_sizes[0][0] / 10), int(full_img_sizes[0][1] / 10))
        cv.waitKey()

        features.append(img_fea)
        img = cv.resize(src=full_img, dsize=None, fx=seam_scale, fy=seam_scale, interpolation=cv.INTER_LINEAR_EXACT)
        images.append(img)

    # Define the matcher type to be used for the features
    if matcher_type == "affine":
        matcher = cv.detail_AffineBestOf2NearestMatcher(False, try_cuda, match_conf)
    elif range_width == -1:
        matcher = cv.detail_BestOf2NearestMatcher(try_cuda, match_conf)
    else:
        matcher = cv.detail_BestOf2NearestRangeMatcher(range_width, try_cuda, match_conf)

    # Apply the matcher to the features, obtaining matches between them
    p = matcher.apply2(features)
    # Frees unused memory
    matcher.collectGarbage()

    # Save the graph if chosen
    if save_graph:
        f = open(save_graph_to, "w")
        f.write(cv.detail.matchesGraphAsString(img_names, p, conf_thresh))
        f.close()

    # Remove matches if not above a confidence threshold
    indices = cv.detail.leaveBiggestComponent(features, p, match_conf)

    # Pre-allocate
    img_subset = []                 # Array to hold subset of images numpy arrays?
    img_names_subset = []           # Array to list the names of subset images
    full_img_sizes_subset = []      # Sizes of the images in their full resolution within the subset
    num_images = len(indices)       # Number of images as determined by the thresholding of the feature matches

    # TODO this appears to be the issue running into before... the matching beforehand is producing 0 results
    # Itearte through the images that were matched and get lists of matches/images
    for i in range(num_images):
        img_names_subset.append(img_names[indices[i, 0]])               # Append the names
        img_subset.append(images[indices[i, 0]])                        # Append the actual image arrays
        full_img_sizes_subset.append(full_img_sizes[indices[i, 0]])     # Append their sizes

    # Update the list of images and image names
    images = img_subset
    img_names = img_names_subset
    full_img_sizes = full_img_sizes_subset

    # Get new number of matched images (shouldn't change with the mosaicing project
    num_images = len(img_names)

    # Do a simple test to check if sufficient images
    if num_images < 2:
        print("Need more images")
        exit()

    # Generate the estimator based on what was set to determine approximate relative orientation parameters
    if estimator_type == "affine":
        estimator = cv.detail_AffineBasedEstimator()
    else:
        estimator = cv.detail_HomographyBasedEstimator()
    b, cameras = estimator.apply(features, p, None)

    # Check if estimation passed based on the boolean 'b'
    if not b:
        print("Homography estimation failed.")
        exit()

    # Iterate through the camera orientations computed
    for cam in cameras:
        # Convert the camera rotation matrix to float 32
        cam.R = cam.R.astype(np.float32)

    # TODO read up on the documentation here
    # Define bundle adjustment cost function
    if ba_cost_func == "reproj":
        adjuster = cv.detail_BundleAdjusterReproj()
    elif ba_cost_func == "ray":
        adjuster = cv.detail_BundleAdjusterRay()
    elif ba_cost_func == "affine":
        adjuster = cv.detail_BundleAdjusterAffinePartial()
    elif ba_cost_func == "no":
        adjuster = cv.detail_NoBundleAdjuster()
    else:
        print("Unknown bundle adjustment cost function: ", ba_cost_func)
        exit()

    # Set the threshold for the adjuster
    adjuster.setConfThresh(1)
    # Pre-allocate array of the mask to be applied to determine which camera parameters to compute
    refine_mask = np.zeros((3, 3), np.uint8)

    # Determine which parameters to compute? Or vice versa... not compute
    # TODO check this
    if ba_refine_mask[0] == 'x':
        refine_mask[0, 0] = 1
    if ba_refine_mask[1] == 'x':
        refine_mask[0, 1] = 1
    if ba_refine_mask[2] == 'x':
        refine_mask[0, 2] = 1
    if ba_refine_mask[3] == 'x':
        refine_mask[1, 1] = 1
    if ba_refine_mask[4] == 'x':
        refine_mask[1, 2] = 1

    # Apply the refinement mask to the adjuster
    adjuster.setRefinementMask(refine_mask)

    # Recompute the camera orientation parameters with the refinement mask
    b, cameras = adjuster.apply(features, p, cameras)

    # Check if the parameters adjusted correctly
    if not b:
        print("Camera parameters adjusting failed.")
        exit()

    # Get list of focal lengths to scale images accordingly...
    # TODO probably remove this scaling as not required for this project, thus warped_image_scale should stay = 1
    focals = []
    for cam in cameras:
        focals.append(cam.focal)
    sorted(focals)
    if len(focals) % 2 == 1:
        warped_image_scale = focals[len(focals) // 2]
    else:
        warped_image_scale = (focals[len(focals) // 2]+focals[len(focals) // 2-1])/2

    # Perform the wave correction
    # TODO adjust this... only performs a horizontal correction, need to implement a vertical correction. Possibly both.
    #   Potentially not required at all if the estimation of camera parameter bundle adjustment is performed well
    if do_wave_correct:
        rmats = []
        for cam in cameras:
            rmats.append(np.copy(cam.R))

        if wave_correct == 'vert':
            rmats = cv.detail.waveCorrect(rmats, cv.detail.WAVE_CORRECT_VERT)
        elif wave_correct == 'horiz':
            rmats = cv.detail.waveCorrect(rmats, cv.detail.WAVE_CORRECT_HORIZ)

        for idx, cam in enumerate(cameras):
            cam.R = rmats[idx]

    # Pre-allocation
    corners = []            # Dimensions of warped images
    masks_warped = []       # The masking regions of the warped areas
    images_warped = []      # the images warped
    sizes = []              # Sizes of ....?
    masks = []              # Masks for the seams ...?

    # Iterate through the images creating 'i' as the index number and appending the pre-allocated mask
    for i in range(0, num_images):
        um = cv.UMat(255*np.ones((images[i].shape[0], images[i].shape[1]), np.uint8))
        masks.append(um)

    # This creates the warper to be used to distort the images according to the layout or shape they're to be stitched
    warper = cv.PyRotationWarper(warp_type, warped_image_scale*seam_work_aspect)

    # Iterate through the images to create the seams
    for idx in range(0, num_images):
        # Get the respective camera matrix
        mat_k = cameras[idx].K().astype(np.float32)

        # Scale the K matrix for the seam aspect scale
        mat_k[0, 0] *= seam_work_aspect
        mat_k[0, 2] *= seam_work_aspect
        mat_k[1, 1] *= seam_work_aspect
        mat_k[1, 2] *= seam_work_aspect

        # Project the image into the warping shape
        # TODO Not sure if we actually need the images warped. If the rotation and translation should suffice
        corner, image_wp = warper.warp(images[idx], mat_k, cameras[idx].R, cv.INTER_LINEAR, cv.BORDER_REFLECT)
        corners.append(corner)
        sizes.append((image_wp.shape[1], image_wp.shape[0]))
        images_warped.append(image_wp)

        # Warp the masks as well
        p, mask_wp = warper.warp(masks[idx], mat_k, cameras[idx].R, cv.INTER_NEAREST, cv.BORDER_CONSTANT)
        masks_warped.append(mask_wp.get())

    # Pre-allocation and conversion of the images warped images to floats
    images_warped_f = []
    for img in images_warped:
        imgf = img.astype(np.float32)
        images_warped_f.append(imgf)

    # If exposure correction required.... Shouldn't although there is one bad image in there so quite possibly
    if cv.detail.ExposureCompensator_CHANNELS == expos_comp_type:
        compensator = cv.detail_ChannelsCompensator(expos_comp_nr_feeds)
    #    compensator.setNrGainsFilteringIterations(expos_comp_nr_filtering)
    elif cv.detail.ExposureCompensator_CHANNELS_BLOCKS == expos_comp_type:
        compensator = cv.detail_BlocksChannelsCompensator(
            expos_comp_block_size, expos_comp_block_size, expos_comp_nr_feeds)
    #    compensator.setNrGainsFilteringIterations(expos_comp_nr_filtering)
    else:
        compensator = cv.detail.ExposureCompensator_createDefault(expos_comp_type)

    # Apply the exposure compensator? Or set it up at least
    compensator.feed(corners=corners, images=images_warped, masks=masks_warped)

    # Define the type of seam finder to be used
    if seam_find_type == "no":
        seam_finder = cv.detail.SeamFinder_createDefault(cv.detail.SeamFinder_NO)
    elif seam_find_type == "voronoi":
        seam_finder = cv.detail.SeamFinder_createDefault(cv.detail.SeamFinder_VORONOI_SEAM)
    elif seam_find_type == "gc_color":
        seam_finder = cv.detail_GraphCutSeamFinder("COST_COLOR")
    elif seam_find_type == "gc_colorgrad":
        seam_finder = cv.detail_GraphCutSeamFinder("COST_COLOR_GRAD")
    elif seam_find_type == "dp_color":
        seam_finder = cv.detail_DpSeamFinder("COLOR")
    elif seam_find_type == "dp_colorgrad":
        seam_finder = cv.detail_DpSeamFinder("COLOR_GRAD")
    if seam_finder is None:
        print("Can't create the following seam finder ", seam_find_type)
        exit()

    # Find the seams
    # TODO potentially try using the non-warped images
    seam_finder.find(images_warped_f, corners, masks_warped)

    # Clear the variables from memory / use later
    imgListe = []
    images_warped = []
    images_warped_f = []
    masks = []

    # Clear or pre-allocate variables
    compose_scale = 1
    corners = []
    sizes = []
    blender = None
    timelapser = None
    compose_work_aspect = 1

    # Iterate through all the images again
    for idx, name in enumerate(img_names):
        # Read in image and get the composition scale. Should be left to 1...
        # TODO check the composition scale and whether this needs to be scaled
        full_img = cv.imread(name)

        # Compute the composition scale, work aspect ratio, warped image scale and create a warper to scale
        if not is_compose_scale_set:
            if compose_megapix > 0:
                compose_scale = min(1.0, np.sqrt(compose_megapix * 1e6 / (full_img.shape[0]*full_img.shape[1])))
            is_compose_scale_set = True
            compose_work_aspect = compose_scale / work_scale
            warped_image_scale *= compose_work_aspect
            warper = cv.PyRotationWarper(warp_type, warped_image_scale)

            for i in range(0, len(img_names)):
                # Adjust the camera parameters based on the composition work aspect ratio
                cameras[i].focal *= compose_work_aspect
                cameras[i].ppx *= compose_work_aspect
                cameras[i].ppy *= compose_work_aspect

                # Compute the size of the scaled full image
                sz = (full_img_sizes[i][0] * compose_scale, full_img_sizes[i][1]*compose_scale)

                # Get the intrinsic camera matrix
                mat_k = cameras[i].K().astype(np.float32)

                # Generate a warper for the rotation and intrinsic matrix
                # TODO this could possibly be just the rotation matrix
                #   One possibility this isn't working is due to it should be translating images...
                roi = warper.warpRoi(sz, mat_k, cameras[i].R)

                # Get the corners from the output parameters
                corners.append(roi[0:2])
                # Get the sizes of the output parameters
                sizes.append(roi[2:4])

        # Scale the image to a size greater than the full resolution, otherwise leave as is
        if abs(compose_scale - 1) > 1e-1:
            img = cv.resize(src=full_img, dsize=None, fx=compose_scale, fy=compose_scale, interpolation=cv.INTER_LINEAR_EXACT)
        else:
            img = full_img

        # Create a tuple of the image size
        img_size = (img.shape[1], img.shape[0])

        # Convert the intrinsic matrix to float 32
        mat_k = cameras[idx].K().astype(np.float32)

        # Warp the images accordingly...
        # TODO look at the interpolation and border values
        corner, image_warped = warper.warp(img, mat_k, cameras[idx].R, cv.INTER_LINEAR, cv.BORDER_REFLECT)

        # Define the masks and warp them as well to the same shape
        mask = 255*np.ones((img.shape[0], img.shape[1]), np.uint8)
        p, mask_warped = warper.warp(mask, mat_k, cameras[idx].R, cv.INTER_NEAREST, cv.BORDER_CONSTANT)

        # Apply the exposure compensation
        compensator.apply(idx, corners[idx], image_warped, mask_warped)

        # Convert the images back to integer for minimal memory use
        image_warped_s = image_warped.astype(np.int16)
        image_warped = []   # Clear variable

        # Dilate the warped mask image
        dilated_mask = cv.dilate(masks_warped[idx], None)

        # Resize the dilated mask to create the seam mask
        seam_mask = cv.resize(dilated_mask, (mask_warped.shape[1], mask_warped.shape[0]), 0, 0, cv.INTER_LINEAR_EXACT)

        # Get the output seam
        mask_warped = cv.bitwise_and(seam_mask, mask_warped)

        # If the blender object hasn't been created yet
        if blender is None and not timelapse:
            blender = cv.detail.Blender_createDefault(cv.detail.Blender_NO)
            dst_sz = cv.detail.resultRoi(corners=corners, sizes=sizes)
            # Get the blend width
            blend_width = np.sqrt(dst_sz[2]*dst_sz[3]) * blend_strength / 100
            # Check if a width is computed. Based on the blend strength
            if blend_width < 1:
                blender = cv.detail.Blender_createDefault(cv.detail.Blender_NO)

            # Create a multiband blender
            elif blend_type == "multiband":
                blender = cv.detail_MultiBandBlender()
                blender.setNumBands((np.log(blend_width)/np.log(2.) - 1.).astype(np.int))

            # Create a feather blender
            elif blend_type == "feather":
                blender = cv.detail_FeatherBlender()
                blender.setSharpness(1./blend_width)

            # Prepare the blender based on the distance
            blender.prepare(dst_sz)

        # If a timelapse type is passed, create the timelapser object
        elif timelapser is None and timelapse:
            timelapser = cv.detail.Timelapser_createDefault(timelapse_type)
            timelapser.initialize(corners, sizes)

        # If the timelapse parameter is passed
        if timelapse:
            # Initialise an array of ones of the right shape
            matones = np.ones((image_warped_s.shape[0], image_warped_s.shape[1]), np.uint8)

            # Adds the warped image into the list
            timelapser.process(image_warped_s, matones, corners[idx])

            # Get the index for where the file name starts
            pos_s = img_names[idx].rfind("/")

            # Get the fixed file name
            if pos_s == -1:
                fixed_file_name = "fixed_" + img_names[idx]
            else:
                fixed_file_name = img_names[idx][:pos_s + 1]+"fixed_" + img_names[idx][pos_s + 1:]

            # Write the temporary partial image
            cv.imwrite(fixed_file_name, timelapser.getDst())
        else:
            # Pass the warped image into the blender
            blender.feed(cv.UMat(image_warped_s), mask_warped, corners[idx])

    # If the timelapse parameter is not passed
    if not timelapse:
        # Pre-allocate the results
        result = None
        result_mask = None

        # Get the blended results
        result, result_mask = blender.blend(result, result_mask)

        # Output the final result
        cv.imwrite(result_name, result)

        # Make the image shape fit into the window
        zoomx = 600.0 / result.shape[1]

        # Show the final output
        dst = cv.normalize(src=result, dst=None, alpha=255., norm_type=cv.NORM_MINMAX, dtype=cv.CV_8U)
        dst = cv.resize(dst, dsize=None, fx=zoomx, fy=zoomx)
        cv.imshow(result_name, dst)
        cv.waitKey()

    print('Done')
Ejemplo n.º 19
0
    def stitch(self):
        if self.wave_correct == 'no':
            do_wave_correct = False
        else:
            do_wave_correct = True

        images = self.images[:]
        full_img_sizes = []
        finder = FEATURES_FIND_CHOICES[self.features]()
        seam_work_aspect = 1
        features = []
        is_work_scale_set = False
        is_seam_scale_set = False
        is_compose_scale_set = False
        for idx, full_img in enumerate(images):
            full_img_sizes.append((full_img.shape[1], full_img.shape[0]))
            if self.work_megapix < 0:
                img = full_img
                work_scale = 1
                is_work_scale_set = True
            else:
                if is_work_scale_set is False:
                    work_scale = min(
                        1.0,
                        np.sqrt(self.work_megapix * 1e6 /
                                (full_img.shape[0] * full_img.shape[1])))
                    is_work_scale_set = True
                img = cv.resize(src=full_img,
                                dsize=None,
                                fx=work_scale,
                                fy=work_scale,
                                interpolation=cv.INTER_LINEAR_EXACT)
            if is_seam_scale_set is False:
                seam_scale = min(
                    1.0,
                    np.sqrt(self.seam_megapix * 1e6 /
                            (full_img.shape[0] * full_img.shape[1])))
                seam_work_aspect = seam_scale / work_scale
                is_seam_scale_set = True
            img_feat = cv.detail.computeImageFeatures2(finder, img)
            features.append(img_feat)
            img = cv.resize(src=img,
                            dsize=None,
                            fx=seam_scale,
                            fy=seam_scale,
                            interpolation=cv.INTER_LINEAR_EXACT)
            images[idx] = img

        matcher = self.get_matcher()
        p = matcher.apply2(features)
        matcher.collectGarbage()

        indices = cv.detail.leaveBiggestComponent(features, p, 0.3)
        img_subset = []
        full_img_sizes_subset = []
        for i in range(len(indices)):
            img_subset.append(images[indices[i, 0]])
            full_img_sizes_subset.append(full_img_sizes[indices[i, 0]])
        images = img_subset
        full_img_sizes = full_img_sizes_subset
        num_images = len(images)
        if num_images < 2:
            print("Need more images!")
            exit()

        estimator = ESTIMATOR_CHOICES[self.estimator]()
        b, cameras = estimator.apply(features, p, None)
        if not b:
            print("Homography estimation failed.")
            exit()
        for cam in cameras:
            cam.R = cam.R.astype(np.float32)

        adjuster = BA_COST_CHOICES[self.ba]()
        adjuster.setConfThresh(1)
        refine_mask = np.zeros((3, 3), np.uint8)
        refine_mask[0, :] = 1
        refine_mask[1, 0:2] = 1
        adjuster.setRefinementMask(refine_mask)
        b, cameras = adjuster.apply(features, p, cameras)
        if not b:
            print("Camera parameters adjusting failed.")
            exit()
        focals = []
        for cam in cameras:
            focals.append(cam.focal)
        sorted(focals)
        if len(focals) % 2 == 1:
            warped_image_scale = focals[len(focals) // 2]
        else:
            warped_image_scale = (focals[len(focals) // 2] +
                                  focals[len(focals) // 2 - 1]) / 2
        if do_wave_correct:
            rmats = []
            for cam in cameras:
                rmats.append(np.copy(cam.R))
            rmats = cv.detail.waveCorrect(rmats, cv.detail.WAVE_CORRECT_HORIZ)
            for idx, cam in enumerate(cameras):
                cam.R = rmats[idx]

        corners = []
        masks_warped = []
        images_warped = []
        sizes = []
        masks = []
        for i in range(0, num_images):
            um = cv.UMat(255 * np.ones(
                (images[i].shape[0], images[i].shape[1]), np.uint8))
            masks.append(um)

        warper = cv.PyRotationWarper(self.warp,
                                     warped_image_scale * seam_work_aspect)
        for idx in range(0, num_images):
            K = cameras[idx].K().astype(np.float32)
            swa = seam_work_aspect
            K[0, 0] *= swa
            K[0, 2] *= swa
            K[1, 1] *= swa
            K[1, 2] *= swa
            corner, image_wp = warper.warp(images[idx], K, cameras[idx].R,
                                           cv.INTER_LINEAR, cv.BORDER_REFLECT)
            corners.append(corner)
            sizes.append((image_wp.shape[1], image_wp.shape[0]))
            images_warped.append(image_wp)
            p, mask_wp = warper.warp(masks[idx], K, cameras[idx].R,
                                     cv.INTER_NEAREST, cv.BORDER_CONSTANT)
            masks_warped.append(mask_wp.get())

        images_warped_f = []
        for img in images_warped:
            imgf = img.astype(np.float32)
            images_warped_f.append(imgf)

        compensator = self.get_compensator()
        compensator.feed(corners=corners,
                         images=images_warped,
                         masks=masks_warped)

        seam_finder = SEAM_FIND_CHOICES[self.seam]
        seam_finder.find(images_warped_f, corners, masks_warped)
        compose_scale = 1
        corners = []
        sizes = []
        blender = None
        for idx, img in enumerate(self.images):
            if not is_compose_scale_set:
                if self.compose_megapix > 0:
                    compose_scale = min(
                        1.0,
                        np.sqrt(self.compose_megapix * 1e6 /
                                (img.shape[0] * img.shape[1])))
                is_compose_scale_set = True
                compose_work_aspect = compose_scale / work_scale
                warped_image_scale *= compose_work_aspect
                warper = cv.PyRotationWarper(self.warp, warped_image_scale)
                for i in range(0, len(images)):
                    cameras[i].focal *= compose_work_aspect
                    cameras[i].ppx *= compose_work_aspect
                    cameras[i].ppy *= compose_work_aspect
                    sz = (full_img_sizes[i][0] * compose_scale,
                          full_img_sizes[i][1] * compose_scale)
                    K = cameras[i].K().astype(np.float32)
                    roi = warper.warpRoi(sz, K, cameras[i].R)
                    corners.append(roi[0:2])
                    sizes.append(roi[2:4])
            if abs(compose_scale - 1) > 1e-1:
                compose_img = cv.resize(src=img,
                                        dsize=None,
                                        fx=compose_scale,
                                        fy=compose_scale,
                                        interpolation=cv.INTER_LINEAR_EXACT)
            else:
                compose_img = img
            K = cameras[idx].K().astype(np.float32)
            corner, image_warped = warper.warp(img, K, cameras[idx].R,
                                               cv.INTER_LINEAR,
                                               cv.BORDER_REFLECT)
            mask = 255 * np.ones((img.shape[0], img.shape[1]), np.uint8)
            p, mask_warped = warper.warp(mask, K, cameras[idx].R,
                                         cv.INTER_NEAREST, cv.BORDER_CONSTANT)
            compensator.apply(idx, corners[idx], image_warped, mask_warped)
            image_warped_s = image_warped.astype(np.int16)
            dilated_mask = cv.dilate(masks_warped[idx], None)
            seam_mask = cv.resize(dilated_mask,
                                  (mask_warped.shape[1], mask_warped.shape[0]),
                                  0, 0, cv.INTER_LINEAR_EXACT)
            mask_warped = cv.bitwise_and(seam_mask, mask_warped)
            if blender is None:
                blender = cv.detail.Blender_createDefault(cv.detail.Blender_NO)
                dst_sz = cv.detail.resultRoi(corners=corners, sizes=sizes)
                blend_width = np.sqrt(
                    dst_sz[2] * dst_sz[3]) * self.blend_strength / 100
                if blend_width < 1:
                    blender = cv.detail.Blender_createDefault(
                        cv.detail.Blender_NO)
                elif self.blend == 'multiband':
                    blender = cv.detail_MultiBandBlender()
                    blender.setNumBands(
                        (np.log(blend_width) / np.log(2.) - 1.).astype(np.int))
                elif self.blend == "feather":
                    blender = cv.detail_FeatherBlender()
                    blender.setSharpness(1. / blend_width)
                blender.prepare(dst_sz)
            blender.feed(cv.UMat(image_warped_s), mask_warped, corners[idx])

        result = None
        result_mask = None
        result, result_mask = blender.blend(result, result_mask)
        # cv.imwrite(self.output, result)
        zoom_x = 600.0 / result.shape[1]
        dst = cv.normalize(src=result,
                           dst=None,
                           alpha=255.,
                           norm_type=cv.NORM_MINMAX,
                           dtype=cv.CV_8U)
        dst = cv.resize(dst, dsize=None, fx=zoom_x, fy=zoom_x)
        # cv.imshow(self.output, dst)
        # cv.waitKey()
        return dst