1. C/C++

OpenCV-第二话-图像 加载 显示 修改 保存

一.加载

OpenCV加载图像使用 cv::imread()方法,定义如下:

CV_EXPORTS_W Mat imread( const String& filename, int flags = IMREAD_COLOR );

第一个参数为文件名,第二个参数为加载标志,可以选择

IMREAD_UNCHANGED(<0)表示加载原图,不做任何改变

IMREAD_GRASYCALE(0)表示把图片作为灰度图像加载进来

IMREAD_COLOR(>0)表示把原图像作为RGB图像加载进来

注意:OpenCV支持JPG,PNG,TIGG等常见格式图像文件加载!

二.显示

OpenCV显示图像使用cv::namedWindows()方法和cv::imshow()方法

namedWindows()的功能是创建一个OpenCV窗口,它由OpenCV创建与释放,我们不需要手动销毁它。

CV_EXPORTS_W void namedWindow(const String& winname, int flags = WINDOW_AUTOSIZE);

/** @brief Destroys the specified window.

The function destroyWindow destroys the window with the given name.

@param winname Name of the window to be destroyed.
 */

第一个参数为窗口名称

第二个参数为窗口标志:

WINDOW_AUTOSIZE(根据图像大小自动显示窗口大小,不能手动改变敞口大小)

WINDOW_NORMAL(和Qt集成的时候使用,允许改变窗口大小)

imshow()的功能是将图像显示到对应的窗口上。

CV_EXPORTS_W void imshow(const String& winname, InputArray mat);

/** @brief Resizes window to the specified size

@note

-   The specified window size is for the image area. Toolbars are not counted.
-   Only windows created without cv::WINDOW_AUTOSIZE flag can be resized.

@param winname Window name.
@param width The new window width.
@param height The new window height.
 */

三.修改

OpenCV修改图像使用cv::cvtColor()方法,可以对图像的色彩空间进行变换,即从一个色彩空间变换到另一个色彩空间。

CV_EXPORTS_W void cvtColor( InputArray src, OutputArray dst, int code, int dstCn = 0 );

/** @brief Converts an image from one color space to another where the source image is
stored in two planes.

This function only supports YUV420 to RGB conversion as of now.

@param src1: 8-bit image (#CV_8U) of the Y plane.
@param src2: image containing interleaved U/V plane.
@param dst: output image.
@param code: Specifies the type of conversion. It can take any of the following values:
- #COLOR_YUV2BGR_NV12
- #COLOR_YUV2RGB_NV12
- #COLOR_YUV2BGRA_NV12
- #COLOR_YUV2RGBA_NV12
- #COLOR_YUV2BGR_NV21
- #COLOR_YUV2RGB_NV21
- #COLOR_YUV2BGRA_NV21
- #COLOR_YUV2RGBA_NV21
*/

因为如果要修改图片的某些属性则需要将其转换到特定的色彩空间上才能方便的进行操作。

四.保存

OpenCV保存图片可以使用cv::imwrite()方法

CV_EXPORTS_W bool imwrite( const String& filename, InputArray img,
              const std::vector<int>& params = std::vector<int>());

/** @brief 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.

@note In the case of color images, the decoded images will have the channels stored in **B G R** order.
@param buf Input array or vector of bytes.
@param flags The same flags as in cv::imread, see cv::ImreadModes.
*/

可以保存图片到指定路径

只有八位,十六位的PNG,JPG,TIFF文件格式且是单通道或者三通道的BGR的图像才能通过这种方式保存

保存PNG格式的时候可以保存透明通道的图片

可以指定压缩参数

 

Example:

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

int main()
{
	Mat image = imread("D:/go.png", IMREAD_COLOR); // Read the file
	if (image.empty()) // Check for invalid input
	{
		cout << "Could not open or find the image" << std::endl;
		return -1;
	}
	namedWindow("Display window1", WINDOW_AUTOSIZE); // Create a window for display.
	namedWindow("Display window2", WINDOW_AUTOSIZE);
	Mat output;
	cvtColor(image, output, CV_BGR2GRAY);//RGB to GRAY
	imshow("Display window1", image);
	imshow("Display window2", output); // Show our image inside it.
	imwrite("D:/output.png", output);//save img
	waitKey(0); // Wait for a keystroke in the window
	return 0;
}