openCV2使用指针的方式遍历图像image.ptr

来源:互联网 发布:max 软件下载 编辑:程序博客网 时间:2024/04/26 19:28
#include "opencv2/opencv.hpp"
#include <time.h>using namespace cv;using namespace std;//使用单循环的方式 将图像image赋值为白色void setAllWhiteE(Mat& image){        int x;        //把二维矩阵 image看成是1*length的一维向量        int length = image.cols*image.channels()*image.rows;        //获取矩阵数据的起始地址        uchar* data = image.ptr<uchar>(0);        ///逐个访问一维向量中的元素        for(x =0;x<length;x++)        {                data[x] = 255;        }}//使用双循环的方式 将图像image赋值为白色void setAllWhite(Mat& image){        int x,y;        //计算图像一行需要被赋值的个数        int rowLength = image.cols*image.channels();        for(y=0;y<image.rows;y++)        {                //获取第行的起始地址                uchar* data  = image.ptr<uchar>(y);                //对第y行逐个赋值                for(x=0;x<rowLength;x++)                {                        *data++=255;                }        }}//比较 两种方法的运行速度void compareTime(Mat& image){        int count = 100000;        long begin,end;        //统计双循环方式运行count次需要的时间        begin = clock();        while(count-->0)                setAllWhite(image);        end = clock();        //输出时间        printf("time is %f \n",(double)(end-begin)/(double)CLOCKS_PER_SEC);        //统计单循环方式运行count次需要的时间        count = 100000;        begin = clock();        while(count-->0)                setAllWhiteE(image);        end = clock();        //输出时间        printf("time is %f \n",(double)(end-begin)/(double)CLOCKS_PER_SEC);}int main(int argc, char* argv[]){                //读取图像 本图像可以在opencv的安装目录下 samples/c中找到        Mat src;        src = imread("lena.jpg");                //比较两种不同方法的效率        compareTime(src);        //显示图像        namedWindow("src");        imshow("src",src);        waitKey(0);        //创建一个isContinuous() 返回false的矩阵        uchar data[100];        Mat dst(10,10,CV_8U,(void*)data,12);        //dst.isContinuous()        return 0;}
	
				
		
原创粉丝点击