c++ - OpenCV 保存带有 imwrite 问题的大图像

标签 c++ opencv

我想使用 OpenCV 保存一张由 300 张大小为 256x256 的图像组成的图像(即宽度:300*256,高度:256)。

我尝试使用以下代码保存:

#include <string.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <vector>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>

using namespace std;
using namespace cv;

int main(int argc, char **argv)
{
    int img_num = 300;
    cv::Mat img = cv::imread( "256.jpg", cv::IMREAD_UNCHANGED );
    if( img.empty() )
    {
        return -1;
    }
    cv::Mat img_big = cv::Mat::zeros(256,256*img_num,CV_8UC3);
    for (int i = 0; i < img_num; i++)
    {   img(cv::Rect(0,0,256,256)).copyTo(img_big(cv::Rect((i)*256,0,256,256)));        
    }
vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
compression_params.push_back(100);
    imwrite("big.jpg",img_big,compression_params);
    img_big.release();  
    img.release();  
}

并用

编译
g++ -std=gnu++0x -o saveOpenCV saveOpenCV.cpp `pkg-config --libs --cflags opencv`

我预计结果将是一张尺寸为 76800x256 的图像(连续 300 张尺寸为 256x256 的图像),但输出图像只有 4.1kb,无法打开。将图像编号更改为 200 或 250 时,结果正常。我观察到,如果图像编号大于 250,则会出现问题。

谁能告诉我哪里出了问题,或者在机器上试试我的代码,看看它是否会出现同样的问题?

最佳答案

尝试使用其他图像格式。

在 Google 上快速搜索发现:

According to wikipedia JPG supports a maximum image size of 65535×65535. The JPEG format limit is 64k in either dimension. But the Photoshop implementation prior to 13.1 only allowed up to 30000 pixels in either dimension (historical reasons, plus odd bugs that took a while to straighten out)

因此最大为 256 像素的 255 张图像。

可能 imwrite 正在报告错误。您应该始终检查返回值。

关于c++ - OpenCV 保存带有 imwrite 问题的大图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55663346/

相关文章:

c++ - 将敏感数据存储在 C++ 编译的二进制文件中是否安全?

C++ 输入文件字符串浮点错误

c++ - 在非 Qt 线程中使用 Qt 信号/槽

python - 使用 openCV 打开视频

android - 在android/openCV中记录相机输入

c++ - 是否可以定义稍后可以评估的 bool 表达式?

C++ OOP 库存和项目类

python - 无法识别视频中的对象

java - 使用opencv Java获取子图像

c++ - 这是函数声明吗?