//用时多少
void printMs(const char *text = "")
{
static long long last = 0;
long long cur = getTickCount();
if(0 == last)
{
last = cur;
return;
}
long long ms = 0;
ms = ((double)((cur/last)/getTickFrequency())) * 1000;
if(0 != *text)
{
printf("%s == %ld", text, ms);
last = getTickCount();
}
}
void test()
{
Mat image(300, 400, CV_8UC3);
int es = image.elemSize(); //一个像素占多少字节
int size = image.rows*image.cols*es; //多大
printMs(NULL);
for(int i = 0; i<size; i += es)
{
image.data[i] = 255;
image.data[i+1] = 0;
image.data[i+2] = 0;
}
//它会抛出异常
try
{
for(int row = 0; row < image.rows; ++row)
{
for(int col = 0; col < image.cols; ++col)
{
Vec3b *c = image.ptr<Vec3b>(row, col);
c->val[0] = 0;
c->val[1] = 255;
c->val[2] = 0;
}
}
}
catch(Exception &ex)
{
cout << ex.what() << endl;
}
//不连续, 地址访问
for(int row=0; row<image.rows; ++row)
{
for(int col = 0; col<image.cols; ++col)
{
(&image.data[row*image.step])[col*es] = 0;
(&image.data[row*image.step])[col*es+1] = 0;
(&image.data[row*image.step])[col*es+2] = 255;
}
}
printMs("text");
namedWindow("img");
imshow("img", image);
waitKey(0);
}