Opencv笔记——图像的部分基本操作(待补充)

参考网址:

http://docs.opencv.org/modules/core/doc/basic_structures.html

http://docs.opencv.org/doc/user_guide/ug_mat.html

http://docs.opencv.org/trunk/opencv_cheatsheet.pdf


一、基本数据

在存取图像数据前,首先我们有必要了解OpenCV中定义的类Vec

template<typename _Tp, int n> class Vec : public Matx<_Tp, n, 1> {...};

typedef Vec<uchar, 2> Vec2b;
typedef Vec<uchar, 3> Vec3b;
typedef Vec<uchar, 4> Vec4b;

typedef Vec<short, 2> Vec2s;
typedef Vec<short, 3> Vec3s;
typedef Vec<short, 4> Vec4s;

typedef Vec<int, 2> Vec2i;
typedef Vec<int, 3> Vec3i;
typedef Vec<int, 4> Vec4i;

typedef Vec<float, 2> Vec2f;
typedef Vec<float, 3> Vec3f;
typedef Vec<float, 4> Vec4f;
typedef Vec<float, 6> Vec6f;

typedef Vec<double, 2> Vec2d;
typedef Vec<double, 3> Vec3d;
typedef Vec<double, 4> Vec4d;
typedef Vec<double, 6> Vec6d;

二,加载图像

加载图像

Mat img = imread(filename)
imread默认是读取图像文件,创建3通道图像数据。如果要创建单通道灰度图像,可用如下命令:

Mat img = imread(filename,0)

三、访问图像象元

访问图像数据时,需要特别注意数据类型。

对于单通道图像,我们要获取某个位置(y,x)的像素值,可采用如下语句:

Scalar intensity = img.at<uchar>(y, x);
对3通道彩色图像有,我们可以采用如下的方式访问BGR数据:

Vec3b intensity = img.at<Vec3b>(y, x);
uchar blue = intensity.val[0];
uchar green = intensity.val[1];
uchar red = intensity.val[2];
类似的,对浮点型的3通道彩色图像,我们可以采用如下的方式访问BGR数据:
Vec3f intensity = img.at<Vec3f>(y, x);
float blue = intensity.val[0];
float green = intensity.val[1];
float red = intensity.val[2];

除用下标访问之外,我们还可以采用迭代器访问:

Mat_<Vec3b>::iterator it = image.begin<Vec3b>(),itEnd = image.end<Vec3b>();
for(; it != itEnd; ++it){
	uchar blue = (*it)[0];
	uchar green = (*it)[1];
	uchar red = (*it)[2];
}

如果要改变象元BGR分量的强度值,我们可以采用以下方式:

img.at<uchar>(y, x) = 128;
或者

for(int i = 0; i < 100; i++)
    img(i,i)=Vec3b(255,255,255);
或者

Mat_<Vec3b>::iterator it = image.begin<Vec3b>(),itEnd = image.end<Vec3b>();
for(; it != itEnd; ++it){
	*it=Vec3b(255,(*it)[1] ,(*it)[2] );
}


四、图像层面的一些基本操作

选定图像的感兴趣区域

Rect r(10, 10, 100, 100);
Mat smallImg = img(r);

从 IplImage 到 Mat

Mat img = imread("image.jpg");
IplImage img1 = img;
CvMat m = img;

从彩色图像到灰度图像

Mat img = imread("image.jpg"); // loading a 8UC3 image
Mat grey;
cvtColor(img, grey, CV_BGR2GRAY);
 从8UC1 到 32FC1:

src.convertTo(dst, CV_32F);

五、可视化图像
对于8U格式的图像,我们可以如下可视化
Mat img = imread("image.jpg");
namedWindow("image", CV_WINDOW_AUTOSIZE);
imshow("image", img);
waitKey();













  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值