Load, Modify, and Save an Image - 加载、修改和保存图像

OpenCV 4.2.0 - Modules
https://docs.opencv.org/4.2.0/index.html

OpenCV 4.2.0 - Tutorials
https://docs.opencv.org/4.2.0/d9/df8/tutorial_root.html

0. Load, Modify, and Save an Image

https://docs.opencv.org/4.2.0/db/d64/tutorial_load_save_image.html
OpenCV modules -> OpenCV Tutorials -> Introduction to OpenCV -> Load, Modify, and Save an Image

1. Goals

Load an image using cv::imread
Transform an image from BGR to Grayscale format by using cv::cvtColor
Save your transformed image in a file on disk (using cv::imwrite)

2. Code

//============================================================================
// Name        : using namespace cv;
// Author      : Yongqiang Cheng
// Version     : Feb 22, 2020
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================#include <opencv2/opencv.hpp>using namespace cv;int main(int argc, char** argv)
{char* imageName = argv[1];Mat image;image = imread(imageName, IMREAD_COLOR);if (argc != 2 || !image.data){printf(" No image data \n ");return -1;}Mat gray_image;cvtColor(image, gray_image, COLOR_BGR2GRAY);imwrite("../images/Gray_Image.jpg", gray_image);namedWindow(imageName, WINDOW_AUTOSIZE);namedWindow("Gray image", WINDOW_AUTOSIZE);imshow(imageName, image);imshow("Gray image", gray_image);waitKey(0);return 0;
}

3. Explanation

  1. We begin by loading an image using cv::imread, located in the path given by imageName. For this example, assume you are loading a BGR image.
    对于此示例,假设您正在加载 BGR 图像。

  2. Now we are going to convert our image from BGR to Grayscale format. OpenCV has a really nice function to do this kind of transformations:
    cvtColor( image, gray_image, COLOR_BGR2GRAY );
    As you can see, cv::cvtColor takes as arguments:

  • 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 use COLOR_BGR2GRAY (because of cv::imread has BGR default channel order in case of color images).
  1. So now we have our new gray_image and want to save it on disk (otherwise it will get lost after the program ends). To save it, we will use a function analogous to cv::imread : cv::imwrite
    因此,现在我们有了新的 gray_image,并希望将其保存在磁盘上 (否则它将在程序结束后丢失)。
    imwrite( "../../images/Gray_Image.jpg", gray_image );
    Which will save our gray_image as Gray_Image.jpg in the folder images located two levels up of my current location.
    这会将我们的 gray_image 保存为 Gray_Image.jpg 在我当前位置上两层的文件夹图像中。

  2. Finally, let’s check out the images. We create two windows and use them to show the original image as well as the new one:
    namedWindow( imageName, WINDOW_AUTOSIZE );
    namedWindow( "Gray image", WINDOW_AUTOSIZE );
    imshow( imageName, image );
    imshow( "Gray image", gray_image );

  3. Add the waitKey(0) function call for the program to wait forever for an user key press.
    添加 waitKey(0) 函数调用,使程序永远等待用户按下按键。

4. Result

When you run your program you should get something like this:
当您运行程序时,应该得到如下信息:

17:39:13 **** Incremental Build of configuration Debug for project DisplayImage ****
make all
Building file: ../src/DisplayImage.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -I/usr/local/include -I/usr/local/include/opencv4 -I/usr/local/include/opencv4/opencv2 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/DisplayImage.d" -MT"src/DisplayImage.o" -o "src/DisplayImage.o" "../src/DisplayImage.cpp"
Finished building: ../src/DisplayImage.cppBuilding target: DisplayImage
Invoking: GCC C++ Linker
g++ -L/usr/local/lib -o "DisplayImage"  ./src/DisplayImage.o   -lopencv_core -lopencv_video -lopencv_ml -lopencv_imgproc -lopencv_img_hash -lopencv_flann -lopencv_features2d -lopencv_calib3d -lopencv_dnn -lopencv_dnn_objdetect -lopencv_cvv -lopencv_text -lopencv_datasets -lopencv_aruco -lopencv_bgsegm -lopencv_shape -lopencv_imgcodecs -lopencv_videoio -lopencv_highgui -lopencv_bioinspired
Finished building target: DisplayImage17:39:16 Build Finished (took 2s.982ms)

And if you check in your folder (images), you should have a newly .jpg file named Gray_Image.jpg:

strong@foreverstrong:~/eclipse-work/DisplayImage/Debug$ ls -l ./DisplayImage
-rwxrwxr-x 1 strong strong 730488 Feb 23 17:39 ./DisplayImage
strong@foreverstrong:~/eclipse-work/DisplayImage/Debug$
strong@foreverstrong:~/eclipse-work/DisplayImage/Debug$ ./DisplayImage ../images/person.jpg
QXcbConnection: XCB error: 148 (Unknown), sequence: 172, resource id: 0, major code: 140 (Unknown), minor code: 20
strong@foreverstrong:~/eclipse-work/DisplayImage/Debug$



Congratulations, you are done with this tutorial!

Load, Modify, and Save an Image - 加载、修改和保存图像相关推荐

  1. 【OpenCV学习笔记之一】图像加载,修改及保存

    加载图像(用cv::imread) imread功能是加载图像文件成为一个Mat对象 其中第一个参数表示图像文件名称 第二个参数 表示加载的图像是什么类型 支持常见的三个参数值 IMREAD_UNCH ...

  2. [OpenCV学习笔记3][图像的加载+修改+显示+保存]

    正式进入OpenCV学习了,前面开始的都是一些环境搭建和准备工作,对一些数据结构的认识主要是Mat类的认识: [1.学习目标] 图像的加载:imread() 图像的修改:cvtColor() 图像的显 ...

  3. python数据批量写入iq数据库_通过Load table命令将数据文件加载到Sybase IQ数据库里面的Python脚本...

    CREATE TABLE poc_app.sys_ftp_cfg ( ftp_id              varchar(100) NOT NULL,          --话单文件名标记 ftp ...

  4. 【opencv4】opencv视频教程 C++(opencv教程)2、加载imread()(以灰度加载),修改,保存图像

    上一讲:[opencv4]opencv视频教程 C++(opencv教程)1.opencv介绍和环境搭建 下一讲:[opencv4]opencv视频教程 C++(opencv教程)3.矩阵的掩膜操作( ...

  5. opencv imread后做resizie_opencv第1课-加载、修改、保存图像

    (非原创,看课程自己做的笔记,防丢失放到个人空间的) 第1课-加载.修改.保存图像 加载图像(cv::imread) 修改图像(cv::cvtColor) 保存图像(cv::imwrite) 代码演示 ...

  6. iframe异步加载_5种延迟加载图像的方法以帮助你提升网站性能与用户体验

    英文 | https://www.sitepoint.com/five-techniques-lazy-load-images-website-performance/翻译 | web前端开发(ID: ...

  7. php 文字图片怎么保存为图片,php技术实现加载字体并保存成图片

    下面通过一段代码给大家详解介绍下php技术实现加载字体并保存成图片. // Set the content-type header("Content-type: image/png" ...

  8. cascader 动态加载 回显_Elementui cascader 级联选择器 动态加载数据,保存后回显的问题...

    问题描述 使用elementui,进行地区选择,分省市区三级动态加载,首次保存后,再次回看数据,怎么选中上次保存的地区 省市区三级都是动态加载,下次回来,只有省一级数据,model里面存的是一个数组, ...

  9. java 显示网页gif_Java在窗口上加载显示GIF动画图像

    Java在窗口上加载显示GIF动画图像,将多个独立的GIF图像串联在一起显示,形成GIF特有的动画形式.主要代码如下: ImageIcon[] images; //用于动画的图标数组 Timer an ...

最新文章

  1. ASP.NET中移除全部缓存
  2. Windows CE设备驱动开发之电源管理
  3. C++ 类对象作为类成员
  4. Java 整数型的进制间的互相转换
  5. 后台无刷新修改字段js
  6. C/C++高级算法之绘制曼德布洛特集
  7. webRTC之[chromium-style] virtual methods with non-empty bodies shouldnt be declared inline(二十一)
  8. 短信拦截马”黑色产业链与溯源取证研究
  9. 数据治理方案技术调研 Atlas VS Datahub VS Amundsen
  10. idea安装axios
  11. 12864液晶中文资料JHD529m1
  12. java电力巡检项目讲解,电力巡检-东软平台产品官网
  13. 难倒高手了,c语言枚举end的作用是什么?
  14. 可信安全网络 —— 安全左移之DDoS对抗
  15. elasticsearch篇之mapping
  16. 排序相关算法在计算机程序设计竞赛中的研究
  17. MaxIO智能缓存加速技术
  18. 设计模式(四)行为型模式介绍及实例(上)
  19. vue整合uniapp_uni-app仿微信App界面|vue+uniapp聊天室|仿微信朋友圈
  20. 除了we tool还有哪些免费安全好用的微信群发软件?这两个软件比we tool好用!

热门文章

  1. CSS 列表样式 (ul)
  2. git hub寻找资源
  3. MTBF平均故障间隔时间
  4. MC34063+MOSFET扩流 12V-5V 折腾出了高效率电路(转)
  5. 关于PDF嵌入背景图的实现
  6. mybatis generator 自动生成 在线生成器 生成service controller 含基础增删改查 自动生成工具 只需要建表SQL语句
  7. 计算机休眠后黑屏打不开,电脑待机后黑屏打不开怎么办
  8. 系统对接方案_报销费控SaaS对接财务系统解决方案
  9. 苹果手机home键在哪里_苹果手机为什么没有返回键? 原来隐藏着更好的方法, 涨知识了...
  10. Spark SQL中StructField和StructType