Notice: 函数 _load_textdomain_just_in_time 的调用方法不正确twentyseventeen 域的翻译加载触发过早。这通常表示插件或主题中的某些代码运行过早。翻译应在 init 操作或之后加载。 请查阅调试 WordPress来获取更多信息。 (这个消息是在 6.7.0 版本添加的。) in /var/www/html/wp/wp-includes/functions.php on line 6121
图像合并 – 技术栈点

OPENCV多幅图像拼合函数

实现效果:

iShot2021-10-21 16.31.21

python实现:

import cv2
import numpy as np

def stackImages(scale,imgArray):
    rows = len(imgArray)
    cols = len(imgArray[0])
    rowsAvailable = isinstance(imgArray[0], list)
    width = imgArray[0][0].shape[1]
    height = imgArray[0][0].shape[0]
    if rowsAvailable:
        for x in range ( 0, rows):
            for y in range(0, cols):
                if imgArray[x][y].shape[:2] == imgArray[0][0].shape [:2]:
                    imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale)
                else:
                    imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), None, scale, scale)
                if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR)
        imageBlank = np.zeros((height, width, 3), np.uint8)
        hor = [imageBlank]*rows
        hor_con = [imageBlank]*rows
        for x in range(0, rows):
            hor[x] = np.hstack(imgArray[x])
        ver = np.vstack(hor)
    else:
        for x in range(0, rows):
            if imgArray[x].shape[:2] == imgArray[0].shape[:2]:
                imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale)
            else:
                imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None,scale, scale)
            if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR)
        hor= np.hstack(imgArray)
        ver = hor
    return ver
  
  
img = cv2.imread("image.jpg")
imgBlur = cv2.GaussianBlur(img, (7, 7), 1)#高斯模糊处理
imgGray = cv2.cvtColor(imgBlur, cv2.COLOR_BGR2GRAY)#转换成灰度图
imgStack = stackImages(0.4,([img, imgBlur, imgGray],
                            [imgGray, img, imgBlur]))
cv2.imshow("Result", imgStack)

C++实现

#include <vector>
#include "opencv2/opencv.hpp"
#include <iostream>
#include <cstring>
#include <cmath>

using namespace std;

cv::Mat StackImage(float scale, std::vector< std::vector<cv::Mat> > images){
    int rows = images.size();//获取行数
    int cols = images[0].size();//获取列数

    if (images[0][0].empty()){
        cout<<"image not found"<<endl;
        cv::Mat blankImage = cv::Mat::zeros(cv::Size(1, 1), CV_8UC3);;
        return blankImage;
    }

    int width=images[0][0].cols, height=images[0][0].rows;  //获取第一张图像高度和宽度,剩余图像以此为基准
    int targetWidth=round(width*scale), targetHeight=round(height*scale);   //获取单张图像缩放后的宽度和高度
    int resultWidth=targetWidth*cols, resultHeight=targetHeight*rows;   //计算目标拼图的高度和宽度
    int targetType = CV_8UC3;

    cv::Mat result = cv::Mat::zeros(cv::Size(resultWidth, resultHeight), CV_8UC3);
    
    for (int i=0; i<rows; ++i){
        for (int j=0;j<cols; ++j){
            
            if (images[i][j].empty()){
                continue;
            }

            cv::Mat dstImage;
            int imgWidth=images[0][0].cols, imgHeight=images[0][0].rows;
            cv::resize(images[i][j], dstImage, cv::Size(targetWidth, targetHeight));
            cout<<dstImage.type()<<endl;
            switch(dstImage.type()){
                case 0:cv::cvtColor(dstImage, dstImage, CV_GRAY2BGR); break;
                case 4:cv::cvtColor(dstImage, dstImage, CV_BGRA2BGR); break;
            }
            //dstImage.convertTo(dstImage, CV_8UC3);

            CvRect rect = cvRect( targetWidth*j, targetHeight*i, targetWidth, targetHeight);
            cv::Mat dstMat = result(rect);
            dstImage.colRange(0, dstImage.cols).copyTo(dstMat);
        }
    }
    
    return result;
}


int main(){
    cv::Mat img=cv::imread("image.png");
    
    /*C++11标准可以这么写*/
    vector<vector<cv::Mat> > images { 
        {img, img, img} ,
        {img, img, img} 
    };
    /*备用方法*/
    //vector<vector<cv::Mat> > images(1, vector<cv::Mat>(3));
    //images[0][0]=img;
    //images[0][1]=img;
    //images[0][2]=img;

    cv::Mat imgStack = StackImage(0.6f, images);
    cv::imshow("result", imgStack);
    cv::waitKey();
    cv::destroyAllWindows();
    return 0;
}