Mat m=imread("...");
m.data;//图像的数据开始指针
m.ptr<类型>(Y)---m.ptr<uchar>(0) --- 一行的起始位置
连续存储 -- 每行的长度==图像的实际使用长度
非连续存储 --- 即 对每行尾部进行了补充,使其是4或8的整数倍(通常硬件需要,提高处理速度)
例如
uchar data[100];
Mat m(10,10,CV_8U1,data,12);//设置了行长度为12,实际=10;则行尾部需要添加2字节
判读是否为连续存储 m.isContinuous()
#include <Windows.h>
#include <time.h>
#include "opencv.hpp"
using namespace cv;
//void setAllWhite(Mat& src)
//{
// int x,y;
//
// for (int y = 0; y < src.rows; y++)
// {
//
// uchar* p=src.ptr<uchar>(y);
// for (int x = 0; x < src.cols; x++)
// {
// p[0]=255;
// p[1]=255;
// p[2]=255;
//
// p+=3;
// }
//
// }
//
//}
//void setAllWhite(Mat& src)
//{
// int x,y;
//
// int length=src.cols * src.channels();
//
// for (int y = 0; y < src.rows; y++)
// {
//
// uchar* p=src.ptr<uchar>(y);
// for (int x = 0; x < length; x++)
// {
// *(p++)=255;
// }
//
// }
//
//}
void setAllWhite(Mat& src)
{
int x,y;
for (int y = 0; y < src.rows; y++)
{
Vec3b* p=src.ptr<Vec3b>(y);
for (int x = 0; x < src.cols; x++)
*(p++)=Vec3b(255,255,255);
}
}
void setAllWhiteE(Mat& src)
{
uchar* p=src.data;
int length=src.rows * src.cols * src.channels();
while (length-->0)*(p++)=255;
}
void compareTime(Mat& src)
{
int count=1000;
long begin,end;
begin=clock();
while(count-->0)setAllWhite(src);
end=clock();
printf("(setAllWhite) time is %f \n",(double)(end-begin)/(double)CLOCKS_PER_SEC);
count=1000;
begin=clock();
while(count-->0)setAllWhiteE(src);
end=clock();
printf("(setAllWhiteE) time is %f \n",(double)(end-begin)/(double)CLOCKS_PER_SEC);
}
int main()
{
Mat src = imread("C:\\Users\\Administrator\\Desktop\\1.jpg");
compareTime(src);
/*setAllWhiteE(src);
imshow("xx",src);
waitKey(0);*/
return 0;
}