目標:
這次學習的目標是回答下面的幾個問題:
1 圖片像素是如何被掃描的?
2OpenCV 矩陣值如何被存儲?
3如何衡量算法的性能?
4什麼是查找表和為什麼要用他們?
看完這篇,希望能夠解決上面的這些問題。
正文:
首先我們考慮一下簡單的色彩降低方法(color reduction method,翻譯的不好請指正),如果使用的是c或c++無符號的char(八字節大小的空間),一個信道(channel)有256個不同的值(2^8=256),但是如果使用的是GRB方案,三個channel的話,顏色的數量就會變為256*256*256,大概是16個million這麼多,這麼多的顏色數量,對於計算機來說仍然是一個負擔,所以可以想一些方法來降低這些色彩數量。
可以使用簡單的方法來降低圖像色彩空間,比如,將0-9的數字都統一用0來代替,10-19的數字都統一用10代替。這種轉換方案可以用下面的公式表示

通過上面的公式,把所有像素點的值更新一下。但是,上面的公式中有除法,這裡要表達一個是,計算量比較多的情況下,不用乘除,就不要用,最好把他們轉換為加減。我們知道,在轉換前像素點的值只有256個,所以我們可以用查找表的方式,我們事先把所有的計算結果都保存在一個數組裡,每次要執行上面的公式計算的時候,結果直接從數組裡取出來就ok了。比如32對應30,表table[32]=30是早計算出來的,直接訪問table[32]就OK了。
圖片矩陣如何在內存中存儲的:
灰度圖片的矩陣存儲方式:
灰度圖片的每一個像素點,只由一個值來表示,所以,就是一個普通的二維矩陣。

彩色圖片的矩陣存儲方式:

彩色圖片的存儲方式和灰度圖片不一樣,這裡展示的是RGB格式的,可以看到,每一個像素,由三個值,代表藍色,綠色,紅色的三個數值表示,存儲方式不是三維的,而是二維,不過列向量放大了三倍。從圖片中可以清楚的看到。
返回欄目頁:http://www.bianceng.cn/Programming/cplus/
效率:
比較像素數量降低方式效率的代碼,在本文的最後面,代碼看上去很多,其實結構比較簡單,看一會兒就明白了。附上一張結果圖:

最快的OpenCV內的LUT函數。關於LUT,看這裡
可以粗略的看一下代碼,代碼不難,很容易懂:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <sstream>
using namespace std;
using namespace cv;
static void help()
{
//這裡提示輸入有三個參數,第一個是圖像的名字,第二個是參數是公式中的降低顏色數的數字,這裡是10,第三個參數,如果是[G]代表是灰度圖片,否則不是。
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
<< "./howToScanImages 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);
/*
程序主要是看不同的color reduction方式對於程序運行速度的影響。
使用getTickCount()函數來獲取當前時間,利用當前時間-上次獲取的時間,來得到運行時間
*/
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], CV_LOAD_IMAGE_GRAYSCALE);
else
I = imread(argv[1], CV_LOAD_IMAGE_COLOR);
if (!I.data)
{
cout << "The image" << argv[1] << " could not be loaded." << endl;
return -1;
}
int divideWith = 0; // convert our input string to number - C++ style
stringstream s; //使用stringstream來負責將參數轉換為數字
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));
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.data;
for( int i = 0; i < 256; ++i)
p[i] = table[i];
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;
}
Mat& ScanImageAndReduceC(Mat& I, const uchar* const table)
{
// accept only char type matrices
CV_Assert(I.depth() != sizeof(uchar));
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 matrices
CV_Assert(I.depth() != sizeof(uchar));
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 matrices
CV_Assert(I.depth() != sizeof(uchar));
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)];
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;
}
作者:csdn博客 鐘桓