为了在MATLAB中更快地加载图像,我使用的是多页TIFF文件,这对我来说比MATLAB的简单imread要快得多。但我的问题是TIFF文件的大小比普通图像大(比十倍),所以我正在寻找压缩选项。我已经尝试了一些这些选项(使用下面附带的代码),其中一些不起作用并生成空文件,其余的则会降低速度。
有没有办法兼顾尺寸和速度?
谢谢
P.S:我把我的代码放在这里。如果我做错了请告诉我。
#include <stdio.h>
#include <time.h>
#include "tiffio.h"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace std;
#define XSIZE 1280
#define YSIZE 720
#define NPAGES 1000
#define CHANNEL 3
int main (int argc, char **argv)
{
uint32 image_width, image_height;
float xres, yres;
uint16 spp, bpp, photo, res_unit;
TIFF *out;
int i, j;
uint16 page;
Mat img;
int COMPRESSION_TAG = atoi(argv[1]);
unsigned char *array = new unsigned char [XSIZE * YSIZE*3];
char name[20];
out = TIFFOpen("myfile.tif", "w");
image_width = XSIZE;
image_height = YSIZE;
spp = CHANNEL; /* Samples per pixel */
bpp = 8; /* Bits per sample */
photo = PHOTOMETRIC_MINISBLACK;
for (page = 0; page < NPAGES; page++)
{
sprintf(name, "5_29%03d.jpg", page);
img = imread(name);
array = img.data;
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, image_width );
TIFFSetField(out, TIFFTAG_IMAGELENGTH, image_height);
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, bpp);
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, spp);
TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(out, TIFFTAG_PHOTOMETRIC, photo);
TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT);
TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_TAG);
/* It is good to set resolutions too (but it is not nesessary) */
xres = yres = 100;
res_unit = RESUNIT_INCH;
TIFFSetField(out, TIFFTAG_XRESOLUTION, xres);
TIFFSetField(out, TIFFTAG_YRESOLUTION, yres);
TIFFSetField(out, TIFFTAG_RESOLUTIONUNIT, res_unit);
/* We are writing single page of the multipage file */
TIFFSetField(out, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
/* Set the page number */
TIFFSetField(out, TIFFTAG_PAGENUMBER, page, NPAGES);
for (j = 0; j < image_height; j++)
TIFFWriteScanline(out, &array[3*j * image_width], j, 0);
TIFFWriteDirectory(out);
}
TIFFClose(out);
return 0;
}