计算机视觉
图像处理

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

新手学习Opencv,MatIplImage简单处理图像的效率

处理目的:对一张读入的图像,进行灰度值反转,即255减去原始灰度值。

循环200次,取平均耗时。

#include "cv.h"
#include "highgui.h"
#include 
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(i); // 每一行图像的指针
			uchar* idata= src.ptr(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(i); 
			uchar* idata= src.ptr(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;iheight;i++)
		{
			for(int j=0;jwidth;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;
}

转载注明来源:CV视觉网 » 新手学习opencv,Mat和IplImage简单处理图像的效率

分享到:更多 ()
扫描二维码,给作者 打赏
pay_weixinpay_weixin

请选择你看完该文章的感受:

0不错 1超赞 0无聊 0扯淡 0不解 0路过

评论 3

评论前必须登录!