Загрузка
| iOS glue |
| MacOS(OSX) glue |
Classes | |
| struct | cv::Animation |
Represents an animation with multiple frames. The Animation struct is designed to store and manage data for animated sequences such as those from animated formats (e.g., GIF, AVIF, APNG, WebP). It provides support for looping, background color settings, frame timing, and frame storage. More... | |
| class | cv::ImageCollection |
| To read multi-page images on demand. More... | |
Functions | |
| bool | cv::haveImageReader (const String &filename) |
| Checks if the specified image file can be decoded by OpenCV. | |
| bool | cv::haveImageWriter (const String &filename) |
| Checks if the specified image file or specified file extension can be encoded by OpenCV. | |
| size_t | cv::imcount (const String &filename, int flags=IMREAD_ANYCOLOR) |
| Returns the number of images inside the given file. | |
| Mat | cv::imdecode (InputArray buf, int flags) |
| Reads an image from a buffer in memory. | |
| Mat | cv::imdecode (InputArray buf, int flags, Mat *dst) |
| bool | cv::imdecodeanimation (InputArray buf, Animation &animation, int start=0, int count=INT16_MAX) |
| Loads frames from an animated image buffer into an Animation structure. | |
| bool | cv::imdecodemulti (InputArray buf, int flags, std::vector< Mat > &mats, const cv::Range &range=Range::all()) |
| Reads a multi-page image from a buffer in memory. | |
| Mat | cv::imdecodeWithMetadata (InputArray buf, std::vector< int > &metadataTypes, OutputArrayOfArrays metadata, int flags=IMREAD_ANYCOLOR) |
| Reads an image from a memory buffer and extracts associated metadata. | |
| bool | cv::imencode (const String &ext, InputArray img, std::vector< uchar > &buf, const std::vector< int > ¶ms=std::vector< int >()) |
| Encodes an image into a memory buffer. | |
| bool | cv::imencodeanimation (const String &ext, const Animation &animation, std::vector< uchar > &buf, const std::vector< int > ¶ms=std::vector< int >()) |
| Encodes an Animation to a memory buffer. | |
| bool | cv::imencodemulti (const String &ext, InputArrayOfArrays imgs, std::vector< uchar > &buf, const std::vector< int > ¶ms=std::vector< int >()) |
| Encodes array of images into a memory buffer. | |
| bool | cv::imencodeWithMetadata (const String &ext, InputArray img, const std::vector< int > &metadataTypes, InputArrayOfArrays metadata, std::vector< uchar > &buf, const std::vector< int > ¶ms=std::vector< int >()) |
| Encodes an image into a memory buffer. | |
| Mat | cv::imread (const String &filename, int flags=IMREAD_COLOR_BGR) |
| Loads an image from a file. | |
| void | cv::imread (const String &filename, OutputArray dst, int flags=IMREAD_COLOR_BGR) |
| Loads an image from a file. | |
| bool | cv::imreadanimation (const String &filename, Animation &animation, int start=0, int count=INT16_MAX) |
| Loads frames from an animated image file into an Animation structure. | |
| bool | cv::imreadmulti (const String &filename, std::vector< Mat > &mats, int flags=IMREAD_ANYCOLOR) |
| Loads a multi-page image from a file. | |
| bool | cv::imreadmulti (const String &filename, std::vector< Mat > &mats, int start, int count, int flags=IMREAD_ANYCOLOR) |
| Loads images of a multi-page image from a file. | |
| Mat | cv::imreadWithMetadata (const String &filename, std::vector< int > &metadataTypes, OutputArrayOfArrays metadata, int flags=IMREAD_ANYCOLOR) |
| Reads an image from a file along with associated metadata. | |
| bool | cv::imwrite (const String &filename, InputArray img, const std::vector< int > ¶ms=std::vector< int >()) |
| Saves an image to a specified file. | |
| bool | cv::imwriteanimation (const String &filename, const Animation &animation, const std::vector< int > ¶ms=std::vector< int >()) |
| Saves an Animation to a specified file. | |
| static bool | cv::imwritemulti (const String &filename, InputArrayOfArrays img, const std::vector< int > ¶ms=std::vector< int >()) |
| multi-image overload for bindings | |
| bool | cv::imwriteWithMetadata (const String &filename, InputArray img, const std::vector< int > &metadataTypes, InputArrayOfArrays &metadata, const std::vector< int > ¶ms=std::vector< int >()) |
| Saves an image to a specified file with metadata. | |
| bool cv::haveImageReader | ( | const String & | filename | ) |
| Python: | ||||
|---|---|---|---|---|
| cv.haveImageReader( | filename | ) -> | retval |
#include <opencv2/imgcodecs.hpp>
Checks if the specified image file can be decoded by OpenCV.
The function haveImageReader checks if OpenCV is capable of reading the specified file. This can be useful for verifying support for a given image format before attempting to load an image.
| filename | The name of the file to be checked. |
| bool cv::haveImageWriter | ( | const String & | filename | ) |
| Python: | ||||
|---|---|---|---|---|
| cv.haveImageWriter( | filename | ) -> | retval |
#include <opencv2/imgcodecs.hpp>
Checks if the specified image file or specified file extension can be encoded by OpenCV.
The function haveImageWriter checks if OpenCV is capable of writing images with the specified file extension. This can be useful for verifying support for a given image format before attempting to save an image.
| filename | The name of the file or the file extension (e.g., ".jpg", ".png"). It is recommended to provide the file extension rather than the full file name. |
| size_t cv::imcount | ( | const String & | filename, |
| int | flags = IMREAD_ANYCOLOR ) |
| Python: | ||||
|---|---|---|---|---|
| cv.imcount( | filename[, flags] | ) -> | retval |
#include <opencv2/imgcodecs.hpp>
Returns the number of images inside the given file.
The function imcount returns the number of pages in a multi-page image (e.g. TIFF), the number of frames in an animation (e.g. AVIF), and 1 otherwise. If the image cannot be decoded, 0 is returned.
| filename | Name of file to be loaded. |
| flags | Flag that can take values of cv::ImreadModes, default with cv::IMREAD_ANYCOLOR. |
| Mat cv::imdecode | ( | InputArray | buf, |
| int | flags ) |
| Python: | ||||
|---|---|---|---|---|
| cv.imdecode( | buf, flags | ) -> | retval |
#include <opencv2/imgcodecs.hpp>
Reads an image from a buffer in memory.
The function imdecode reads an image from the specified buffer in the memory. If the buffer is too short or contains invalid data, the function returns an empty matrix ( Mat::data==NULL ).
See cv::imread for the list of supported formats and flags description.
| buf | Input array or vector of bytes. |
| flags | Flag that can take values of . |
| Mat cv::imdecode | ( | InputArray | buf, |
| int | flags, | ||
| Mat * | dst ) |
| Python: | ||||
|---|---|---|---|---|
| cv.imdecode( | buf, flags | ) -> | retval |
#include <opencv2/imgcodecs.hpp>
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
| buf | Input array or vector of bytes. |
| flags | Flag that can take values of cv::ImreadModes, default with cv::IMREAD_ANYCOLOR. |
| dst | The optional output placeholder for the decoded matrix. It can save the image reallocations when the function is called repeatedly for images of the same size. In case of decoder failure the function returns empty object, but does not release user-provided dst buffer. |
| bool cv::imdecodeanimation | ( | InputArray | buf, |
| Animation & | animation, | ||
| int | start = 0, | ||
| int | count = INT16_MAX ) |
| Python: | ||||
|---|---|---|---|---|
| cv.imdecodeanimation( | buf[, start[, count]] | ) -> | retval, animation |
#include <opencv2/imgcodecs.hpp>
Loads frames from an animated image buffer into an Animation structure.
The function imdecodeanimation loads frames from an animated image buffer (e.g., GIF, AVIF, APNG, WEBP) into the provided Animation struct.
| buf | A reference to an InputArray containing the image buffer. |
| animation | A reference to an Animation structure where the loaded frames will be stored. It should be initialized before the function is called. |
| bool cv::imdecodemulti | ( | InputArray | buf, |
| int | flags, | ||
| std::vector< Mat > & | mats, | ||
| const cv::Range & | range = Range::all() ) |
| Python: | ||||
|---|---|---|---|---|
| cv.imdecodemulti( | buf, flags[, mats[, range]] | ) -> | retval, mats |
#include <opencv2/imgcodecs.hpp>
Reads a multi-page image from a buffer in memory.
The function imdecodemulti reads a multi-page image from the specified buffer in the memory. If the buffer is too short or contains invalid data, the function returns false.
See cv::imreadmulti for the list of supported formats and flags description.
| buf | Input array or vector of bytes. |
| flags | Flag that can take values of cv::ImreadModes. |
| Mat cv::imdecodeWithMetadata | ( | InputArray | buf, |
| std::vector< int > & | metadataTypes, | ||
| OutputArrayOfArrays | metadata, | ||
| int | flags = IMREAD_ANYCOLOR ) |
| Python: | ||||
|---|---|---|---|---|
| cv.imdecodeWithMetadata( | buf[, metadata[, flags]] | ) -> | retval, metadataTypes, metadata |
#include <opencv2/imgcodecs.hpp>
Reads an image from a memory buffer and extracts associated metadata.
This function decodes an image from the specified memory buffer. If the buffer is too short or contains invalid data, the function returns an empty matrix ( Mat::data==NULL ).
See cv::imread for the list of supported formats and flags description.
| buf | Input array or vector of bytes containing the encoded image data. |
| metadataTypes |
| bool cv::imencode | ( | const String & | ext, |
| InputArray | img, | ||
| std::vector< uchar > & | buf, | ||
| const std::vector< int > & | params = std::vector< int >() ) |
| Python: | ||||
|---|---|---|---|---|
| cv.imencode( | ext, img[, params] | ) -> | retval, buf |
#include <opencv2/imgcodecs.hpp>
Encodes an image into a memory buffer.
The function imencode compresses the image and stores it in the memory buffer that is resized to fit the result. See cv::imwrite for the list of supported formats and flags description.
| ext | File extension that defines the output format. Must include a leading period. |
| img | Image to be compressed. |
| buf | Output buffer resized to fit the compressed image. |
| params | Format-specific parameters. See cv::imwrite and . |
| bool cv::imencodeanimation | ( | const String & | ext, |
| const Animation & | animation, | ||
| std::vector< uchar > & | buf, | ||
| const std::vector< int > & | params = std::vector< int >() ) |
| Python: | ||||
|---|---|---|---|---|
| cv.imencodeanimation( | ext, animation[, params] | ) -> | retval, buf |
#include <opencv2/imgcodecs.hpp>
Encodes an Animation to a memory buffer.
The function imencodeanimation encodes the provided Animation data into a memory buffer in an animated format. Supported formats depend on the implementation and may include formats like GIF, AVIF, APNG, or WEBP.
| ext | The file extension that determines the format of the encoded data. |
| animation | A constant reference to an Animation struct containing the frames and metadata to be encoded. |
| bool cv::imencodemulti | ( | const String & | ext, |
| InputArrayOfArrays | imgs, | ||
| std::vector< uchar > & | buf, | ||
| const std::vector< int > & | params = std::vector< int >() ) |
| Python: | ||||
|---|---|---|---|---|
| cv.imencodemulti( | ext, imgs[, params] | ) -> | retval, buf |
#include <opencv2/imgcodecs.hpp>
Encodes array of images into a memory buffer.
The function is analog to cv::imencode for in-memory multi-page image compression. See cv::imwrite for the list of supported formats and flags description.
| ext | File extension that defines the output format. Must include a leading period. |
| imgs | Vector of images to be written. |
| buf | Output buffer resized to fit the compressed data. |
| params | Format-specific parameters. See and . |
| bool cv::imencodeWithMetadata | ( | const String & | ext, |
| InputArray | img, | ||
| const std::vector< int > & | metadataTypes, | ||
| InputArrayOfArrays | metadata, | ||
| std::vector< uchar > & | buf, | ||
| const std::vector< int > & | params = std::vector< int >() ) |
| Python: | ||||
|---|---|---|---|---|
| cv.imencodeWithMetadata( | ext, img, metadataTypes, metadata[, params] | ) -> | retval, buf |
#include <opencv2/imgcodecs.hpp>
Encodes an image into a memory buffer.
The function imencode compresses the image and stores it in the memory buffer that is resized to fit the result. See cv::imwrite for the list of supported formats and flags description.
| ext | File extension that defines the output format. Must include a leading period. |
| img | Image to be compressed. |
| metadataTypes | Vector with types of metadata chucks stored in metadata to write, see ImageMetadataType. |
| metadata | Vector of vectors or vector of matrices with chunks of metadata to store into the file |
| Mat cv::imread | ( | const String & | filename, |
| int | flags = IMREAD_COLOR_BGR ) |
| Python: | ||||
|---|---|---|---|---|
| cv.imread( | filename[, flags] | ) -> | retval | |
| cv.imread( | filename[, dst[, flags]] | ) -> | dst |
#include <opencv2/imgcodecs.hpp>
Loads an image from a file.
The imread function loads an image from the specified file and returns OpenCV matrix. If the image cannot be read (because of a missing file, improper permissions, or unsupported/invalid format), the function returns an empty matrix.
Currently, the following file formats are supported:
| void cv::imread | ( | const String & | filename, |
| OutputArray | dst, | ||
| int | flags = IMREAD_COLOR_BGR ) |
| Python: | ||||
|---|---|---|---|---|
| cv.imread( | filename[, flags] | ) -> | retval | |
| cv.imread( | filename[, dst[, flags]] | ) -> | dst |
#include <opencv2/imgcodecs.hpp>
Loads an image from a file.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts and the return value.
| filename | Name of file to be loaded. |
| dst | object in which the image will be loaded. |
| flags | Flag that can take values of cv::ImreadModes, default with cv::IMREAD_COLOR_BGR. |
| bool cv::imreadanimation | ( | const String & | filename, |
| Animation & | animation, | ||
| int | start = 0, | ||
| int | count = INT16_MAX ) |
| Python: | ||||
|---|---|---|---|---|
| cv.imreadanimation( | filename[, start[, count]] | ) -> | retval, animation |
#include <opencv2/imgcodecs.hpp>
Loads frames from an animated image file into an Animation structure.
The function imreadanimation loads frames from an animated image file (e.g., GIF, AVIF, APNG, WEBP) into the provided Animation struct.
| filename | A string containing the path to the file. |
| animation | A reference to an Animation structure where the loaded frames will be stored. It should be initialized before the function is called. |
| bool cv::imreadmulti | ( | const String & | filename, |
| std::vector< Mat > & | mats, | ||
| int | flags = IMREAD_ANYCOLOR ) |
| Python: | ||||
|---|---|---|---|---|
| cv.imreadmulti( | filename[, mats[, flags]] | ) -> | retval, mats | |
| cv.imreadmulti( | filename, start, count[, mats[, flags]] | ) -> | retval, mats |
#include <opencv2/imgcodecs.hpp>
Loads a multi-page image from a file.
The function imreadmulti loads a multi-page image from the specified file into a vector of Mat objects.
| filename | Name of file to be loaded. |
| mats | A vector of Mat objects holding each page. |
| flags | Flag that can take values of cv::ImreadModes, default with . |
| bool cv::imreadmulti | ( | const String & | filename, |
| std::vector< Mat > & | mats, | ||
| int | start, | ||
| int | count, | ||
| int | flags = IMREAD_ANYCOLOR ) |
| Python: | ||||
|---|---|---|---|---|
| cv.imreadmulti( | filename[, mats[, flags]] | ) -> | retval, mats | |
| cv.imreadmulti( | filename, start, count[, mats[, flags]] | ) -> | retval, mats |
#include <opencv2/imgcodecs.hpp>
Loads images of a multi-page image from a file.
The function imreadmulti loads a specified range from a multi-page image from the specified file into a vector of Mat objects.
| filename | Name of file to be loaded. |
| mats | A vector of Mat objects holding each page. |
| start | Start index of the image to load |
| count |
| Mat cv::imreadWithMetadata | ( | const String & | filename, |
| std::vector< int > & | metadataTypes, | ||
| OutputArrayOfArrays | metadata, | ||
| int | flags = IMREAD_ANYCOLOR ) |
| Python: | ||||
|---|---|---|---|---|
| cv.imreadWithMetadata( | filename[, metadata[, flags]] | ) -> | retval, metadataTypes, metadata |
#include <opencv2/imgcodecs.hpp>
Reads an image from a file along with associated metadata.
This function behaves similarly to cv::imread(), loading an image from the specified file. In addition to the image pixel data, it also attempts to extract any available metadata embedded in the file (such as EXIF, XMP, etc.), depending on file format support.
| filename | Name of the file to be loaded. |
| metadataTypes | Output vector with types of metadata chunks returned in metadata, see ImageMetadataType. |
| bool cv::imwrite | ( | const String & | filename, |
| InputArray | img, | ||
| const std::vector< int > & | params = std::vector< int >() ) |
| Python: | ||||
|---|---|---|---|---|
| cv.imwrite( | filename, img[, params] | ) -> | retval |
#include <opencv2/imgcodecs.hpp>
Saves an image to a specified file.
The function imwrite saves the image to the specified file. The image format is chosen based on the filename extension (see cv::imread for the list of extensions). In general, only 8-bit unsigned (CV_8U) single-channel or 3-channel (with 'BGR' channel order) images can be saved using this function, with these exceptions:
| bool cv::imwriteanimation | ( | const String & | filename, |
| const Animation & | animation, | ||
| const std::vector< int > & | params = std::vector< int >() ) |
| Python: | ||||
|---|---|---|---|---|
| cv.imwriteanimation( | filename, animation[, params] | ) -> | retval |
#include <opencv2/imgcodecs.hpp>
Saves an Animation to a specified file.
The function imwriteanimation saves the provided Animation data to the specified file in an animated format. Supported formats depend on the implementation and may include formats like GIF, AVIF, APNG, or WEBP.
| filename | The name of the file where the animation will be saved. The file extension determines the format. |
| animation | A constant reference to an Animation struct containing the frames and metadata to be saved. |
| inlinestatic |
| Python: | ||||
|---|---|---|---|---|
| cv.imwritemulti( | filename, img[, params] | ) -> | retval |
#include <opencv2/imgcodecs.hpp>
multi-image overload for bindings
| bool cv::imwriteWithMetadata | ( | const String & | filename, |
| InputArray | img, | ||
| const std::vector< int > & | metadataTypes, | ||
| InputArrayOfArrays & | metadata, | ||
| const std::vector< int > & | params = std::vector< int >() ) |
| Python: | ||||
|---|---|---|---|---|
| cv.imwriteWithMetadata( | filename, img, metadataTypes, metadata[, params] | ) -> | retval |
#include <opencv2/imgcodecs.hpp>
Saves an image to a specified file with metadata.
The function imwriteWithMetadata saves the image to the specified file. It does the same thing as imwrite, but additionally writes metadata if the corresponding format supports it.
| start |
| The index of the first frame to load. This is optional and defaults to 0. |
| count | The number of frames to load. This is optional and defaults to 32767. |
| mats | A vector of Mat objects holding each page, if more than one. |
| range | A continuous selection of pages. |
| Output vector with types of metadata chucks returned in metadata, see cv::ImageMetadataType |
| metadata | Output vector of vectors or vector of matrices to store the retrieved metadata |
| flags | Flag that can take values of cv::ImreadModes, default with cv::IMREAD_ANYCOLOR. |
| buf | A reference to a vector of unsigned chars where the encoded data will be stored. |
| params | Optional format-specific parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, ...). These parameters are used to specify additional options for the encoding process. Refer to cv::ImwriteFlags for details on possible parameters. |
| buf | Output buffer resized to fit the compressed image. |
| params | Format-specific parameters. See cv::imwrite and cv::ImwriteFlags. |
OPENCV_IO_MAX_IMAGE_PIXELS. See OpenCV environment variables reference.| filename | Name of the file to be loaded. |
| flags | Flag that can take values of cv::ImreadModes, default with cv::IMREAD_COLOR_BGR. |
| start |
| The index of the first frame to load. This is optional and defaults to 0. |
| count | The number of frames to load. This is optional and defaults to 32767. |
| Count number of images to load |
| flags | Flag that can take values of cv::ImreadModes, default with cv::IMREAD_ANYCOLOR. |
| metadata |
| Output vector of vectors or vector of matrices to store the retrieved metadata. |
| flags | Flag that can take values of cv::ImreadModes, default with cv::IMREAD_ANYCOLOR. |
If the image format is not supported, the image will be converted to 8-bit unsigned (CV_8U) and saved that way.
If the format, depth or channel order is different, use Mat::convertTo and cv::cvtColor to convert it before saving. Or, use the universal FileStorage I/O functions to save the image to XML or YAML format.
The sample below shows how to create a BGRA image, how to set custom compression parameters and save it to a PNG file. It also demonstrates how to save multiple images in a TIFF file:
#include <opencv2/imgcodecs.hpp>
using namespace cv;
using namespace std;
static void paintAlphaMat(Mat &mat)
{
CV_Assert(mat.channels() == 4);
for (int i = 0; i < mat.rows; ++i)
{
for (int j = 0; j < mat.cols; ++j)
{
Vec4b& bgra = mat.at<Vec4b>(i, j);
bgra[0] = UCHAR_MAX; // Blue
bgra[1] = saturate_cast<uchar>((float (mat.cols - j)) / ((float)mat.cols) * UCHAR_MAX); // Green
bgra[2] = saturate_cast<uchar>((float (mat.rows - i)) / ((float)mat.rows) * UCHAR_MAX); // Red
bgra[3] = saturate_cast<uchar>(0.5 * (bgra[1] + bgra[2])); // Alpha
}
}
}
int main()
{
Mat mat(480, 640, CV_8UC4); // Create a matrix with alpha channel
paintAlphaMat(mat);
vector<int> compression_params;
compression_params.push_back(IMWRITE_PNG_COMPRESSION);
compression_params.push_back(9);
bool result = false;
try
{
result = imwrite("alpha.png", mat, compression_params);
}
catch (const cv::Exception& ex)
{
fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
}
if (result)
printf("Saved PNG file with alpha data.\n");
else
printf("ERROR: Can't save PNG file.\n");
vector<Mat> imgs;
imgs.push_back(mat);
imgs.push_back(~mat);
imgs.push_back(mat(Rect(0, 0, mat.cols / 2, mat.rows / 2)));
imwrite("test.tiff", imgs);
printf("Multiple files saved in test.tiff\n");
return result ? 0 : 1;
}| filename | Name of the file. |
| img | (Mat or vector of Mat) Image or Images to be saved. |
| params | Format-specific parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, ... .) see cv::ImwriteFlags |
| params | Optional format-specific parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, ...). These parameters are used to specify additional options for the encoding process. Refer to cv::ImwriteFlags for details on possible parameters. |
| metadata |
| Vector of vectors or vector of matrices with chunks of metadata to store into the file |
| params | Format-specific parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, ... .) see cv::ImwriteFlags |