图像的打开、修改、显示和保存示例(>OpenCV 2.0)

2023-11-19

代码如下:

#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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

  • [VScode]终端回应“pnpm : 无法将“pnpm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。“解决思路

    问题概述 遇到问题 在VScode终端输入pnpm install有错误提示 pnpm 无法将 pnpm 项识别为 cmdlet 函数 脚本文件或可运行程序的名称 请检查名称的拼写 如 果包括路径 请确保路径正确 然后再试一次 所在位置 行
  • wps表格中的文字不能顺利水平居中的解决办法

    今天调整别人做的表格 想要把表格中文字水平居中 竟然指令操作后发现很多行的表格无动于衷 发现这样设置一下应该可以解决 WPS文字右边有个小的下拉箭头 选择 格式 段落进行设置 段前段后设置为0 行距设置为单倍行距或者固定行距 固定行距需要测
  • maven私服不能重复部署解决方法

    maven私服不能重复部署解决 1 报错 Return code is 400 ReasonPhrase Repository does not allow updating assets maven releases 2 原因 经排查发现
  • Elasticsearch 7.x生产配置

    虽然Elasticsearch需要很少的配置 但是有一些设置需要手动配置 并且必须在进入生产之前进行配置 1 官方文档 这些重要配置说明 请参考官方文档 https www elastic co guide en elasticsearch
  • c++得到窗口句柄

    include
  • Gitlab Java API 使用示例(亲测、有效)

    提示 文章写完后 目录可以自动生成 如何生成可参考右边的帮助文档 文章目录 简介 一 依赖 常量 Maven依赖 定义常量类 二 增删改查 1 新增私有仓库 2 删除指定仓库 3 修改项目简介和是否开源 三 后续更新 简介 在开发中 偶尔会
  • 算法学习——动态规划的学习

    作者 小 琛 欢迎转载 请标明出处 引言 动态规划是算法中较难的内容 常常被用来区分学习者的档次 笔者也是在面试中发现了这个问题 故而回头认真学习并总结递归算法 希望能够帮助到阅读的你 文章目录 动态规划的思想 动态规划的具体实现步骤 入门
  • QThread介绍

    一 介绍 Qt中的QThread类提供了与平台无关的线程 一个QThread代表了一个在应用程序中可以独立控制的线程 它与进程中的其他线程分享数据 但是是独立执行的 相对于一般的程序都从main 函数开始执行 QThread从run 函数开
  • Q_D指针(d指针)和Q_Q指针(q指针)简介

    文章目录 1 Q D指针 2 Q Q指针 1 Q D指针 在class中配合使用 Q DECLARE PRIVATE 和 Q D 方便获取d指针 d指针指向Class Private 2 Q Q指针 在class Private配合使用 Q
  • 记录--Vue中如何导出excel表格

    这里给大家分享我在网上总结出来的一些知识 希望对大家有所帮助 一 导出静态数据 1 安装 vue json excel npm i vue json excel 注意 此插件对node有版本要求 安装失败检查一下报错是否由于node版本造成
  • hive、impala、prestoDB 优缺点对比

    hive 优点 缺点 被广泛应用 经受时间的考验 既然是基于Mapreduce 也拥有MapReduce所有缺点 包含昂贵的Shuffle操作和磁盘IO操作 运行在Mapreduce框架之上 hive仍然不支持多个reduce操作group
  • JavaScript系列——数组元素左右移动N位算法实现

    引言 在自己刚刚毕业不久的时候 去了一家公司面试 面试官现场考了我这道题 我记忆深刻 当时没有想到思路 毫无疑问被面试官当成菜鸟了 最近刚好在研究数组的各种算法实现 就想到这道题 可以拿来实现一下 纪念自己逝去的青春 需求 假设有这样一个数
  • stm32(十)滴答定时器

    1 系统滴答定时器的概述 滴答定时器又称 SysTick 有两个时钟源 一个为内部时钟 一个为外部时钟 滴答定时器是一个 24 位倒计 数的定时器 从预装载值一直到 0 重装载寄存器的值会自动装载到计数寄存器中 只要不把它使能位清除 那 么
  • 用过多款团队协作工具后,少数派为什么选择飞书

    飞书 字节跳动旗下SaaS应用 自诞生以来 受到了国内众多企业的青睐和关注 飞书也一直致力于为企业提供高效协作的解决方案 如今 飞书的解决方案已覆盖互联网 教育 媒体 法律 零售等行业 未来还将进一步扩展到更多行业 少数派 是媒体行业中最早
  • Java程序员必备的几款开发工具

    工欲善其事 必先利其器 作为一名优秀的Java程序员 怎能没有几款得心应手的高效开发工具呢 市面上类库 工具千千万 下面就给大家推荐几款高效的Java开发工具 1 UItraEdit UltraEdit是初学者们非常喜爱的一款开发工具 可以
  • 史上最全的MathCAD安装教程

    今天我们来安装一下MathCAD 今天我们安装的版本是PTC MathCAD Prime 5 0 Step 01 首先我们先来下载一下软件 提取码 nifp Step 02 软件下载好了之后如下图 Step 03 解压此文件 Step 04
  • vue的v-for循环普通数组、对象数组、对象、数字

    div span u 索引 i span br span u a 索引 i span div
  • JavaScript经典案例之按下拖拽、跟随鼠标移动

    div div

随机推荐