參考:http://www.cnblogs.com/ronny/p/opencv_road_2.html
http://blog.csdn.net/xiaowei_cqu/article/details/7771760
http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/core/how_to_scan_images/how_to_scan_images.html
http://segmentfault.com/a/1190000000598650
本文主要是在opencv2.0以後的版本,使用Mat作為資料的基本操作類型。
最快的方法-----使用指針周遊圖像
1 周遊單通道圖像
利用指針通路
Mat src = imread("lena.jpg", 0);
Mat dst = Mat(src.rows, src.cols, CV8U_C1);
for (int i = 0; i < row; ++i)
{
uchar *srcptr = src.ptr<uchar>(i);
uchar *dstptr = dst.ptr<float>(i);
for (int j = 0; j < col; ++j)
{
*(dstptr + j) = *(srcptr + j);
}
}
利用at通路
cv::Mat srcgrayimg;
cv::cvtColor(srcimg, srcgrayimg, CV_BGR2GRAY);
for (int y = facetybegin; y < facetyend; ++y)
{
for (int x = facetybegin; x < facetyend; ++x)
{
FacePoint dstpoint;
dstpoint.x = x;
dstpoint.y = y;
if (isPointInRect(dstpoint, quadrilateralLeft) == true)
{
sumLeft += srcgrayimg.at<uchar>(y, x);
areaLeft++;
}
if (isPointInRect(dstpoint, quadrilateralRight) == true)
{
sumRight += srcgrayimg.at<uchar>(y, x);
areaRight++;
}
}
}
2 周遊彩色圖像
這裡可以通過兩種方式Vec3b和step elemSize兩種方式來通路
Mat src = imread("lena.jpg", 1);
//通過指針周遊彩色圖像
uchar *data = src.data;
int i = 100;
int j = 100;
//擷取第i行 第j列的元素RGB值
//擷取B通道
int pix1 = src.at<Vec3b>(i, j)[0];
int pix2 = *(data + i * src.step + j* src.elemSize()+0);
cout << pix1 << " " << pix2 << endl;
cout << src.step << " " << src.elemSize() << endl;
通過指針,适合與任何通道的圖像
channel = 3
int row = src.rows;
int col = src.cols;
Mat dst = Mat(row, col, CV_16UC3);
for (int i = 0; i < row; ++i)
{
ushort *dataWarpRow = dst.ptr<ushort>(i);
for (int j = 0; j < col; ++j)
{
ushort *dataWarpCol = dataWarpRow + j * src.channels();
if ((dataWarpCol)[0] == 0 && (dataWarpCol)[1] == 0 && (dataWarpCol)[2] == 0)
{
;
}
}
}