Opencvは画像を保存して画像を読み取ります

コンテンツ

1つは、画像を保存する

1、imwriteを保存します

2.エンコード形式

3.画像ビット深度

次に、画像を読みます

1、未読を読む

2、読み取りタイプImreadModes


1つは、画像を保存する

1、imwriteを保存します

imwrite("D:/im2.jpg", image2);

3番目のパラメーターはデフォルトです。

2.エンコード形式

jpgは不可逆圧縮、pngは可逆圧縮です。画像の要件が高い場合は、pngを使用することをお勧めします。

3.画像ビット深度

32Fタイプの写真は、このように保存すると8Uになり、読んだ後も8Uになります。

32Fに変換しても元の画像と異なる場合があります。

次に、画像を読みます

1、未読を読む

string path = "D:/im2.jpg";
Mat image = imread(path, IMREAD_UNCHANGED);
if (!image.data) {
	cout << "imread fail\n";
	return;
}

2番目のパラメーターは、ImreadModesタイプの列挙であり、読み取られたチャネルの数を示します。

タイプMatのオブジェクトを返します。

メンバーデータはucharへのポインターであり、読み取りが失敗した場合、ポインターはnullになります。

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チャネルによることを意味します

おすすめ

転載: blog.csdn.net/nameofcsdn/article/details/122966084