openCV combat practice (4) - panorama stitching

Project Introduction:

                After traversing the pictures in the folder through os, use the stitcher to stitch the photos to achieve a panoramic effect.

Opencv project combat: 04 panorama image stitching

project name:

        Panoramic image stitching

Project process preview:

1. Import pictures and read the main file

2. Use the os.listdir(path) function to traverse the pictures in the file

3. Use the Stitcher class in Opencv to briefly create a stitcher configured in one of the stitching modes

4. Use the stitch function under the Stitcher class to return the status code and the stitching result.

5. Check whether the return status is normal

Linkage between projects and knowledge:

                os, stitcher class

Specific operation steps and code:

1. Create a home folder

import library

import cv2
import os

2. Loop through the images

mainFolder = 'image'
myFolders  = os.listdir(mainFolder)
print(myFolders)

3. Suture

for folder in myFolders:
    path = mainFolder + '/' + folder
    images = []
    myList = os.listdir(path)
    print(f'Total number of images detected {len(myList)} ')
    for imgN in myList:
        curImg = cv2.imread(f'{path}/{imgN}')
        curImg = cv2.resize(curImg,(0,0),None,0.2,0.2)
        images.append(curImg)

    stitcher = cv2.Stitcher.create()
    (status,result) = stitcher.stitch(images)
    if(status == cv2.STITCHER_OK):
        print('Panorama Generated Successfully')
        cv2.imshow(folder,result)
        cv2.waitKey(1)

    else :
        print('Panorama Generation Unsuccessful')

cv2.waitKey(0)

4. Display the results

 

 

 

 

The tools and preset parameters required for the project:

(image  show)

Realize the result display:

(img show)

The overall display of the code:

import cv2
import os

mainFolder = 'image'
myFolders  = os.listdir(mainFolder)
print(myFolders)

for folder in myFolders:
    path = mainFolder + '/' + folder
    images = []
    myList = os.listdir(path)
    print(f'Total number of images detected {len(myList)} ')
    for imgN in myList:
        curImg = cv2.imread(f'{path}/{imgN}')
        curImg = cv2.resize(curImg,(0,0),None,0.2,0.2)
        images.append(curImg)

    stitcher = cv2.Stitcher.create()
    (status,result) = stitcher.stitch(images)
    if(status == cv2.STITCHER_OK):
        print('Panorama Generated Successfully')
        cv2.imshow(folder,result)
        cv2.waitKey(1)

    else :
        print('Panorama Generation Unsuccessful')

cv2.waitKey(0)

Guess you like

Origin blog.csdn.net/Crabfishhhhh/article/details/129090905