【发布时间】:2019-09-03 21:51:12
【问题描述】:
我想使用 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,则出现问题。
谁能告诉我哪里出了问题,或者在机器上试试我的代码,看看它是否会出现同样的问题?
【问题讨论】: