日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

OpenCV图像数据访问,查询表和时间消耗测试

發布時間:2025/7/25 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OpenCV图像数据访问,查询表和时间消耗测试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

OpenCV圖像數據訪問, 查詢表和時間消耗測試


代碼示例

#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);//灰度模式打開圖像elseI = imread(argv[1], IMREAD_COLOR);//RGB模式打開圖像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;//! [查詢表初始化]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)//! [查詢表使用]LUT(I, lookUpTable, J);//! [查詢表使用]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風格[]方式訪問] 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; }//! [迭代器安全方式訪問] 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; }//! [數組尋址隨機訪問方式] 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 灰度圖像的存儲方式


2 RGB模式的存儲方式


RGB模式像素的顏色值存儲方式BGR。內存存儲的方式在計算機內存足夠大的情況下是連續的,也許是不連續的判斷方式: cv::Mat::isContinuous()



總結

以上是生活随笔為你收集整理的OpenCV图像数据访问,查询表和时间消耗测试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。