简体   繁体   English

合并多个图像并在Qt中显示

[英]Combine multiple images and display it in Qt

I am a beginner in OpenCV and Qt, my project now is combining multiple images and display it on Qt Creator. 我是OpenCV和Qt的初学者,我的项目现在是合并多个图像并将其显示在Qt Creator上。

#include <stdio.h>
#include <iostream>
#include <mutex>
#include <opencv2/opencv.hpp>
using namespace std;
int main(int argc, char** argv) {
string img_path = "/home/m/pictures/cat.jpg";
std::vector<cv::Mat> img_pool;
for (int i=0;i<10;i++)
{
    cv::Mat data = cv::imread(img_path,-1);
    img_pool.push_back(data);
}
    cv::Mat data = cv::imread(img_path,-1);
cv::namedWindow("image", CV_WINDOW_NORMAL);
cv::imshow("image",data);
cv::waitKey(0);
cv::Mat data_dst = cv::Mat::zeros(500, 500, data.type());
cv::Mat data_resize;


for(int i=0;i<10;i++)
{
    for(int j=0;j<10;j++)
    {
        cv::resize( img_pool[3], data_resize, cv::Size(50,50));
        cv::Rect f_target(i*50,j*50,50,50);
        data_resize.copyTo(data_dst(f_target));
    }
}
cv::namedWindow("image_n", CV_WINDOW_NORMAL);
cv::imshow("image_n",data_dst);
cv::waitKey(0);
return 0;
}

Here is the result: 结果如下:

我的代码结果

My code now can display one image but what I want to do is display different multiple images, I think I need to load the images or image path to the vector but I failed, so someone can help me? 我的代码现在可以显示一个图像,但是我想显示的是多个图像,我想我需要将图像或图像路径加载到矢量中,但是我失败了,所以有人可以帮助我吗?

There is no Qt used in your code. 您的代码中没有使用Qt。 If you want something like this with Qt, you can use a simple widget with a grid layout or a flowlayout. 如果您想在Qt中使用类似的功能,则可以使用具有网格布局或流布局的简单小部件。 Then load all the images from a path in an array of QImage and the display it. 然后从一个路径中的QImage数组中加载所有图像并显示它。

You can find an example with FlowLayout here. 您可以在此处找到FlowLayout的示例。 You can use a QLabel to display the image, convert your QImage to QPixmap and the use: 您可以使用QLabel来显示图像,将QImage转换为QPixmap并使用:

 QImage yourImage("path");
 QLabel image new QLabel(centralwidget);
 imagelabel->setGeometry(QRect(20, 10, 371, 311));
 imagelabel->setPixmap(QPixmap(QPixmap::fromImage(yourImage));

If you are new with Qt, you can integrate it easily with QML using a FlowLayout and and array of QML components. 如果您不熟悉Qt,则可以使用FlowLayout和QML组件数组轻松将其与QML集成。 Look this example of the usage of an ImageViewer 看这个使用ImageViewer的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM