学习体会:ios
/*************************************************************** * * 内容摘要:本例采用8种方法对图像Mat的像素进行扫描,并对像素点的像 * 素进行压缩,压缩间隔为div=64,并比较扫描及压缩的效率,效 * 率最高的是采用.ptr及减小循环次数来遍历图像,并采用位操 * 做来对图像像素进行压缩。 * 做 者:Jacky Liu * 完成日期:2012.8.10 * 参考资料:《OpenCV 2 computer Vision Application Programming * cookbook》 * ***************************************************************/ #include <opencv2/core/core.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream> //利用.ptr和数组下标进行图像像素遍历 void colorReduce0(cv::Mat &image, int div = 64) { int nl = image.rows; int nc = image.cols * image.channels(); //遍历图像的每一个像素 for(int j=0; j<nl ;++j) { uchar *data = image.ptr<uchar>(j); for(int i=0; i<nc; ++i) { data[i] = data[i]/div*div+div/2; //减小图像中颜色总数的关键算法:if div = 64, then the total number of colors is 4x4x4;整数除法时,是向下取整。 } } } //利用.ptr和 *++ 进行图像像素遍历 void colorReduce1(cv::Mat &image, int div = 64) { int nl = image.rows; int nc = image.cols * image.channels(); //遍历图像的每一个像素 for(int j=0; j<nl ;++j) { uchar *data = image.ptr<uchar>(j); for(int i=0; i<nc; ++i) { *data++ = *data/div*div + div/2; } } } //利用.ptr和数组下标进行图像像素遍历,取模运算用于减小图像颜色总数 void colorReduce2(cv::Mat &image, int div = 64) { int nl = image.rows; int nc = image.cols * image.channels(); //遍历图像的每一个像素 for(int j=0; j<nl ;++j) { uchar *data = image.ptr<uchar>(j); for(int i=0; i<nc; ++i) { data[i] = data[i]-data[i]%div +div/2; //利用取模运算,速度变慢,由于要读每一个像素两次 } } } //利用.ptr和数组下标进行图像像素遍历,位操做运算用于减小图像颜色总数 void colorReduce3(cv::Mat &image, int div = 64) { int nl = image.rows; int nc = image.cols * image.channels(); int n = static_cast<int>(log(static_cast<double>(div))/log(2.0)); //div=64, n=6 uchar mask = 0xFF<<n; //e.g. div=64, mask=0xC0 //遍历图像的每一个像素 for(int j=0; j<nl ;++j) { uchar *data = image.ptr<uchar>(j); for(int i=0; i<nc; ++i) { *data++ = *data&mask + div/2; } } } //形参传入const conference,故输入图像不会被修改;利用.ptr和数组下标进行图像像素遍历 void colorReduce4(const cv::Mat &image, cv::Mat &result,int div = 64) { int nl = image.rows; int nc = image.cols * image.channels(); result.create(image.rows,image.cols,image.type()); //遍历图像的每一个像素 for(int j=0; j<nl ;++j) { const uchar *data_in = image.ptr<uchar>(j); uchar *data_out = result.ptr<uchar>(j); for(int i=0; i<nc; ++i) { data_out[i] = data_in[i]/div*div+div/2; //减小图像中颜色总数的关键算法:if div = 64, then the total number of colors is 4x4x4;整数除法时,是向下取整。 } } } //利用.ptr和数组下标进行图像像素遍历,并将nc放入for循环中(比较糟糕的作法) void colorReduce5(cv::Mat &image, int div = 64) { int nl = image.rows; //遍历图像的每一个像素 for(int j=0; j<nl ;++j) { uchar *data = image.ptr<uchar>(j); for(int i=0; i<image.cols * image.channels(); ++i) { data[i] = data[i]/div*div+div/2; //减小图像中颜色总数的关键算法:if div = 64, then the total number of colors is 4x4x4;整数除法时,是向下取整。 } } } //利用迭代器 cv::Mat iterator 进行图像像素遍历 void colorReduce6(cv::Mat &image, int div = 64) { cv::Mat_<cv::Vec3b>::iterator it = image.begin<cv::Vec3b>(); //因为利用图像迭代器处理图像像素,所以返回类型必须在编译时知道 cv::Mat_<cv::Vec3b>::iterator itend = image.end<cv::Vec3b>(); for(;it != itend; ++it) { (*it)[0] = (*it)[0]/div*div+div/2; //利用operator[]处理每一个通道的像素 (*it)[1] = (*it)[1]/div*div+div/2; (*it)[2] = (*it)[2]/div*div+div/2; } } //利用.at<cv::Vec3b>(j,i)进行图像像素遍历 void colorReduce7(cv::Mat &image, int div = 64) { int nl = image.rows; int nc = image.cols; //遍历图像的每一个像素 for(int j=0; j<nl ;++j) { for(int i=0; i<nc; ++i) { image.at<cv::Vec3b>(j,i)[0] = image.at<cv::Vec3b>(j,i)[0]/div*div + div/2; image.at<cv::Vec3b>(j,i)[1] = image.at<cv::Vec3b>(j,i)[1]/div*div + div/2; image.at<cv::Vec3b>(j,i)[2] = image.at<cv::Vec3b>(j,i)[2]/div*div + div/2; } } } //减小循环次数,进行图像像素遍历,调用函数较少,效率最高。 void colorReduce8(cv::Mat &image, int div = 64) { int nl = image.rows; int nc = image.cols; //判断是不是连续图像,便是否有像素填充 if(image.isContinuous()) { nc = nc*nl; nl = 1; } int n = static_cast<int>(log(static_cast<double>(div))/log(2.0)); uchar mask = 0xFF<<n; //遍历图像的每一个像素 for(int j=0; j<nl ;++j) { uchar *data = image.ptr<uchar>(j); for(int i=0; i<nc; ++i) { *data++ = *data & mask +div/2; *data++ = *data & mask +div/2; *data++ = *data & mask +div/2; } } } const int NumTests = 9; //测试算法的数量 const int NumIteration = 20; //迭代次数 int main(int argc, char* argv[]) { int64 t[NumTests],tinit; cv::Mat image1; cv::Mat image2; //数组初始化 int i=0; while(i<NumTests) { t[i++] = 0; } int n = NumIteration; //迭代n次,取平均数 for(int i=0; i<n; ++i) { image1 = cv::imread("../boldt.jpg"); if(!image1.data) { std::cout<<"read image failue!"<<std::endl; return -1; } // using .ptr and [] tinit = cv::getTickCount(); colorReduce0(image1); t[0] += cv::getTickCount() - tinit; // using .ptr and *++ image1 = cv::imread("../boldt.jpg"); tinit = cv::getTickCount(); colorReduce1(image1); t[1] += cv::getTickCount() - tinit; // using .ptr and [] and modulo image1 = cv::imread("../boldt.jpg"); tinit = cv::getTickCount(); colorReduce2(image1); t[2] += cv::getTickCount() - tinit; // using .ptr and *++ and bitwise image1 = cv::imread("../boldt.jpg"); tinit = cv::getTickCount(); colorReduce3(image1); t[3] += cv::getTickCount() - tinit; //using input and output image image1 = cv::imread("../boldt.jpg"); tinit = cv::getTickCount(); colorReduce4(image1,image2); t[4] += cv::getTickCount() - tinit; // using .ptr and [] with image.cols * image.channels() image1 = cv::imread("../boldt.jpg"); tinit = cv::getTickCount(); colorReduce5(image1); t[5] += cv::getTickCount() - tinit; // using .ptr and *++ and iterator image1 = cv::imread("../boldt.jpg"); tinit = cv::getTickCount(); colorReduce6(image1); t[6] += cv::getTickCount() - tinit; //using at image1 = cv::imread("../boldt.jpg"); tinit = cv::getTickCount(); colorReduce7(image1); t[7] += cv::getTickCount() - tinit; //using .ptr and * ++ and bitwise (continuous+channels) image1 = cv::imread("../boldt.jpg"); tinit = cv::getTickCount(); colorReduce8(image1); t[8] += cv::getTickCount() - tinit; } cv::namedWindow("Result"); cv::imshow("Result",image1); cv::namedWindow("Result Image"); cv::imshow("Result Image",image2); std::cout<<std::endl<<"-------------------------------------------------------------------------"<<std::endl<<std::endl; std::cout<<"using .ptr and [] = "<<1000*t[0]/cv::getTickFrequency()/n<<"ms"<<std::endl; std::cout<<"using .ptr and *++ = "<<1000*t[1]/cv::getTickFrequency()/n<<"ms"<<std::endl; std::cout<<"using .ptr and [] and modulo = "<<1000*t[2]/cv::getTickFrequency()/n<<"ms"<<std::endl; std::cout<<"using .ptr and *++ and bitwise = "<<1000*t[3]/cv::getTickFrequency()/n<<"ms"<<std::endl; std::cout<<"using input and output image = "<<1000*t[4]/cv::getTickFrequency()/n<<"ms"<<std::endl; std::cout<<"using .ptr and [] with image.cols * image.channels() = "<<1000*t[5]/cv::getTickFrequency()/n<<"ms"<<std::endl; std::cout<<"using .ptr and *++ and iterator = "<<1000*t[6]/cv::getTickFrequency()/n<<"ms"<<std::endl; std::cout<<"using at = "<<1000*t[7]/cv::getTickFrequency()/n<<"ms"<<std::endl; std::cout<<"using .ptr and * ++ and bitwise (continuous+channels) = "<<1000*t[8]/cv::getTickFrequency()/n<<"ms"<<std::endl; std::cout<<std::endl<<"-------------------------------------------------------------------------"<<std::endl<<std::endl; cv::waitKey(); return 0; }