使用 OpenCV 3.0 和 MSVS 12 在单个窗口上显示多个图像

问题描述 投票:0回答:2

我是 opencv 和 c++ 的新手,目前正在开发一个程序,该程序要求我使用 opencv 3.0 版和 visual studio 12 显示多个图像。我正在处理以下代码,但它没有用。我想问一下我该如何解决这个问题。

#include <opencv2/imgproc/imgproc.hpp>  
#include <opencv2/core/core.hpp>        
#include <opencv2/highgui/highgui.hpp>
#include <iostream>


using namespace cv;
using namespace std;

int main()
{
//Image Reading
IplImage* img1 = cvLoadImage( "ball.jpg" );
IplImage* img2 = cvLoadImage( "ball.jpg" );
IplImage* img3 = cvLoadImage( "ball.jpg" );
IplImage* img4 = cvLoadImage( "ball.jpg" );

int dstWidth=img1->width+img1->width;
int dstHeight=img1->height+img1->height;

IplImage* dst=cvCreateImage(cvSize(dstWidth,dstHeight),IPL_DEPTH_8U,3); 

// Copy first image to dst
cvSetImageROI(dst, cvRect(0, 0,img1->width,img1->height) );
cvCopy(img1,dst,NULL);
cvResetImageROI(dst);

// Copy second image to dst
cvSetImageROI(dst, cvRect(img2->width, 0,img2->width,img2->height) );
cvCopy(img2,dst,NULL);
cvResetImageROI(dst);

// Copy third image to dst
cvSetImageROI(dst, cvRect(0, img3->height,img3->width,img3->height) );
cvCopy(img3,dst,NULL);
cvResetImageROI(dst);

// Copy fourth image to dst
cvSetImageROI(dst, cvRect(img4->width, img4->height,img4->width,img4->height) );
cvCopy(img4,dst,NULL);
cvResetImageROI(dst);

//show all in a single window
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage( "Example1", dst );
cvWaitKey(0);

}
c++ opencv visual-studio-2012
2个回答
3
投票

如果所有图像都具有相同的大小,那么您可以使用 C++ API 执行此操作:

int main(int argc, char* argv[])
{
    cv::Mat input1 = cv::imread("C:/StackOverflow/Input/Lenna.png");
    cv::Mat input2 = cv::imread("C:/StackOverflow/Input/Lenna.png");
    cv::Mat input3 = cv::imread("C:/StackOverflow/Input/Lenna.png");
    cv::Mat input4 = cv::imread("C:/StackOverflow/Input/Lenna.png");

    int width = 2*input1.cols; // width of 2 images next to each other
    int height = 2*input1.rows; // height of 2 images over reach other

    cv::Mat inputAll = cv::Mat(height, width, input1.type());

    cv::Rect subImageROI = cv::Rect(0, 0, input1.cols, input1.rows);

    // copy to subimage:
    input1.copyTo(inputAll(subImageROI));

    // move to 2nd image ROI position:
    subImageROI.x = input1.cols;
    input2.copyTo(inputAll(subImageROI));

    subImageROI.x = 0;
    subImageROI.y = input1.rows;
    input3.copyTo(inputAll(subImageROI));

    subImageROI.x = input1.cols;
    subImageROI.y = input1.rows;
    input4.copyTo(inputAll(subImageROI));


    cv::imshow("input", inputAll);
    cv::waitKey(0);
    return 0;
}

0
投票

python3版本:

注意:它只适用于所有图片大小相同的情况。

import numpy as np
import math


def all_in_one(imgs):
    num = len(imgs)
    row = math.ceil(math.sqrt(num))
    column = row
    width, height, channel = imgs[0].shape
    all_in_one = np.ones((width * row, height * column, channel), dtype=np.uint8)
    for index, img in enumerate(imgs):
        a = index // row
        b = index % column
        all_in_one[a * width:(a + 1) * width, b * height: (b + 1) * height, :] = img
    return all_in_one
© www.soinside.com 2019 - 2024. All rights reserved.