由于图片尺寸过大,对图像进行resize处理用到的各种差值方法比较耗费时间,于是手动将图片缩小,每8*8个像素取一个点组成新的图片。
主要考虑两种方式,第一种at方法取图像中的点的用法:image.at<uchar>(i,j):取出灰度图像中i行j列的点;image.at<Vec3b>(i,j)[k]:取出三通道图像中的某一通道对应的值。
第二种(明显较为高效):image.ptr<uchar>(i)。
#include <stdio.h>
#include <opencv.hpp>
#include <cxcore.h>
#include <iostream>
using namespace cv;
using namespace std;
int main(){
double t = getTickCount();
Mat src = imread("test.bmp", 0);
t = double(getTickCount() - t) * 1000 / getTickFrequency();
cout << "the load src pic time is " << t << "ms" << endl;
//double t00 = getTickCount();//start
//int nr = src.rows;
//int nc = src.cols;
//Mat img_src(cv::Size(nc / 8, nr / 8), CV_8U);
//for (int i = 1; i < nr / 8; i++){
// for (int j = 1; j < nc / 8; j++){
// img_src.at<uchar>(i, j) = src.at<uchar>(8 * i , 8 * j);
// }
//}
//t00 = double(getTickCount() - t00) * 1000 / getTickFrequency();
//cout << "the test time=" << t00 << "ms" << endl;//end
///*Mat img_src(cv::Size((nc + 1) / 3,(nr + 1) / 3),CV_8U);
//for (int i=1; i < (nr + 1) / 3; i++){
// for (int j=1; j < (nc + 1) / 3; j++){
// img_src.at<uchar>(i, j) = src.at<uchar>(3 * i - 1, 3 * j - 1);
// }
//}*/
double t00 = getTickCount();//start
int nr = src.rows;
int nc = src.cols;
Mat img_src(cv::Size(nc / 8, nr / 8), CV_8U);
for (int i = 1; i < nr / 8; i++){
//取出原始图像中8*i行的指针,对应新的图像中的out指针
const uchar*inData = src.ptr<uchar>(8*i);
uchar*outData = img_src.ptr<uchar>(i);
for (int j = 1; j < nc / 8; j++){
outData[j] = inData[8*j];
}
}
t00 = double(getTickCount() - t00) * 1000 / getTickFrequency();
cout << "the test time=" << t00 << "ms" << endl;//end
imshow("the resized img", img_src);
//fast detect cornors
//Mat src_copy = src.clone();
double t_fast = getTickCount();
vector<cv::KeyPoint>keypoints;
cv::FastFeatureDetector fast(40, true);
fast.detect(img_src, keypoints);
drawKeypoints(img_src, keypoints, img_src, cv::Scalar::all(255), cv::DrawMatchesFlags::DRAW_OVER_OUTIMG);
t_fast = double(getTickCount() - t_fast) * 1000 / getTickFrequency();
cout << "the fast detect corners time = " << t_fast << "ms" << endl;
cout << "the keypoints size=" << keypoints.size() << endl;
//resize(src, src, cv::Size(0.5*src.cols, 0.5*src.rows));
imshow("corners", img_src);
waitKey();
}
第一种image.at(i,j):用时431ms。
第二种image.ptr(i):用时9.6ms。