出于个人兴趣 学习一下opencv(一)

由于lab的情况略让人头痛,以及偶然,我决定学习一下opencv,一方面作为动手练习,一方面了解下图形图像处理方面的知识。


安装什么的就不写了 网上太多 这里环境是vs2010+opencv2.47 语言使用C++

结构:

opencv主要由以下几个模块构成:cxcore, cv, ml(machine learning), highgui, cvaux


cxcore.h 文件中包含的主要内容是一些基础的类定义, 比如CvPoint, CvSize, cvCreateImage, cvCreateMat等。

cv.h文件主要是用作基础的图像处理用,还有运动分析,目标跟踪,模式识别,三维重建等 这个文件比较重要。

ml.h 包含的是机器学习里面的一些算法实现

highgui.h 主要是做界面,读写视频用

cvaux.h 里面是一些要被淘汰的算法


我们主要用到前四个文件

第一个程序:以原始比例显示一张图片

#include <iostream>
#include <opencv\cv.h>
#include <opencv\cxcore.h>
#include <opencv\highgui.h>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
	const char* imagename = "test.jpg";  
	Mat img = imread(imagename); 
	namedWindow("image", CV_WINDOW_AUTOSIZE);   
	imshow("image", img); 
	waitKey();  
	return 0;
}



 读取图片的函数imread定义在highgui.h中  CV_EXPORTS_W Mat imread( const string& filename, int flags=1 );

flag = 0 表示以灰度图像显示 flag > 0 表示3通道彩色图像 flag < 0 表示原始图像

具体参见http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#Mat imread(const string& filename, int flags)

Parameters:
  • filename – Name of file to be loaded.
  • flags –

    Flags specifying the color type of a loaded image:

    • CV_LOAD_IMAGE_ANYDEPTH - If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
    • CV_LOAD_IMAGE_COLOR - If set, always convert image to the color one
    • CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one
    • >0 Return a 3-channel color image.

      Note

       

      In the current implementation the alpha channel, if any, is stripped from the output image. Use negative value if you need the alpha channel.

    • =0 Return a grayscale image.
    • <0 Return the loaded image as is (with alpha channel).

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)
  • Portable image format - *.pbm, *.pgm, *.ppm (always supported)
  • Sun rasters - *.sr, *.ras (always supported)
  • TIFF files - *.tiff, *.tif (see the Notes section)
这里需要把test.jpg放到工程文件中才能正确读取,并且没考虑读取错误的问题

 然后说下nameWindow函数,就是显示一个窗口 用来展示图片 两个参数第一个用作窗口的标志(字符串),如果有相同名字的窗口,那这个函数什么也不做

第二个参数表示要怎么样的显示窗口 具体参照上述地址可以查找api

imShow函数是用来显示图像第一个参数指定在哪个窗口上显示图像(字符串作为标志)

waitKey函数是用作使程序暂停等待键盘输入 可以添加参数 以毫秒为单位,表示等待时间 以便看到图像,类似于避免控制台一闪而过那个功能

好像新版的opencv能自动释放资源 嗯 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值