简介:opencv c++里面访问像素数据的方法不是很直观,不像python里面直接img[i,j]就可以,在c++中还需要考虑数据类型,和图片通道数,在此整理记录,造个轮子。综合来看采用指针访问的速度是最快的,在使用时尽量用指针方式去处理图片。
1. opencv图片数据类型定义:
- 单通道的基本数据类型定义
Mat_<uchar>-------- - CV_8U
Mat<char>---------- - CV_8S
Nat_<short>-------- - CV_16S
Mat_<ushort>--------CV_16U
Mat_<int>---------- - CV_32S
Mat_<float>----------CV_32F
Mat_<double>--------CV_64F
opencv中的图片数据类型中常看到CV_8UC1、CV_8UC3这种写法,8U 表示uchar, C表示的是channel通道数,C1表示只有一个通道,C3表示有三个通道。
- 多通道的vector 类型定义
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<ushort, 2> Vec2w;
typedef Vec<ushort, 3> Vec3w;
typedef Vec<ushort, 4> Vec4w;
typedef Vec<int, 2> Vec2i;
typedef Vec<int, 3> Vec3i;
typedef Vec<int, 4> Vec4i;
typedef Vec<int, 6> Vec6i;
typedef Vec<int, 8> Vec8i;
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;
2.Mat.at<type>(i,j)访问方式:
- 对单通道的图,比如图像类型是CV_8UC1,这是最常见的灰度图形式,uchar类型,用img.at<type>时type就是uchar,如下:
cv::Mat img = cv::Mat::zeros(cv::Size(w, h), CV_8UC1);
//mat.at<i,j> 按像素访问
for (int i = 0; i < img.rows; i++)
{
for (int j = 0; j < img.cols; j++)
{
img.at<uchar>(i, j) = 255;
}
}
- 对多通道的图,如图像类型是CV_64FC3,这表示的是一个用double类型存储的三通道图,用img.at<type>()访问时,type就是Vec3d,如下:
cv::Mat img2 = cv::Mat::zeros(cv::Size(w, h), CV_64FC3);
//mat.at 访问方式
for (int i = 0; i < img2.rows; i++)
{
for (int j = 0; j < img2.cols; j++)
{
img2.at<cv::Vec3d>(i, j)[0] = 125;//B channel
img2.at<cv::Vec3d>(i, j)[1] = 100;//G channel
img2.at<cv::Vec3d>(i, j)[2] = 255;//R channel
}
}
2. 指针访问方式:
- 对单通道的图,比如图像类型是CV_8UC1,这是最常见的灰度图形式,uchar类型,img.ptr<type> 的type就是uchar
cv::Mat img = cv::Mat::zeros(cv::Size(w, h), CV_8UC1);
uchar *ptr = img.ptr<uchar>(0);
for (int i = 0; i < img.rows; i++)
{
ptr = img.ptr<uchar>(i);//ptr指向第i行数据的首地址
for (int j = 0; j < img.cols; j++)
{
*ptr = 127;
ptr++;//ptr移动到下一个地址
}
}
- 对多通道图,比如类型为CV_64FC3,这表示用double类型存储的三通道图
cv::Mat img2 = cv::Mat::zeros(cv::Size(w, h), CV_64FC3);
//mat.at 访问方式
double *ptr2 = img2.ptr<double>(0);
for (int i = 0; i < img2.rows; i++)
{
ptr2 = img2.ptr<double>(i);//ptr指向第i行数据的首地址
for (int j = 0; j < img2.cols; j++)
{
*ptr2++ = 255;//B channel
*ptr2++ = 127;//G channel
*ptr2++ = 0;//R channel
//注:*ptr++ = 255;//是先对(*ptr)赋值为255 然后再把ptr指向下一个位置。
}
}
- 指针访问方式,直接访问指定位置(i,j)
double *ptr3 = img2.ptr<double>(0);
for (int i = 0; i < img2.rows; i++)
{
ptr3 = img2.ptr<double>(i);//ptr指向第i行数据的首地址
for (int j = 0; j < img2.cols; j++)
{
*(ptr3 + 3*j) = 0;//B channel
*(ptr3 + 3*j + 1) = 120;//G channel
*(ptr3 + 3*j + 2) = 120;//R channel
}
}
附录
- 完整代码
#include <opencv.hpp>
int main()
{
int h = 300;
int w = 200;
/*
Mat_<uchar>-------- - CV_8U
Mat<char>---------- - CV_8S
Nat_<short>-------- - CV_16S
Mat_<ushort>--------CV_16U
Mat_<int>---------- - CV_32S
Mat_<float>----------CV_32F
Mat_<double>--------CV_64F
*/
//单通道图的访问
cv::Mat img = cv::Mat::zeros(cv::Size(w, h), CV_8UC1);
cv::imshow("img", img);
cv::waitKey(0);
//mat.at<i,j> 按像素访问
for (int i = 0; i < img.rows; i++)
{
for (int j = 0; j < img.cols; j++)
{
img.at<uchar>(i, j) = 255;
}
}
cv::imshow("img", img);
cv::waitKey(0);
//用指针方式访问
//初始化指针ptr指向img的数据的首地址
uchar *ptr = img.ptr<uchar>(0);
for (int i = 0; i < img.rows; i++)
{
ptr = img.ptr<uchar>(i);//ptr指向第i行数据的首地址
for (int j = 0; j < img.cols; j++)
{
*ptr = 127;
ptr++;//ptr移动到下一个地址
}
}
cv::imshow("img", img);
cv::waitKey(0);
//三通道图的访问
/*
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<ushort, 2> Vec2w;
typedef Vec<ushort, 3> Vec3w;
typedef Vec<ushort, 4> Vec4w;
typedef Vec<int, 2> Vec2i;
typedef Vec<int, 3> Vec3i;
typedef Vec<int, 4> Vec4i;
typedef Vec<int, 6> Vec6i;
typedef Vec<int, 8> Vec8i;
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;
*/
cv::Mat img2 = cv::Mat::zeros(cv::Size(w, h), CV_64FC3);
cv::imshow("img", img2);
cv::waitKey(0);
//mat.at 访问方式
for (int i = 0; i < img2.rows; i++)
{
for (int j = 0; j < img2.cols; j++)
{
img2.at<cv::Vec3d>(i, j)[0] = 125;//B channel
img2.at<cv::Vec3d>(i, j)[1] = 100;//G channel
img2.at<cv::Vec3d>(i, j)[2] = 255;//R channel
}
}
cv::imshow("img", img2);
cv::waitKey(0);
//指针访问方式
//img2是CV_64FC3,表示的是double类型的三通道的图
double *ptr2 = img2.ptr<double>(0);
for (int i = 0; i < img2.rows; i++)
{
ptr2 = img2.ptr<double>(i);//ptr指向第i行数据的首地址
for (int j = 0; j < img2.cols; j++)
{
*ptr2++ = 255;//B channel
*ptr2++ = 127;//G channel
*ptr2++ = 0;//R channel
//注:*ptr++ = 255;//是先对(*ptr)赋值为255 然后再把ptr指向下一个位置。
}
}
cv::imshow("img", img2);
cv::waitKey(0);
double *ptr3 = img2.ptr<double>(0);
for (int i = 0; i < img2.rows; i++)
{
ptr3 = img2.ptr<double>(i);//ptr指向第i行数据的首地址
for (int j = 0; j < img2.cols; j++)
{
*(ptr3 + 3*j) = 0;//B channel
*(ptr3 + 3*j + 1) = 120;//G channel
*(ptr3 + 3*j + 2) = 120;//R channel
}
}
cv::imshow("img", img2);
cv::waitKey(0);
}
网友评论