代码如下:

#include <opencv2/opencv.hpp>
using namespace cv;
int main( int argc, char** argv )
{char* imageName = argv[1];char* outName = argv[2];Mat image;image = imread( imageName, 1 );//打开if( argc != 2 || !image.data ){printf( " No image data \n " );return -1;}Mat gray_image;cvtColor( image, gray_image, COLOR_BGR2GRAY );//修改imwrite( outName, gray_image );//保存namedWindow( imageName, WINDOW_AUTOSIZE );namedWindow( "Gray image", WINDOW_AUTOSIZE );imshow( imageName, image );//显示imshow( "Gray image", gray_image );//显示waitKey(0);return 0;
}

1  cv::imread 读取

 Mat cv::imread (const String &  filename, int  flags = IMREAD_COLOR )

  • IMREAD_UNCHANGED (<0) 按照图像原有的模式打开 (包含alpha通道)
  • IMREAD_GRAYSCALE ( 0) 以灰度模式打开图像
  • IMREAD_COLOR (>0) 打开图像为 RGB通道模式

The function imread loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix (Mat::data==NULL ). Currently, the following file formats are supported:

  • Windows bitmaps - *.bmp, *.dib (always supported)
  • JPEG files - *.jpeg, *.jpg, *.jpe (see the Notes section)
  • JPEG 2000 files - *.jp2 (see the Notes section)
  • Portable Network Graphics - *.png (see the Notes section)
  • WebP - *.webp (see the Notes section)
  • Portable image format - *.pbm, *.pgm, *.ppm (always supported)
  • Sun rasters - *.sr, *.ras (always supported)
  • TIFF files - *.tiff, *.tif (see the Notes section)

2  cvtColor( image, gray_image, COLOR_BGR2GRAY );

  • a source image (image)
  • a destination image (gray_image), in which we will save the converted image.
  • an additional parameter that indicates what kind of transformation will be performed. In this case we useCOLOR_BGR2GRAY (because of cv::imread has BGR default channel order in case of color images).

void cv::cvtColor ( InputArray  src, OutputArray  dst, int  code, int  dstCn = 0 )

Converts an image from one color space to another.

The function converts an input image from one color space to another. In case of a transformation to-from RGB color space, the order of the channels should be specified explicitly (RGB or BGR). Note that the default color format in OpenCV is often referred to as RGB but it is actually BGR (the bytes are reversed). So the first byte in a standard (24-bit) color image will be an 8-bit Blue component, the second byte will be Green, and the third byte will be Red. The fourth, fifth, and sixth bytes would then be the second pixel (Blue, then Green, then Red), and so on.

The conventional ranges for R, G, and B channel values are:

  • 0 to 255 for CV_8U images
  • 0 to 65535 for CV_16U images
  • 0 to 1 for CV_32F images

In case of linear transformations, the range does not matter. But in case of a non-linear transformation, an input RGB image should be normalized to the proper value range to get the correct results, for example, for RGB→ L*u*v* transformation. For example, if you have a 32-bit floating-point image directly converted from an 8-bit image without any scaling, then it will have the 0..255 value range instead of 0..1 assumed by the function. So, before calling cvtColor , you need first to scale the image down:

img *= 1./255;
cvtColor(img, img,COLOR_BGR2Luv);

If you use cvtColor with 8-bit images, the conversion will have some information lost. For many applications, this will not be noticeable but it is recommended to use 32-bit images in applications that need the full range of colors or that convert an image before an operation and then convert back.

If conversion adds the alpha channel, its value will set to the maximum of corresponding channel range: 255 for CV_8U, 65535 for CV_16U, 1 for CV_32F.

3 cv::namedWindow

void cv::namedWindow ( const String &winname,int  flags = WINDOW_AUTOSIZE )

  • WINDOW_NORMAL If this is set, the user can resize the window (no constraint).
  • WINDOW_AUTOSIZE If this is set, the window size is automatically adjusted to fit the displayed image (see imshow ), and you cannot change the window size manually.
  • WINDOW_OPENGL If this is set, the window will be created with OpenGL support.

Qt backend supports additional flags:

  • CV_WINDOW_NORMAL or CV_WINDOW_AUTOSIZE: CV_WINDOW_NORMAL enables you to resize the window, whereas CV_WINDOW_AUTOSIZE adjusts automatically the window size to fit the displayed image (see imshow ), and you cannot change the window size manually.
  • CV_WINDOW_FREERATIO or CV_WINDOW_KEEPRATIO: CV_WINDOW_FREERATIO adjusts the image with no respect to its ratio, whereas CV_WINDOW_KEEPRATIO keeps the image ratio.
  • CV_GUI_NORMAL or CV_GUI_EXPANDED: CV_GUI_NORMAL is the old way to draw the window without statusbar and toolbar, whereas CV_GUI_EXPANDED is a new enhanced GUI. By default, flags == CV_WINDOW_AUTOSIZE | CV_WINDOW_KEEPRATIO | CV_GUI_EXPANDED

4 cv::imshow

void cv::imshow ( const String &winname, InputArray  mat )

Displays an image in the specified window.

Parameters
winname Name of the window.
mat Image to be shown.

The function imshow displays an image in the specified window. If the window was created with the CV_WINDOW_AUTOSIZE flag, the image is shown with its original size, however it is still limited by the screen resolution. Otherwise, the image is scaled to fit the window. The function may scale the image, depending on its depth:

  • If the image is 8-bit unsigned, it is displayed as is.
  • If the image is 16-bit unsigned or 32-bit integer, the pixels are divided by 256. That is, the value range [0,255*256] is mapped to [0,255].
  • If the image is 32-bit floating-point, the pixel values are multiplied by 255. That is, the value range [0,1] is mapped to [0,255].

If window was created with OpenGL support, imshow also support ogl::Buffer , ogl::Texture2D and cuda::GpuMat as input.

If the window was not created before this function, it is assumed creating a window with CV_WINDOW_AUTOSIZE.

If you need to show an image that is bigger than the screen resolution, you will need to call namedWindow("", WINDOW_NORMAL) before the imshow.

Note
This function should be followed by waitKey function which displays the image for specified milliseconds. Otherwise, it won't display the image. For example, waitKey(0) will display the window infinitely until any keypress (it is suitable for image display). waitKey(25) will display a frame for 25 ms, after which display will be automatically closed. (If you put it in a loop to read videos, it will display the video frame-by-frame)

5 waitKey(0)

int cv::waitKey ( int  delay = 0)

Waits for a pressed key.

Parameters
delay Delay in milliseconds. 0 is the special value that means "forever".

The function waitKey waits for a key event infinitely (when

图像的打开、修改、显示和保存示例(OpenCV 2.0)相关推荐

  1. 结合CDIB类,对图像的打开、显示、保存

    需要先建一个显示类,这个显示类的基类我选的是CStatic,以下代码均放在这个显示类中,另外需在这个类的.h文件中添加CDib m_CDib; 另外,CDIB类文件需要自己添加. 1.图像的打开: v ...

  2. C++ openCV 图像的读取、显示、保存、加权融合、改变对比度、修改色域

    文章目录 图像的读取.显示.保存 图像的读取 图像的显示 图像的保存 图像的属性修改 图像的色域修改 图像的对比度.数据类型修改 两张图像的加权融合 图像的读取.显示.保存 使用的命名空间 #prag ...

  3. OpenCV之Python学习笔记(1)(2): 图像的载入、显示和保存 图像元素的访问、通道分离与合并

    OpenCV之Python学习笔记 一直都在用Python+OpenCV做一些算法的原型.本来想留下发布一些文章的,可是整理一下就有点无奈了,都是写零散不成系统的小片段.现在看到一本国外的新书< ...

  4. 【python图像处理】图像的读取、显示与保存

    python作为机器学习和图像处理的利器,收到越来越多的推崇,特别是在图像处理领域,越来越多的研究和开发开始转向使用python语言,下面就介绍python图像处理中最基本的操作,即图像的读取显示与保 ...

  5. [转载] OpenCV-Python 图像处理(二):图像的读取、显示与保存

    参考链接: Python OpenCV 基础 2 : imwrite 保存图像 说明: 本系列主要是学习OpenCV-Python文档的个人笔记.很少有理论的叙述,都是函数名.参数描述.作用.应用场景 ...

  6. python中cv2、Image和plt的图片打开、显示和保存

    使用cv2.Image.plt打开.显示和保存图片 文章目录 使用cv2.Image.plt打开.显示和保存图片 0.读取图片 1.使用cv2显示cv2打开的图片 2.使用cv2显示Image打开的图 ...

  7. opencv-python之图像的读取、显示与保存

    OpenCV图像基本处理 图像基本处理 读取图像 显示图像 建立窗口 显示图片 等待按键 销毁窗口 保存图像 图像基本处理 读取图像 import cv2cv2.imread(filename[, f ...

  8. 深度图像+rgb转化点云数据、点云数据打开、显示以及保存

    头文件 #include<iostream> #include <fstream> #include <stdio.h> #include <string.h ...

  9. 图像的读取,显示与保存(基于skimage模块)

    一 skiamge模块 skimage包的全称是scikit-image SciKit (toolkit for SciPy) ,它对scipy.ndimage进行了扩展,提供了更多的图片处理功能.它 ...

最新文章

  1. [USACO07JAN]平衡的阵容Balanced Lineup BZOJ 1699
  2. reflow 和 repaint
  3. shell实例第6讲:检查主机存活状态
  4. 20175221 MyCP(课下作业,必做)
  5. Java开发环境的搭建(JDK和Eclipse的安装)
  6. 如果 “ 2X ”的补码是“ 90H ”,那么 X 的真值是( )。
  7. 【两种解法】he Falling Leaves UVA - 699
  8. Java同步锁——lock与synchronized 的区别【转】
  9. 一文读懂云上DevOps能力体系
  10. IntelliJ IDEA:文件的路径本该是”\“,却变成了”¥“
  11. 为什么成员属性不会被重写
  12. 测试集的准确率为什么高于训练集的准确率?
  13. python的难点是什么,【python基础学习】基础重点难点知识汇总
  14. android智能电视直播源抓取教程,求人不如求己,教你自己抓取直播源的方法!...
  15. android10下载更新功能,Android 10部分新功能曝光 感觉越来越暗黑
  16. 基于DenseNet的图像识别
  17. 第五节 电阻分压 蓦然回首,那人却在,灯火阑珊处
  18. 兆,字节,位等单位转换
  19. java多线程问题(代码示例)
  20. Scikit-Learn机器学习(knn算法)

热门文章

  1. C/C++函数形参传实参时值传递、指针传递、引用传递的区别
  2. flannel源码分析--LookupExtIface
  3. leetcode算法题--下降路径最小和
  4. leetcode算法题--最长公共子数组
  5. 中盐总公司:盐业公司24小时配送保供应
  6. Django MTV结构分析
  7. python 三元表达式、列表推导式、生成器表达式、递归、匿名函数、内置函数
  8. 数据科学家最常用的10种算法
  9. ODAC(V9.5.15) 学习笔记(十七)主从模式
  10. Codeforces #264 (Div. 2) D. Gargari and Permutations