目录
一,存储图像
1,存储 imwrite
imwrite("D:/im2.jpg", image2);
第三个参数缺省了。
2,编码格式
jpg是有损压缩,png是无损压缩,如果对图片要求很高的,还是用png好一点。
3,图片位深
32F类型的图片,像这样存下来之后会变成8U的,读取之后也是8U的,
即使再转换成32F的,也可能和原图有差异。
二,读取图像
1,读取 imread
string path = "D:/im2.jpg";
Mat image = imread(path, IMREAD_UNCHANGED);
if (!image.data) {
cout << "imread fail\n";
return;
}
第二个参数是ImreadModes类型的枚举,表示读取的通道数
返回的是一个Mat类型的对象。
成员data是uchar的指针,如果读取失败那么指针为空
2,读取类型 ImreadModes
ImreadModes的源代码:
enum ImreadModes {
IMREAD_UNCHANGED = -1, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped). Ignore EXIF orientation.
IMREAD_GRAYSCALE = 0, //!< If set, always convert image to the single channel grayscale image (codec internal conversion).
IMREAD_COLOR = 1, //!< If set, always convert image to the 3 channel BGR color image.
IMREAD_ANYDEPTH = 2, //!< If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
IMREAD_ANYCOLOR = 4, //!< If set, the image is read in any possible color format.
IMREAD_LOAD_GDAL = 8, //!< If set, use the gdal driver for loading the image.
IMREAD_REDUCED_GRAYSCALE_2 = 16, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/2.
IMREAD_REDUCED_COLOR_2 = 17, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.
IMREAD_REDUCED_GRAYSCALE_4 = 32, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/4.
IMREAD_REDUCED_COLOR_4 = 33, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.
IMREAD_REDUCED_GRAYSCALE_8 = 64, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/8.
IMREAD_REDUCED_COLOR_8 = 65, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.
IMREAD_IGNORE_ORIENTATION = 128 //!< If set, do not rotate the image according to EXIF's orientation flag.
};
-1 IMREAD_UNCHANGED 表示按照图片本身的通道数
0 IMREAD_GRAYSCALE 表示灰度图像,即单通道
1 IMREAD_COLOR 表示按照3通道
3,读取yuv400图像
void readYuvImage(const char* path, int row, int col, int ratio = 1)
{
std::ifstream in(path, ios_base::binary);
uchar *ch = new uchar[row * col];
memset(ch, 0, row*col);
Mat img(row, col, CV_8U, ch);
int k = 0;
while(in >> std::noskipws >> ch[k]){
k++;
}
imshow("inmg", img * ratio);
cout << k;
waitKey(0);
}
// readYuvImage("D:/IMG", 200, 200);