OpenCV图像数据访问,查询表和时间消耗测试
生活随笔
收集整理的這篇文章主要介紹了
OpenCV图像数据访问,查询表和时间消耗测试
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
OpenCV圖像數(shù)據(jù)訪問(wèn), 查詢(xún)表和時(shí)間消耗測(cè)試
代碼示例
#include <opencv2/core.hpp> #include <opencv2/core/utility.hpp> #include "opencv2/imgcodecs.hpp" #include <opencv2/highgui.hpp> #include <iostream> #include <sstream>using namespace std; using namespace cv;static void help() {cout<< "\n--------------------------------------------------------------------------" << endl<< "This program shows how to scan image objects in OpenCV (cv::Mat). As use case"<< " we take an input image and divide the native color palette (255) with the " << endl<< "input. Shows C operator[] method, iterators and at function for on-the-fly item address calculation."<< endl<< "Usage:" << endl<< "./how_to_scan_images <imageNameToUse> <divideWith> [G]" << endl<< "if you add a G parameter the image is processed in gray scale" << endl<< "--------------------------------------------------------------------------" << endl<< endl; }Mat& ScanImageAndReduceC(Mat& I, const uchar* table); Mat& ScanImageAndReduceIterator(Mat& I, const uchar* table); Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar * table);int main( int argc, char* argv[]) {help();if (argc < 3){cout << "Not enough parameters" << endl;return -1;}Mat I, J;if( argc == 4 && !strcmp(argv[3],"G") )I = imread(argv[1], IMREAD_GRAYSCALE);//灰度模式打開(kāi)圖像elseI = imread(argv[1], IMREAD_COLOR);//RGB模式打開(kāi)圖像if (I.empty()){cout << "The image" << argv[1] << " could not be loaded." << endl;return -1;}//! [dividewith]int divideWith = 0; // convert our input string to number - C++ stylestringstream s;s << argv[2];s >> divideWith;if (!s || !divideWith){cout << "Invalid number entered for dividing. " << endl;return -1;}uchar table[256];for (int i = 0; i < 256; ++i)table[i] = (uchar)(divideWith * (i/divideWith));//! [dividewith]const int times = 100;double t;t = (double)getTickCount();for (int i = 0; i < times; ++i){cv::Mat clone_i = I.clone();J = ScanImageAndReduceC(clone_i, table);}t = 1000*((double)getTickCount() - t)/getTickFrequency();t /= times;cout << "Time of reducing with the C operator [] (averaged for "<< times << " runs): " << t << " milliseconds."<< endl;t = (double)getTickCount();for (int i = 0; i < times; ++i){cv::Mat clone_i = I.clone();J = ScanImageAndReduceIterator(clone_i, table);}t = 1000*((double)getTickCount() - t)/getTickFrequency();t /= times;cout << "Time of reducing with the iterator (averaged for "<< times << " runs): " << t << " milliseconds."<< endl;t = (double)getTickCount();for (int i = 0; i < times; ++i){cv::Mat clone_i = I.clone();ScanImageAndReduceRandomAccess(clone_i, table);}t = 1000*((double)getTickCount() - t)/getTickFrequency();t /= times;cout << "Time of reducing with the on-the-fly address generation - at function (averaged for "<< times << " runs): " << t << " milliseconds."<< endl;//! [查詢(xún)表初始化]Mat lookUpTable(1, 256, CV_8U);uchar* p = lookUpTable.ptr();for( int i = 0; i < 256; ++i)p[i] = table[i];//! [table-init]t = (double)getTickCount();for (int i = 0; i < times; ++i)//! [查詢(xún)表使用]LUT(I, lookUpTable, J);//! [查詢(xún)表使用]t = 1000*((double)getTickCount() - t)/getTickFrequency();t /= times;cout << "Time of reducing with the LUT function (averaged for "<< times << " runs): " << t << " milliseconds."<< endl;return 0; }//! [C風(fēng)格[]方式訪問(wèn)] Mat& ScanImageAndReduceC(Mat& I, const uchar* const table) {// accept only char type matricesCV_Assert(I.depth() == CV_8U);int channels = I.channels();int nRows = I.rows;int nCols = I.cols * channels;if (I.isContinuous()){nCols *= nRows;nRows = 1;}int i,j;uchar* p;for( i = 0; i < nRows; ++i){p = I.ptr<uchar>(i);for ( j = 0; j < nCols; ++j){p[j] = table[p[j]];}}return I; }//! [迭代器安全方式訪問(wèn)] Mat& ScanImageAndReduceIterator(Mat& I, const uchar* const table) {// accept only char type matricesCV_Assert(I.depth() == CV_8U);const int channels = I.channels();switch(channels){case 1:{MatIterator_<uchar> it, end;for( it = I.begin<uchar>(), end = I.end<uchar>(); it != end; ++it)*it = table[*it];break;}case 3:{MatIterator_<Vec3b> it, end;for( it = I.begin<Vec3b>(), end = I.end<Vec3b>(); it != end; ++it){(*it)[0] = table[(*it)[0]];(*it)[1] = table[(*it)[1]];(*it)[2] = table[(*it)[2]];}}}return I; }//! [數(shù)組尋址隨機(jī)訪問(wèn)方式] Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar* const table) {// accept only char type matricesCV_Assert(I.depth() == CV_8U);const int channels = I.channels();switch(channels){case 1:{for( int i = 0; i < I.rows; ++i)for( int j = 0; j < I.cols; ++j )I.at<uchar>(i,j) = table[I.at<uchar>(i,j)];//灰度圖像cv::at()break;}case 3:{Mat_<Vec3b> _I = I;for( int i = 0; i < I.rows; ++i)for( int j = 0; j < I.cols; ++j ){_I(i,j)[0] = table[_I(i,j)[0]];_I(i,j)[1] = table[_I(i,j)[1]];_I(i,j)[2] = table[_I(i,j)[2]];}I = _I;break;}}return I; }1 灰度圖像的存儲(chǔ)方式
2 RGB模式的存儲(chǔ)方式
RGB模式像素的顏色值存儲(chǔ)方式BGR。內(nèi)存存儲(chǔ)的方式在計(jì)算機(jī)內(nèi)存足夠大的情況下是連續(xù)的,也許是不連續(xù)的判斷方式: cv::Mat::isContinuous()
總結(jié)
以上是生活随笔為你收集整理的OpenCV图像数据访问,查询表和时间消耗测试的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 图像的打开、修改、显示和保存示例(Ope
- 下一篇: 矩阵的掩模操作(锐化举例)