简体   繁体   中英

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).

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. When changing image number to 200 or 250, the result is ok. I observed that if image number is bigger than 250 then the problem occurred.

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:

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)

So the maximum is 255 images of 256 pixels.

Maybe imwrite is reporting the error. You should always check the return value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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