简体   繁体   English

OpenCV保存大图像与写入问题

[英]OpenCV saving big image with imwrite problem

I want to save an image which is a sequence of 300 images size 256x256 using OpenCV (ie width: 300*256, height: 256). 我想使用OpenCV保存一个图像,该图像是300张图像大小256x256的序列(即宽度:300 * 256,高度:256)。

I tried to save using the code below: 我尝试使用以下代码进行保存:

#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();  
}

and compiled with 并用

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

I expected the result will be an image with size 76800x256 (300 images size 256x256 in a row) but the output image just 4.1kb and cannot be opened. 我预期结果将是尺寸为76800x256的图像(连续300张图像,尺寸为256x256),但输出图像仅为4.1kb,无法打开。 When changing image number to 200 or 250, the result is ok. 将图像编号更改为200或250时,结果正常。 I observed that if image number is bigger than 250 then the problem occurred. 我观察到,如果图像数大于250,则会出现问题。

Can anybody tell me what is wrong or try my code on machine and see if it occurs the same problem? 谁能告诉我什么地方出了问题,或者在机器上尝试我的代码,看看是否发生相同的问题?

Try using another image format. 尝试使用其他图像格式。

A quick search at Google finds: 在Google上进行的快速搜索发现:

According to wikipedia JPG supports a maximum image size of 65535×65535. 根据Wikipedia的介绍,JPG支持的最大图像尺寸为65535×65535。 The JPEG format limit is 64k in either dimension. 任一尺寸的JPEG格式限制均为64k。 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) 但是在13.1之前的Photoshop实施中,每个尺寸最多只能允许30000像素(历史原因,加上一些奇怪的错误,需要一段时间才能弄清楚)

So the maximum is 255 images of 256 pixels. 因此最大为255张256像素的图像。

Maybe imwrite is reporting the error. 也许imwrite正在报告错误。 You should always check the return value. 您应该始终检查返回值。

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

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