1.簡單的方法:下標法cv::at(x, y)
類似C語言的二維數組遍歷,通過下標(x, y)來訪問數據元素,在opencv中用img.at<T>(x, y)來訪問圖像的元素(像素點)。其中img是Mat類的對象,該對象調用成員函數at<T>(x, y),這個函數是模板函數,T是元素的類型,可以是uchar,也可以是Vec3b類型,等等。(x, y)分別是在圖像的位置行和列。
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
using namespace std;
using namespace cv;
int main()
{
double t;
namedWindow("src");
namedWindow("dst");
Mat src, src2, dst;
src = imread("./src/img4.jpg", IMREAD_UNCHANGED);
dst = Mat::zeros(src.size(), src.type());
int channel = src.channels(); //獲取圖像的通道數量
float a = 1.5;//對比度
int b = 0;//亮度
t = (double)getTickCount();
//對通道數量不同的圖像進行不同處理
switch (channel)
{
case 1:
for (int i = 0; i < src.rows; i++)
for (int j = 0; j < src.cols; j++)
dst.at<uchar>(i, j) = saturate_cast<uchar>(a * src.at<uchar>(i, j) + b);
break;
default:
for (int i = 0; i < src.rows; i++)
{
for (int j = 0; j < src.cols; j++)
{
for (int c = 0; c < src.channels(); c++)
{
dst.at<Vec3b>(i, j)[c] = saturate_cast<uchar>(a * src.at<Vec3b>(i, j)[c] + b);
}
}
}
}
t = ((double)getTickCount() - t) / getTickFrequency();
cout << t << endl;
imshow("src", src);
imshow("dst", dst);
waitKey(0);
return 0;
}
2.有效的方式:通過指針訪問cv::ptr(i = 0)或cv::ptr(i, j)
img.ptr<T>(i = 0)返回T類型的指針,該函數是模板函數,T是元素的類型,默認為uchar類型,img.ptr<T>(i, j)用法類似。
//部分代碼
uchar *p, *pt;
if (channel == 1)
{
for (int i = 0; i < src.rows; i++)
{
pt = src.ptr(i);
p = dst.ptr(i);
for (int j = 0; j < src.cols; j++)
{
p[j] = saturate_cast<uchar>(a * pt[j] + b);
}
}
}
else
{
for (int i = 0; i < src.rows; i++)
{
for (int j = 0; j < src.cols; j++)
{
pt = src.ptr(i,j);
p = dst.ptr(i,j);
for (int c = 0; c < src.channels(); c++)
{
p[c] = saturate_cast<uchar>(a * pt[c] + b);
}
}
}
}
3.通過查找表掃描:cv :: LUT(src, lut, dst )
三個參數都是Mat的對象。src是元素為8位的輸入圖像, dst是輸出結果,大小、元素類型要和src一樣,lut為包含256個元素的查找表
首先我們構建一個Mat類型的查找表:
Mat lookUpTable(1, 256, CV_8U)
uchar *p = lookUpTable.ptr()
for( int i = 0; i < 256; ++i)
p[i] = table[i];
最后調用函數cv :: LUT(src, lut, dst )
總結:
這幾個的效率比較:at() < ptr() < LUT()