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

新手学习opencv三:最最简单的图片浏览 新手学习opencv最最简单的图片浏览 阅读详情

代码如下:

#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
winnameName of the window.
matImage 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
delayDelay in milliseconds. 0 is the special value that means "forever".

The function waitKey waits for a key event infinitely (when

打开BMP图片的源代码 数字图像获取处理及实践应用源代码\ImageProcessing\cdib.cpp ................................\...............\cdib.h ................................\...............\ChildFrm.cpp ................................\...............\ChildFrm.h ................................\...............\ColorTable.h ................................\...............\Default.SUP ................................\...............\DIBPrcs.cpp ................................\...............\DibShow.cpp ................................\...............\DlgAftReg.cpp ................................\...............\DlgAftReg.h ................................\...............\DlgArith.cpp ................................\...............\DlgBitPlane.cpp ................................\...............\DlgCoding.h ................................\...............\DlgCodingHuffman.cpp ................................\...............\DlgEhnLinTrans.cpp ................................\...............\DlgEhnLinTrans.h ................................\...............\DlgEnhColor.cpp ................................\...............\DlgEnhColor.h ................................\...............\DlgHistShow.cpp ................................\...............\DlgHistShow.h ................................\...............\DlgHistShow1.cpp ................................\...............\DlgHistShow1.h ................................\...............\DlgHuffman.cpp ................................\...............\DlgMedian.cpp ................................\...............\DlgMedian.h ................................\...............\DlgRecMatch.cpp ................................\...............\DlgRecMatch.h ................................\...............\DlgReg.cpp ................................\...............\DlgReg.h ................................\...............\DlgShannon.cpp ................................\...............\DlgSmooth.cpp ................................\...............\DlgSmooth.h ................................\...............\DWT.CPP ................................\...............\Enhance.cpp ................................\...............\FreTrans.cpp ................................\...............\GlobalApi.h ................................\...............\ImageAnalysis.cpp ................................\...............\ImageCoding.cpp ................................\...............\ImageProcessing.aps ................................\...............\ImageProcessing.clw ................................\...............\ImageProcessing.cpp ................................\...............\ImageProcessing.dsp ................................\...............\ImageProcessing.dsw ................................\...............\ImageProcessing.h ................................\...............\ImageProcessing.ncb ................................\...............\ImageProcessing.opt ................................\...............\ImageProcessing.plg ................................\...............\ImageProcessing.rc ................................\...............\ImageProcessingDoc.cpp ................................\...............\ImageProcessingDoc.h ................................\...............\ImageProcessingView.cpp ................................\...............\ImageProcessingView.h ................................\...............\ImageView.cpp ................................\...............\MainFrm.cpp ................................\...............\MainFrm.h ................................\...............\Motion.cpp ................................\...............\ReadMe.txt ................................\...............\Recog.asp ................................\...............\Recog.cpp ................................\...............\res\ImageProcessing.ico ................................\...............\...\ImageProcessing.rc2 ................................\...............\...\ImageProcessingDoc.ico ................................\...............\...\Toolbar.bmp ................................\...............\resource.h ................................\...............\restore.cpp ................................\...............\SegApi.cpp ................................\...............\StdAfx.cpp ................................\...............\StdAfx.h ................................\lena.bmp ................................\ImageProcessing\Debug ................................\...............\Release ................................\...............\res ................................\ImageProcessing 立即下载

相关推荐

基于图像分割的LIDC-IDRI 数据集预处理之数据下载篇

近期接触到了一个关于图像分割实验项目,关于数据预处理部分在此予以记录,和大家分享。

qq_67696694的博客 1904

vscode中python报错:找不到文件

文件路径出错 在python自带的ide里打开某个文件,文件路径就是文件名 在vscode里打开某个文件,文件路径应为绝对路径或相对路径

Khazking的博客 5065

1.安装EMQX服务器

阿里云;EMQX

hfh8130的博客 5311

because file is missing, has improper permissions, or is an unsupported or invalid format报错

使用pyaotugui 定位指定的图片报错。原因是图片路径中有汉字,重新设置路径为纯字母的就可以解决问题。

ahxttt的博客 2181

Theil-Sen Median斜率估计和Mann-Kendall趋势分析:以多年NPP数据为例

一、理论 Theil-Sen Median方法又称为Sen斜率估计,是一种稳健的非参数统计的趋势计算方法。该方法计算效率高,对于测量误差和利群数据不敏感,适用于长时间序列数据的趋势分析。其计算公式为: Mann-Kendall(MK)检验是一种非参数的时间序列趋势性检验方法,其不需要测量值服从正太分布,不受缺失值和异常值的影响,适用于长时间序列数据的趋势显著检验。其过程如下:对 于 序 列Xt = x1,x2,⋯xn,先确定所有对偶值( xi , xj ,...

丑唐的修炼之旅 8万+

深入MoveIt与OMPL集成开发:从源码编译到自定义规划算法实战

本文详细介绍了如何从源码编译集成MoveIt与OMPL,并实现自定义规划算法的完整流程。通过清理环境、获取源码、解决编译依赖与错误,最终指导开发者创建并集成名为MyRRT的自定义规划算法,从而实现对机器人运动规划栈的深度定制与优化。

h0i1j2k3l的博客 468

RF踩坑之调用pyautogui鼠标操作报错

OSError: Failed to read 938 because file is missing, has improper permissions, or is an unsupported or invalid format

weixin_39691472的博客 2530

集成calibre到cadence virtuoso环境中 load: can‘t access file - “/opt/eda/Mentor/lib/calibre.skl“

项目场景: 设想是将Calibre集成到cadence virtuoso中,但网上的解决办法尝试过在本机上实验都有点问题,因此想记录下来我的解决办法仅供参考。 问题描述: 安装好cadence, calibre之后打开virtuoso,CIW窗口显示 *Error* load: can't access file - "/opt/eda/Mentor/lib/calibre.skl" 问题是出在calibre.skl的加载路径出错,尝试网上的在工程目录下修改.cdsinit文件,添加calibre安装

weixin_52714276的博客 1万+

博客专栏 >OpenCV学习笔记

共55篇博文 博客专栏  >  编程语言   >  OpenCV学习笔记 OpenCV学习笔记(十六)——CamShift研究 CamShitf算法,即Continuously Apative Mean-Shift算法,基本思想就是对视频图像的多帧进行MeanShift运算,将上一帧结果作为下一帧的初始值,迭代下去。基本步骤为... 2011-10-31 20:57 1019

autumn20080101的专栏 324

xlinx项目系列之基于zynq-7020ISP图像处理

本Demo基于zynq-7020(正点原子启明星+OV5640+4.3'800x480)实现了ISP图像处理(将ov5640的isp关闭,在7020上实现ISP功能),输出到LCD和HDMI,软件基于SDK裸机开发,2592x1944@15fp。位置: xil_ip_repo/xil_isp_lite_1.0。项目获取链接:2.基于zynq-7020实现ISP图像处理。位置: xil_ip_repo/xil_vip_1.0。

JY6669991010的博客 2637

混合整数规划(MIP)在资源优化中的实战应用

本文深入探讨了混合整数规划(MIP)在资源优化中的实战应用,从菜市场采购到工厂调度,再到物流配送和钢铁厂切割问题。通过分支定界法和割平面法等技术,MIP能够有效解决包含整数和连续变量的复杂优化问题,提升企业运营效率。文章还介绍了现代求解器的优化策略和实际项目中的避坑指南,帮助读者更好地应用MIP技术。

weixin_30500105的博客 91

(九)算法(02):机器视觉定位,基于OpenCV的NCC模板匹配方法

C# +OpenCV+海康相机联调方法

Bishop077的博客 339

【Segment Anything Model】十(1):利用SAM做分类任务,方法二,不放过SAM强大特征提取能力,将SAM提取到的ImageEmbedding插入到其他cnn网络中

直接黏贴复制就好'''该类定义了VGG-16的基本结构。features: 代表VGG网络的主体部分,即所有的卷积层和池化层。avgpool: 是自适应平均池化层,将特征图大小调整为7x7。classifier: 是VGG网络的分类部分,由全连接层组成。_initialize_weights(): 初始化权重的方法。

cvxiayixiao的博客 3563

opencv的图像载入、显示和输出

1、关于opencv的命名空间       opencv中的C++类或者函数类都是定义在命名空间cv中的,两种方法可以访问,一种是在代码开头位置加上using namespace cv; 另外一种是在使用函数时加cv::. 2、关于Mat类型       Mat类是用于保存图像以及其他矩阵数据的数据结构。 3、图像的读入与显示       3.1    imread函数

longwinyang的博客 3423

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

color属性 python_Python cv2.CV_LOAD_IMAGE_COLOR属性代码示例

# 需要导入模块: import cv2 [as 别名]# 或者: from cv2 import CV_LOAD_IMAGE_COLOR [as 别名]def _load_cv2(img, grayscale=None):"""TODO"""# load images if given filename, or convert as needed to opencv# Alpha layer j...

weixin_29313547的博客 1568

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

Robot Framework 基于图像识别的 C/S 自动化测试 --- 从入门到实战

引子: 由于最近有一个分量很重的windows Client需要测试,所以开始研究C/S的自动化。 其实一开始知道这个客户端是基于Node.js,并且使用Chromium内核,我是想使用Selenium的Webdriver驱动的。但是经过各种努力,发现无法突破第三方给Chromium自定义套的壳,且内核是不完整chromium,遂放弃。 也考虑过直接使用AutoIt来操作windows窗口,无奈在这种套壳Chromium下,窗口对象都是一团,无法操作。 也考虑过Win...

qq_27534003的博客 6784

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

OpenCV学习笔记(六):imread介绍

内容来源:http://docs.opencv.org/3.0.0/d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56 Mat cv::imread ( const String &  filename,     int  flags = IMREAD_COLOR 

dhj555的博客 2万+

深度图像帧差法处理以及CV16UC1深度图像的存储方式

最近为了拾取桌面上的任务目标,采用了实时图像与背景图像相减的方法来进行,因为采用彩色图像相减会有影子的干扰,所以采用了深度图像。 但是深度图像的Mat是CV16UC1格式的,矩阵内部数据采用uint_16格式存储,开始我用imwrite保存了png格式的深度图像并读取,但是在后续处理中发现没有起到差值的效果,经过各种折腾,终于发现问题在于png格式最多就是uint_8的数据存储格式,所以导致读取...

跃动的风的博客 9098

OpenCV——保存图片和视频

保存图片 imwrite bool cv :: imwrite(const String& filename,InputArray img,Const std::vector<int>& params = std::vector<int>()) shi

小铅笔 1609

opencv里可以调整大小的画图窗口: imshow, namedwindow

如果能鼠标拖动改变窗口大小在调试程序时候会方便很多啊~ opencv 画图可以 直接 cv::imshow("camera", frame); 会创建一个命名为“camera”的窗口。 但是默认的创建窗口的namedWindow参数为WINDOW_AUTOSIZE。窗口大小会自动调整以适应所显示的图像,但是不能更改大小。 创建窗口时候改变下参数就可以鼠标随意拖动窗口改变大

wuyoy520 的博客 9万+

使用OpenCV进行人脸识别的三种方法

OpenCV从版本2.4开始,加入了一个类FaceRecognizer,使用它可以方便的地进行人脸识别(源代码,在OpenCV的opencv\modules\contrib\doc\facerec\src下)。 目前支持三种算法(BSD license): Eigenfaces特征脸: createEigenFaceRecognizer() Fisherfaces: createFisherFaceRecognizer() LocalBinary Patterns Histograms(局部二值直方图):c

凌风探梅的专栏 2万+
上一篇: 2016各大公司校招薪水曝光:年薪28万,这只是零花钱
下一篇: OpenCV图像数据访问,查询表和时间消耗测试
凌风探梅
博客等级 码龄19年 2575粉丝 113原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值