重新梳理一下 opencv遍历图像 isContinuous or not

之前一直在使用ptr at,有一个问题,debug版太慢了,影响效率,想直接使用.data的方法,但是对于 not isContinuous的情况,不好把握,现在先将梳理后的第一个版本记录下来。

at就直接抛弃了。

一、isContinuous的情况

这里实验了4种数据类型:

Mat grayimg;  //cv_8uc1
Mat rgbimg;  //cv_8uc3
Mat shortimg; //cv_16s
Mat shortimg3;//cv_16sc3

1.ptr 版本一,使用指针ptr()

void useptrv1()
{
	int n, i, j;
	for (n=0;n<100;n++)
	{
		const int nrow = grayimg.rows;
		const int ncols = grayimg.cols;
		uchar *p1, *p3;
		p1 = grayimg.ptr();
		p3 = rgbimg.ptr();
		short *ps1;
		short *ps3;
		ps1 = (short *)shortimg.ptr();
		ps3 = (short *)shortimg3.ptr();
		int thecstep = shortimg3.channels()*shortimg3.cols;
		for (i = 0; i < nrow; i++, p1 += grayimg.step, p3+=rgbimg.step, ps1+=shortimg.cols,ps3+= thecstep)
		{
			for (j = 0; j < ncols; j++)
			{
				p1[j] = 0xff;
				p3[3*j] = 0xff;
				p3[3 * j+1] = 0;
				p3[3 * j+2] = 0;
				ps1[j] = 500;
				ps3[3 * j] = 500;
				ps3[3 * j+1] = 300;
				ps3[3 * j+2] = 700;
			}
		}
	}
}

2. ptr版本二 使用模板函数 .ptr<>

void useptrv2()
{
	int n, i, j;
	for (n = 0; n < 100; n++)
	{
		const int nrow = grayimg.rows;
		const int ncols = grayimg.cols;
		uchar *p1;
		Vec3b *p3;
		short *ps1;
		Vec3s *ps3;
		for (i = 0; i < nrow; i++)
		{
			p1 = grayimg.ptr<uchar>(i);
			p3 = rgbimg.ptr<Vec3b>(i);
			ps1 = shortimg.ptr<short>(i);
			ps3 = shortimg3.ptr<Vec3s>(i);
			for (j = 0; j < ncols; j++)
			{
				p1[j] = 0xff;
				p3[j][0] = 0xff;
				p3[j][1] = 0;
				p3[j][2] = 0;
				ps1[j] = 0xff;
				ps3[j][0] = 500;
				ps3[j][1] = 300;
				ps3[j][2] = 700;
			}
		}
	}
}

3.使用data

void usedata()
{
	int n, i, j;
	for (n = 0; n < 100; n++)
	{
		const int nrow = grayimg.rows;
		const int ncols = grayimg.cols;
		uchar *p1, *p3;
		p1 = grayimg.data;
		p3 = rgbimg.data;
		short *ps1;
		short *ps3;
		ps1 = (short *)shortimg.data;
		ps3 = (short *)shortimg3.data;
		int thecstep = shortimg3.channels()*shortimg3.cols;
		for (i = 0; i < nrow; i++, p1 += grayimg.step, p3 += rgbimg.step, ps1 += shortimg.cols, ps3 += thecstep)
		{
			for (j = 0; j < ncols; j++)
			{
				p1[j] = 0xff;
				p3[3 * j] = 0xff;
				p3[3 * j + 1] = 0;
				p3[3 * j + 2] = 0;
				ps1[j] = 500;
				ps3[3 * j] = 500;
				ps3[3 * j + 1] = 300;
				ps3[3 * j + 2] = 700;
			}
		}
	}
}

可以看到.data 与.ptr()的效果是接近的。

最后是测试结果:

可以看见,Debug版的差别,Release板是没有差别的。当然对于CV_8UC1 .ptr()的方法和.ptr<>的方法耗时是接近的。但是对于Vec类的逐个枚举,耗时是比较大的。

二、not isContinuous的情况

制造not isContinuous,常见的情况就是截取图片。例如利用rect range截取大图片中的一部分。

使用的数据:

Mat grayimg_cut;
Mat shortimg3_cut;
grayimg_cut = grayimg(Rect(1, 1, 10, 8));
shortimg3_cut= shortimg3(Rect(1, 1, 10, 8));

对于.ptr<>其自带防呆,这里直接放data方法的代码:

void usedata_forcut()
{
	if (grayimg_cut.isContinuous() || shortimg3_cut.isContinuous())
	{
		std::cout << "Error!!!!!!!!!!!!!!!!!!!" << std::endl;
	}
	int n, i, j;
	for (n = 0; n < 100; n++)
	{
		const int nrow = grayimg_cut.rows;
		const int ncols = grayimg_cut.cols;
		uchar *p1, *ps3;
		p1 = grayimg_cut.data;
		ps3 = shortimg3_cut.data;
		for (i = 0; i < nrow; i++, p1 += grayimg_cut.step, ps3+=shortimg3_cut.step)
		{
			for (j = 0; j < ncols; j++)
			{
				p1[j] = 0x20;
				((short*)ps3)[3 * j] = 200;
				((short*)ps3)[3 * j+1] = 200;
				((short*)ps3)[3 * j+2] = 200;
			}
		}
	}
}

看效果图:

以下是shortimg的Image Watch显示结果:

以下是shortimg_cut的Image Watch显示结果:

三、最重要的地方

这里稍微看了一下资料,发现很多地方说 img.step[0]=img.channels*img.cols; 现在看来是不对的。

    std::cout << "grayimg.step: " << grayimg.step << std::endl;
	std::cout << "grayimg_cut.step: " << grayimg_cut.step << std::endl;
	std::cout << "grayimg_cut.channels * grayimg_cut.cols::   " << grayimg_cut.channels()*grayimg_cut.cols<<"\n";

	std::cout << "shortimg3.step: " << shortimg3.step << std::endl;
	std::cout << "shortimg3_cut.step: " << shortimg3_cut.step << std::endl;
	std::cout << "shortimg3_cut.channels * shortimg3_cut.cols::   " << shortimg3_cut.channels()*shortimg3_cut.cols << "\n";

代码运行结果:

img.step[0]=img.channels*img.cols 有两个前提,第一 isContinuous 第二 CV_8UC1.

这里还没来得及具体查看Mat 的opencv原文档,有空了,后续在这里补上。

最后、附上完整代码

using namespace std;
using namespace cv;
Mat grayimg;
Mat rgbimg;
Mat shortimg; //cv_16s
Mat shortimg3;//cv_16sc3
Mat grayimg_cut;
Mat shortimg3_cut;
void usedata_forcut()
{
	if (grayimg_cut.isContinuous() || shortimg3_cut.isContinuous())
	{
		std::cout << "Error!!!!!!!!!!!!!!!!!!!" << std::endl;
	}
	int n, i, j;
	std::cout << "grayimg.step: " << grayimg.step << std::endl;
	std::cout << "grayimg_cut.step: " << grayimg_cut.step << std::endl;
	std::cout << "grayimg_cut.channels * grayimg_cut.cols::   " << grayimg_cut.channels()*grayimg_cut.cols<<"\n";

	std::cout << "shortimg3.step: " << shortimg3.step << std::endl;
	std::cout << "shortimg3_cut.step: " << shortimg3_cut.step << std::endl;
	std::cout << "shortimg3_cut.channels * shortimg3_cut.cols::   " << shortimg3_cut.channels()*shortimg3_cut.cols << "\n";
	for (n = 0; n < 100; n++)
	{
		const int nrow = grayimg_cut.rows;
		const int ncols = grayimg_cut.cols;
		uchar *p1, *ps3;
		p1 = grayimg_cut.data;
		ps3 = shortimg3_cut.data;
		for (i = 0; i < nrow; i++, p1 += grayimg_cut.step, ps3+=shortimg3_cut.step)
		{
			for (j = 0; j < ncols; j++)
			{
				p1[j] = 0x20;
				((short*)ps3)[3 * j] = 200;
				((short*)ps3)[3 * j+1] = 200;
				((short*)ps3)[3 * j+2] = 200;
			}
		}
	}
}
void usedata()
{
	int n, i, j;
	for (n = 0; n < 100; n++)
	{
		const int nrow = grayimg.rows;
		const int ncols = grayimg.cols;
		uchar *p1, *p3;
		p1 = grayimg.data;
		p3 = rgbimg.data;
		short *ps1;
		short *ps3;
		ps1 = (short *)shortimg.data;
		ps3 = (short *)shortimg3.data;
		int thecstep = shortimg3.channels()*shortimg3.cols;
		for (i = 0; i < nrow; i++, p1 += grayimg.step, p3 += rgbimg.step, ps1 += shortimg.cols, ps3 += thecstep)
		{
			for (j = 0; j < ncols; j++)
			{
				p1[j] = 0xff;
				p3[3 * j] = 0xff;
				p3[3 * j + 1] = 0;
				p3[3 * j + 2] = 0;
				ps1[j] = 500;
				ps3[3 * j] = 500;
				ps3[3 * j + 1] = 300;
				ps3[3 * j + 2] = 700;
			}
		}
	}
}
void useptrv1()
{
	int n, i, j;
	for (n=0;n<100;n++)
	{
		const int nrow = grayimg.rows;
		const int ncols = grayimg.cols;
		uchar *p1, *p3;
		p1 = grayimg.ptr();
		p3 = rgbimg.ptr();
		short *ps1;
		short *ps3;
		ps1 = (short *)shortimg.ptr();
		ps3 = (short *)shortimg3.ptr();
		int thecstep = shortimg3.channels()*shortimg3.cols;
		for (i = 0; i < nrow; i++, p1 += grayimg.step, p3+=rgbimg.step, ps1+=shortimg.cols,ps3+= thecstep)
		{
			for (j = 0; j < ncols; j++)
			{
				p1[j] = 0xff;
				p3[3*j] = 0xff;
				p3[3 * j+1] = 0;
				p3[3 * j+2] = 0;
				ps1[j] = 500;
				ps3[3 * j] = 500;
				ps3[3 * j+1] = 300;
				ps3[3 * j+2] = 700;
			}
		}
	}
}
void useptrv2()
{
	int n, i, j;
	for (n = 0; n < 100; n++)
	{
		const int nrow = grayimg.rows;
		const int ncols = grayimg.cols;
		uchar *p1;
		Vec3b *p3;
		short *ps1;
		Vec3s *ps3;
		for (i = 0; i < nrow; i++)
		{
			p1 = grayimg.ptr<uchar>(i);
			p3 = rgbimg.ptr<Vec3b>(i);
			ps1 = shortimg.ptr<short>(i);
			ps3 = shortimg3.ptr<Vec3s>(i);
			for (j = 0; j < ncols; j++)
			{
				p1[j] = 0xff;
				p3[j][0] = 0xff;
				p3[j][1] = 0;
				p3[j][2] = 0;
				ps1[j] = 0xff;
				ps3[j][0] = 500;
				ps3[j][1] = 300;
				ps3[j][2] = 700;
			}
		}
	}
}
int main()
{
#if _DEBUG
	std::cout << "Now is Debug version:" << endl;
#else
	std::cout << "Now is Release version:" << endl;
#endif
	grayimg = Mat(Size(1000, 800), CV_8UC1);
	cvtColor(grayimg, rgbimg, COLOR_GRAY2BGR);
	shortimg = Mat(Size(1000, 800), CV_16SC1);
	shortimg3 = Mat(Size(1000, 800), CV_16SC3);
	std::cout << "For gray,the step:" << grayimg.step << endl;
	std::cout << "For Rgb, the step:" << rgbimg.step << endl;
	std::cout << "For short1, the step:" << shortimg.step << endl;
	std::cout << "For short3, the step:" << shortimg3.step << endl;

	int64 st = getTickCount();
	useptrv1();
	std::cout << "ptrv1 :  " << double(getTickCount() - st) *1000.0 / getTickFrequency() << "ms" << endl;
	st = getTickCount();
	useptrv2();
	std::cout << "ptrv2 :  " << double(getTickCount() - st) *1000.0 / getTickFrequency() << "ms" << endl;
	st = getTickCount();
	usedata();
	std::cout << "data : " << double(getTickCount() - st) *1000.0 / getTickFrequency() << "ms" << endl;
	st = getTickCount();
	Test for Fase isContinuous()//
	grayimg_cut = grayimg(Rect(1, 1, 10, 8));
	shortimg3_cut= shortimg3(Rect(1, 1, 10, 8));
	usedata_forcut();
	std::cout << "data_forcut  : " << double(getTickCount() - st) *1000.0 / getTickFrequency() << "ms" << endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陶醉鱼

感觉写的不错就给个打赏哦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值