新手学习opencv,Mat和IplImage简单处理图像的效率

                          新手学习opencv,Mat和IplImage简单处理图像的效率

      处理目的:对一张读入的图像,进行灰度值反转,即255减去原始灰度值。
      循环200次,取平均耗时。
#include "cv.h"
#include "highgui.h"
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
	string imageName ="1001.jpg";
	Mat src = imread(imageName,1);
	Mat res = Mat::zeros(src.rows,src.cols,src.type());

	long int count = 0;
	float num = 0;
	double t1 = (double)getTickCount();
	for(int n=0;n<200;n++)
	{
		for (int i = 0; i < src.rows; i++)
		{
			uchar* data= res.ptr<uchar>(i); // 每一行图像的指针
			uchar* idata= src.ptr<uchar>(i); // 每一行图像的指针
			for (int j = 0; j < src.cols*src.channels(); j++)
			{
				*data++=255-(*idata++);  
			}
		}
	}
	t1= ((double)getTickCount() - t1)/getTickFrequency()/200;
	imshow("src",src);
	imshow("result1",res);
	///////////////////////////////
	int nl= src.rows; 
	int nc= src.cols * src.channels(); 
	if (src.isContinuous()&&res.isContinuous())  
	{
		nc= nc*nl; 
		nl= 1;  
	}
	double t2 = (double)getTickCount();
	for(int n=0;n<200;n++)
	{
		for (int i=0; i<nl; i++)
		{
			uchar* data= res.ptr<uchar>(i); 
			uchar* idata= src.ptr<uchar>(i);
			for (int j=0; j<nc; j++)
			{
				*data++=255-(*idata++);  
			}    
		}
	}
	t2= ((double)getTickCount() - t2)/getTickFrequency()/200; 
	imshow("src2",src);
	imshow("result2",res);
	//////////////IplImage类遍历//////////////
	IplImage * img = cvLoadImage("1001.jpg");
	IplImage *res2=cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,img->nChannels);
	char  *data = img->imageData;
	int step = img->widthStep;
	int chanel = img->nChannels;
	char  *outdata = res2->imageData;
	double t3= (double)getTickCount();
	for(int n=0;n<200;n++)
	{
		for(int i=0;i<img->height;i++)
		{
			for(int j=0;j<img->width;j++)
			{
				outdata[i*step + j*chanel] = 255-data[i*step + j*chanel];
				outdata[i*step + j*chanel+1] = 255-data[i*step + j*chanel + 1];
				outdata[i*step + j*chanel+2] = 255-data[i*step + j*chanel + 2];
			}
		}
	}
	t3= ((double)getTickCount() - t3)/getTickFrequency()/200; 
	cvNamedWindow("yaunshi");
	cvShowImage("yaunshi",img);
	cvNamedWindow("result3");
	cvShowImage("result3",res2);
	cout <<"Mat类简单处理耗时1:" << t1 << endl;
	cout <<"Mat类简单处理耗时2:" << t2 << endl;
	cout <<"IplImage类简单处理耗时:"<<t3<< endl;
	cvWaitKey(0);
	cvReleaseImage(&img);
	cvReleaseImage(&res2);
	cvDestroyWindow("yaunshi");
	cvDestroyWindow("result3");
	return 0;
}


新手学习opencv,Mat和IplImage简单处理图像的效率_第1张图片

你可能感兴趣的:(新手学习opencv,Mat和IplImage简单处理图像的效率)