【视觉SLAM】 十四讲ch5习题

1.*寻找一个相机(你手机或笔记本的摄像头即可),标定它的内参。你可能会用到标定板,或者自己打印一张标定用的棋盘格。

参考我之前写过的这篇博客:【OpenCV】 相机标定 calibrateCamera

Code来源是《学习OpenCV3》18.01的例程,笔者做了注释补充,Code如下:

// Example 18-1. Reading a chessboard’s width and height, reading and collecting
// the requested number of views, and calibrating the camera
#include <iostream>
#include <opencv2/opencv.hpp>using std::vector;
using std::cout;
using std::cerr;
using std::endl;void help(char **argv) {  // todo rewrite thiscout << "\n\n"<< "Example 18-1:\nReading a chessboard’s width and height,\n"<< "              reading and collecting the requested number of views,\n" << "              and calibrating the camera\n\n" << "Call:\n" << argv[0] << " <board_width> <board_height> <number_of_boards> <if_video,_delay_between_framee_capture> <image_scaling_factor>\n\n"<< "Example:\n" << argv[0] << " 9 6 15 500 0.5\n"<< "-- to use the checkerboard9x6.png provided\n\n"<< " * First it reads in checker boards and calibrates itself\n" << " * Then it saves and reloads the calibration matricies\n"<< " * Then it creates an undistortion map and finally\n"<< " * It displays an undistorted image\n"<< endl;
}int main(int argc, char *argv[]) {int n_boards = 0;           // will be set by input listfloat image_sf = 0.5f;      // image scaling factorfloat delay = 1.f;int board_w = 0;int board_h = 0;if (argc < 4 || argc > 6) {cout << "\nERROR: Wrong number of input parameters\n";help(argv);return -1;}board_w = atoi(argv[1]);board_h = atoi(argv[2]);n_boards = atoi(argv[3]);if (argc > 4) {delay = atof(argv[4]);}if (argc > 5) {image_sf = atof(argv[5]);}/*image_sf是一个用来缩放图像的尺度因子(scale factor)。            角点检测的稳定性: 通过缩放图像,可以在一定程度上增强棋盘格角点检测的稳定性。有时候,一些图像处理算法对于图像的尺寸大小比较敏感,通过调整图像的尺寸可以帮助算法更好地检测到棋盘格角点。提高匹配精度: 调整图像的尺寸可以使棋盘格角点在缩小或放大后仍然保持一定的像素间距,有助于提高匹配的准确性。这样在后续的相机标定过程中,可以更准确地匹配到对应的图像坐标和三维空间坐标。加快算法速度: 在某些情况下,缩小图像的尺寸可以减少计算量,从而加快算法的执行速度。特别是对于较大分辨率的图像,通过缩小尺寸可以降低处理的复杂度,提高算法的效率。*/int board_n = board_w * board_h;cv::Size board_sz = cv::Size(board_w, board_h);cv::VideoCapture capture(0);if (!capture.isOpened()) {cout << "\nCouldn't open the camera\n";help(argv);return -1;}// ALLOCATE STORAGE//vector<vector<cv::Point2f> > image_points;vector<vector<cv::Point3f> > object_points;// Capture corner views: loop until we've got n_boards successful// captures (all corners on the board are found).//double last_captured_timestamp = 0;cv::Size image_size;while (image_points.size() < (size_t)n_boards) {cv::Mat image0, image;capture >> image0;image_size = image0.size();cv::resize(image0, image, cv::Size(), image_sf, image_sf, cv::INTER_LINEAR);// Find the board//vector<cv::Point2f> corners;bool found = cv::findChessboardCorners(image, board_sz, corners);// Draw it//drawChessboardCorners(image, board_sz, corners, found);// If we got a good board, add it to our data//double timestamp = static_cast<double>(clock()) / CLOCKS_PER_SEC;if (found && timestamp - last_captured_timestamp > 1) {last_captured_timestamp = timestamp;image ^= cv::Scalar::all(255);/*代码使用了异或运算符 ^,并且将图像 image 与所有像素值为255的白色 cv::Scalar 进行异或操作。换句话说,这行代码将图像中所有像素值与255进行逐位异或操作,实现了反转图像像素的效果。具体来说,对于每个像素的每个通道,如果像素值为0,异或255后为255;如果像素值为255,异或255后为0。因此,总体上就是将原图像中的亮度值取反,白色变为黑色,黑色变为白色,实现了一种图像反色的效果。这种操作常常用于在图像处理中实现一些效果,比如图像的反色展示或者图像的二值化处理。*/cv::Mat mcorners(corners);/*将存储棋盘格角点的corners向量转换为cv::Mat类型的mcorners矩阵,并对矩阵中的数据进行缩放操作。具体解释如下:cv::Mat mcorners(corners);: 这一行代码通过将corners向量作为参数,创建了一个新的cv::Mat类型的mcorners矩阵。这将导致mcorners矩阵拥有与corners向量相同的数据内容。mcorners *= (1.0 / image_sf);: 这行代码对mcorners矩阵中的所有元素进行缩放操作。具体地,每个元素除以image_sf,这样就实现了对角点坐标的缩放操作。这是由于之前将图像缩放了image_sf倍,而角点坐标也需要相应进行缩放处理。通过这两行代码,将角点坐标存储在corners向量中的数据转换为了cv::Mat类型的矩阵,并对矩阵中的数据进行了缩放操作。这是为了保持角点坐标和图像坐标的一致性。*/// 后续似乎没有用到// do not copy the datamcorners *= (1.0 / image_sf);// scale the corner coordinatesimage_points.push_back(corners);object_points.push_back(vector<cv::Point3f>());vector<cv::Point3f> &opts = object_points.back();opts.resize(board_n);for (int j = 0; j < board_n; j++) {opts[j] = cv::Point3f(static_cast<float>(j / board_w),static_cast<float>(j % board_w), 0.0f);}cout << "Collected our " << static_cast<uint>(image_points.size())<< " of " << n_boards << " needed chessboard images\n" << endl;}cv::imshow("Calibration", image);// show in color if we did collect the imageif ((cv::waitKey(30) & 255) == 27)return -1;}// END COLLECTION WHILE LOOP.cv::destroyWindow("Calibration");cout << "\n\n*** CALIBRATING THE CAMERA...\n" << endl;// CALIBRATE THE CAMERA!//cv::Mat intrinsic_matrix, distortion_coeffs;double err = cv::calibrateCamera(object_points, image_points, image_size, intrinsic_matrix,distortion_coeffs, cv::noArray(), cv::noArray(),cv::CALIB_ZERO_TANGENT_DIST | cv::CALIB_FIX_PRINCIPAL_POINT);/*err表示相机标定的平均重投影误差。在 OpenCV 中,calibrateCamera 函数会返回相机标定的重投影误差,即用标定结果对相机拍摄的图像进行重投影并计算误差的平均值。*//*一些常见的重投影误差标准范围:一般标准: 对于一般应用,重投影误差在0.1到1像素之间可以被接受。精度要求高: 对于需要高精度测量的应用,重投影误差应该控制在0.1像素以下。实时应用: 对于实时图像处理或运动跟踪等应用,重投影误差在1到2像素之间可能是可以接受的。*/// SAVE THE INTRINSICS AND DISTORTIONScout << " *** DONE!\n\nReprojection error is " << err<< "\nStoring Intrinsics.xml and Distortions.xml files\n\n";cv::FileStorage fs("intrinsics.xml", cv::FileStorage::WRITE);fs << "image_width" << image_size.width << "image_height" << image_size.height<< "camera_matrix" << intrinsic_matrix << "distortion_coefficients"<< distortion_coeffs;fs.release();// EXAMPLE OF LOADING THESE MATRICES BACK IN:fs.open("intrinsics.xml", cv::FileStorage::READ);cout << "\nimage width: " << static_cast<int>(fs["image_width"]);cout << "\nimage height: " << static_cast<int>(fs["image_height"]);cv::Mat intrinsic_matrix_loaded, distortion_coeffs_loaded;fs["camera_matrix"] >> intrinsic_matrix_loaded;fs["distortion_coefficients"] >> distortion_coeffs_loaded;cout << "\nintrinsic matrix:" << intrinsic_matrix_loaded;cout << "\ndistortion coefficients: " << distortion_coeffs_loaded << endl;// Build the undistort map which we will use for all// subsequent frames.//cv::Mat map1, map2;cv::initUndistortRectifyMap(intrinsic_matrix_loaded, distortion_coeffs_loaded,cv::Mat(), intrinsic_matrix_loaded, image_size,CV_16SC2, map1, map2);// Just run the camera to the screen, now showing the raw and// the undistorted image.//for (;;) {cv::Mat image, image0;capture >> image0;if (image0.empty()) {break;}cv::remap(image0, image, map1, map2, cv::INTER_LINEAR,cv::BORDER_CONSTANT, cv::Scalar());cv::imshow("Undistorted", image);if ((cv::waitKey(30) & 255) == 27) {break;}}return 0;
}

2.叙述相机内参的物理意义。如果一个相机的分辨率变成两倍而其他地方不变,它的内参如何变化?


3.搜索特殊的相机(鱼眼或全景)相机的标定方法。它们与普通的针孔模型有何不同?

鱼眼相机模型和普通针孔相机模型的标定流程类似,但是鱼眼相机在结构上比普通相机的透镜更多,引入了更大的径向畸变。

因为,鱼眼相机的投影模型为了将尽可能大的场景投影到有限的图像平面内,允许了相机畸变的存在。并且由于鱼眼相机的径向畸变非常严重,所以鱼眼相机主要的是考虑径向畸变,而忽略其余类型的畸变。

为了将尽可能大的场景投影到有限的图像平面内,鱼眼相机会按照一定的投影函数来设计。根据投影函数的不同,鱼眼相机的设计模型大致能被分为四种:等距投影模型、等立体角投影模型、正交投影模型和体视投影模型。

总的来说,就是在标定时采用的畸变模型不同。以OpenCV为例,OpenCV求解相机参数分为两步:1、求解焦距和偏移,2、求解畸变参数。鱼眼相机与普通相机第一步大致相同,但鱼眼相机标定时畸变模型一般使用,五元素形式(k1,k2,p1,p2和k3)。详细请参考【OpenCV】 相机标定 calibrateCamera和鱼眼相机成像模型以及基于OpenCV标定鱼眼镜头

笔者没有接触过全景相机,请参考【计算机视觉】全景相机标定(MATLAB/opencv)

4.调研全局快门相机(global shutter)和卷帘快门相机(rolling shutter)的异同。它们在SLAM 中有何优缺点?

全局快门相机通过整幅场景在同一时间曝光实现的(CCD是全局快门)。Sensor所有像素点同时收集光线,同时曝光。即在曝光开始的时候,Sensor开始收集光线;在曝光结束的时候,光线收集电路被切断。然后Sensor值读出即为一幅照片。全局快门曝光时间更短,容易产生噪点。由于硬件成本,帧率一般比同价位的卷帘快门低。尤其是对于抓拍高速运动的物体有劣势:由于技术限制,很难将巨大的数据量同时处理。

卷帘快门相机通过Sensor逐行曝光的方式实现的(CMOS大多是卷帘快门)。在曝光开始的时候,Sensor逐行扫描逐行进行曝光,直至所有像素点都被曝光。当然,所有的动作在极短的时间内完成。不同行像元的曝光时间不同。大多数CMOS传感器采用这一快门。当物体或者相机在高速运动时会出现果冻效应。

果冻效应

用卷帘快门方式拍摄,逐行扫描速度不够,拍摄结果就可能出现"倾斜"、"摇摆不定"或"部分曝光"等情况。这种卷帘快门方式拍摄出现的现象,就定义为果冻效应。

总结:对于高速移动的物体来说,卷帘快门容易出现扭曲现象(果冻效应)。用Global shutter方式拍摄,假如曝光时间过长,照片会产生像糊现象。而且由于技术限制,很难将巨大的数据量同时处理。


5.RGB-D 相机是如何标定的?以Kinect 为例,需要标定哪些参数?

(参照GitHub - code-iai/iai_kinect2: Tools for using the Kinect One (Kinect v2) in ROS.)

Kinect2由普通RGB相机、红外相机和红外投射器组成,其中红外相机和红外投射器共同组成深度相机。

根据Kinect2结构,所以需要标定参数比普通RGB相机多出来红外相机的相机内参和畸变参数以及普通RGB相机、红外相机的外参(位姿变换矩阵)和深度图的深度值校准。

故需要标定参数为:

RGB相机:相机内参和畸变参数;

红外相机:相机内参和畸变参数;

RGB相机和红外相机的外参(位姿变换矩阵);

深度图的深度值校准;

Kinect2标定原理详情请参考:RGB-D相机的标定与图像配准


6.除了示例程序演示的遍历图像的方式,你还能举出哪些遍历图像的方法?

《OpenCV3编程入门》配套示例程序提供了14种之多。

Code如下:

//--------------------------------------【程序说明】-------------------------------------------
//		程序说明:《OpenCV3编程入门》OpenCV2版书本配套示例程序24
//		程序描述:来自一本国外OpenCV2书籍的示例-遍历图像像素的14种方法
//		测试所用IDE版本:Visual Studio 2010
//		测试所用OpenCV版本:	2.4.9
//		2014年11月 Revised by @浅墨_毛星云
//------------------------------------------------------------------------------------------------/*------------------------------------------------------------------------------------------*\This file contains material supporting chapter 2 of the cookbook:Computer Vision Programming using the OpenCV Library.by Robert Laganiere, Packt Publishing, 2011.This program is free software; permission is hereby granted to use, copy, modify,and distribute this source code, or portions thereof, for any purpose, without fee,subject to the restriction that the copyright notice may not be removedor altered from any source or altered source distribution.The software is released on an as-is basis and without any warranties of any kind.In particular, the software is not guaranteed to be fault-tolerant or free from failure.The author disclaims all warranties with regard to this software, any use,and any consequent failure, is purely the responsibility of the user.Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
\*------------------------------------------------------------------------------------------*///---------------------------------【头文件、命名空间包含部分】-----------------------------
//		描述:包含程序所使用的头文件和命名空间
//-------------------------------------------------------------------------------------------------
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;//---------------------------------【宏定义部分】---------------------------------------------
//		描述:包含程序所使用宏定义
//-------------------------------------------------------------------------------------------------
#define NTESTS 14
#define NITERATIONS 20//----------------------------------------- 【方法一】-------------------------------------------
//		说明:利用.ptr 和 []
//-------------------------------------------------------------------------------------------------
void colorReduce0(Mat& image, int div = 64) {int nl = image.rows; //行数int nc = image.cols * image.channels(); //每行元素的总元素数量for (int j = 0; j < nl; j++){uchar* data = image.ptr<uchar>(j);for (int i = 0; i < nc; i++){//-------------开始处理每个像素-------------------data[i] = data[i] / div * div + div / 2;//-------------结束像素处理------------------------} //单行处理结束                  }
}//-----------------------------------【方法二】-------------------------------------------------
//		说明:利用 .ptr 和 * ++ 
//-------------------------------------------------------------------------------------------------
void colorReduce1(Mat& image, int div = 64) {int nl = image.rows; //行数int nc = image.cols * image.channels(); //每行元素的总元素数量for (int j = 0; j < nl; j++){uchar* data = image.ptr<uchar>(j);for (int i = 0; i < nc; i++){//-------------开始处理每个像素-------------------*data++ = *data / div * div + div / 2;//-------------结束像素处理------------------------} //单行处理结束              }
}//-----------------------------------------【方法三】-------------------------------------------
//		说明:利用.ptr 和 * ++ 以及模操作
//-------------------------------------------------------------------------------------------------
void colorReduce2(Mat& image, int div = 64) {int nl = image.rows; //行数int nc = image.cols * image.channels(); //每行元素的总元素数量for (int j = 0; j < nl; j++){uchar* data = image.ptr<uchar>(j);for (int i = 0; i < nc; i++){//-------------开始处理每个像素-------------------int v = *data;*data++ = v - v % div + div / 2;//-------------结束像素处理------------------------} //单行处理结束                   }
}//----------------------------------------【方法四】---------------------------------------------
//		说明:利用.ptr 和 * ++ 以及位操作
//----------------------------------------------------------------------------------------------------
void colorReduce3(Mat& image, int div = 64) {int nl = image.rows; //行数int nc = image.cols * image.channels(); //每行元素的总元素数量int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0));//掩码值uchar mask = 0xFF << n; // e.g. 对于 div=16, mask= 0xF0for (int j = 0; j < nl; j++) {uchar* data = image.ptr<uchar>(j);for (int i = 0; i < nc; i++) {//------------开始处理每个像素-------------------*data++ = *data & mask + div / 2;//-------------结束像素处理------------------------}  //单行处理结束            }
}//----------------------------------------【方法五】----------------------------------------------
//		说明:利用指针算术运算
//---------------------------------------------------------------------------------------------------
void colorReduce4(Mat& image, int div = 64) {int nl = image.rows; //行数int nc = image.cols * image.channels(); //每行元素的总元素数量int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0));int step = image.step; //有效宽度//掩码值uchar mask = 0xFF << n; // e.g. 对于 div=16, mask= 0xF0//获取指向图像缓冲区的指针uchar* data = image.data;for (int j = 0; j < nl; j++){for (int i = 0; i < nc; i++){//-------------开始处理每个像素-------------------*(data + i) = *data & mask + div / 2;//-------------结束像素处理------------------------} //单行处理结束              data += step;  // next line}
}//---------------------------------------【方法六】----------------------------------------------
//		说明:利用 .ptr 和 * ++以及位运算、image.cols * image.channels()
//-------------------------------------------------------------------------------------------------
void colorReduce5(Mat& image, int div = 64) {int nl = image.rows; //行数int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0));//掩码值uchar mask = 0xFF << n; // e.g. 例如div=16, mask= 0xF0for (int j = 0; j < nl; j++){uchar* data = image.ptr<uchar>(j);for (int i = 0; i < image.cols * image.channels(); i++){//-------------开始处理每个像素-------------------*data++ = *data & mask + div / 2;//-------------结束像素处理------------------------} //单行处理结束            }
}// -------------------------------------【方法七】----------------------------------------------
//		说明:利用.ptr 和 * ++ 以及位运算(continuous)
//-------------------------------------------------------------------------------------------------
void colorReduce6(Mat& image, int div = 64) {int nl = image.rows; //行数int nc = image.cols * image.channels(); //每行元素的总元素数量if (image.isContinuous()){//无填充像素nc = nc * nl;nl = 1;  // 为一维数列}int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0));//掩码值uchar mask = 0xFF << n; // e.g. 比如div=16, mask= 0xF0for (int j = 0; j < nl; j++) {uchar* data = image.ptr<uchar>(j);for (int i = 0; i < nc; i++) {//-------------开始处理每个像素-------------------*data++ = *data & mask + div / 2;//-------------结束像素处理------------------------} //单行处理结束                   }
}//------------------------------------【方法八】------------------------------------------------
//		说明:利用 .ptr 和 * ++ 以及位运算 (continuous+channels)
//-------------------------------------------------------------------------------------------------
void colorReduce7(Mat& image, int div = 64) {int nl = image.rows; //行数int nc = image.cols; //列数if (image.isContinuous()){//无填充像素nc = nc * nl;nl = 1;  // 为一维数组}int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0));//掩码值uchar mask = 0xFF << n; // e.g. 比如div=16, mask= 0xF0for (int j = 0; j < nl; j++) {uchar* data = image.ptr<uchar>(j);for (int i = 0; i < nc; i++) {//-------------开始处理每个像素-------------------*data++ = *data & mask + div / 2;*data++ = *data & mask + div / 2;*data++ = *data & mask + div / 2;//-------------结束像素处理------------------------} //单行处理结束                    }
}// -----------------------------------【方法九】 ------------------------------------------------
//		说明:利用Mat_ iterator
//-------------------------------------------------------------------------------------------------
void colorReduce8(Mat& image, int div = 64) {//获取迭代器Mat_<Vec3b>::iterator it = image.begin<Vec3b>();Mat_<Vec3b>::iterator itend = image.end<Vec3b>();for (; it != itend; ++it) {//-------------开始处理每个像素-------------------(*it)[0] = (*it)[0] / div * div + div / 2;(*it)[1] = (*it)[1] / div * div + div / 2;(*it)[2] = (*it)[2] / div * div + div / 2;//-------------结束像素处理------------------------}//单行处理结束  
}//-------------------------------------【方法十】-----------------------------------------------
//		说明:利用Mat_ iterator以及位运算
//-------------------------------------------------------------------------------------------------
void colorReduce9(Mat& image, int div = 64) {// div必须是2的幂int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0));//掩码值uchar mask = 0xFF << n; // e.g. 比如 div=16, mask= 0xF0// 获取迭代器Mat_<Vec3b>::iterator it = image.begin<Vec3b>();Mat_<Vec3b>::iterator itend = image.end<Vec3b>();//扫描所有元素for (; it != itend; ++it){//-------------开始处理每个像素-------------------(*it)[0] = (*it)[0] & mask + div / 2;(*it)[1] = (*it)[1] & mask + div / 2;(*it)[2] = (*it)[2] & mask + div / 2;//-------------结束像素处理------------------------}//单行处理结束  
}//------------------------------------【方法十一】---------------------------------------------
//		说明:利用Mat Iterator_
//-------------------------------------------------------------------------------------------------
void colorReduce10(Mat& image, int div = 64) {//获取迭代器Mat_<Vec3b> cimage = image;Mat_<Vec3b>::iterator it = cimage.begin();Mat_<Vec3b>::iterator itend = cimage.end();for (; it != itend; it++) {//-------------开始处理每个像素-------------------(*it)[0] = (*it)[0] / div * div + div / 2;(*it)[1] = (*it)[1] / div * div + div / 2;(*it)[2] = (*it)[2] / div * div + div / 2;//-------------结束像素处理------------------------}
}//--------------------------------------【方法十二】--------------------------------------------
//		说明:利用动态地址计算配合at
//-------------------------------------------------------------------------------------------------
void colorReduce11(Mat& image, int div = 64) {int nl = image.rows; //行数int nc = image.cols; //列数for (int j = 0; j < nl; j++){for (int i = 0; i < nc; i++){//-------------开始处理每个像素-------------------image.at<Vec3b>(j, i)[0] = image.at<Vec3b>(j, i)[0] / div * div + div / 2;image.at<Vec3b>(j, i)[1] = image.at<Vec3b>(j, i)[1] / div * div + div / 2;image.at<Vec3b>(j, i)[2] = image.at<Vec3b>(j, i)[2] / div * div + div / 2;//-------------结束像素处理------------------------} //单行处理结束                 }
}//----------------------------------【方法十三】----------------------------------------------- 
//		说明:利用图像的输入与输出
//-------------------------------------------------------------------------------------------------
void colorReduce12(const Mat& image, //输入图像Mat& result,      // 输出图像int div = 64) {int nl = image.rows; //行数int nc = image.cols; //列数//准备好初始化后的Mat给输出图像result.create(image.rows, image.cols, image.type());//创建无像素填充的图像nc = nc * nl;nl = 1;  //单维数组int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0));//掩码值uchar mask = 0xFF << n; // e.g.比如div=16, mask= 0xF0for (int j = 0; j < nl; j++) {uchar* data = result.ptr<uchar>(j);const uchar* idata = image.ptr<uchar>(j);for (int i = 0; i < nc; i++) {//-------------开始处理每个像素-------------------*data++ = (*idata++) & mask + div / 2;*data++ = (*idata++) & mask + div / 2;*data++ = (*idata++) & mask + div / 2;//-------------结束像素处理------------------------} //单行处理结束                   }
}//--------------------------------------【方法十四】------------------------------------------- 
//		说明:利用操作符重载
//-------------------------------------------------------------------------------------------------
void colorReduce13(Mat& image, int div = 64) {int n = static_cast<int>(log(static_cast<double>(div)) / log(2.0));//掩码值uchar mask = 0xFF << n; // e.g. 比如div=16, mask= 0xF0//进行色彩还原image = (image & Scalar(mask, mask, mask)) + Scalar(div / 2, div / 2, div / 2);
}//-----------------------------------【ShowHelpText( )函数】-----------------------------
//		描述:输出一些帮助信息
//----------------------------------------------------------------------------------------------
void ShowHelpText()
{//输出欢迎信息和OpenCV版本printf("\n\n\t\t\t非常感谢购买《OpenCV3编程入门》一书!\n");printf("\n\n\t\t\t此为本书OpenCV2版的第24个配套示例程序\n");printf("\n\n\t\t\t   当前使用的OpenCV版本为:" CV_VERSION);printf("\n\n  ----------------------------------------------------------------------------\n");printf("\n\n正在进行存取操作,请稍等……\n\n");
}//-----------------------------------【main( )函数】--------------------------------------------
//		描述:控制台应用程序的入口函数,我们的程序从这里开始
//-------------------------------------------------------------------------------------------------
int main()
{int64 t[NTESTS], tinit;Mat image0;Mat image1;Mat image2;//system("color 4F");ShowHelpText();image0 = imread("1.png");if (!image0.data)return 0;//时间值设为0for (int i = 0; i < NTESTS; i++)t[i] = 0;// 多次重复测试int n = NITERATIONS;for (int k = 0; k < n; k++){cout << k << " of " << n << endl;image1 = imread("1.png");//【方法一】利用.ptr 和 []tinit = getTickCount();colorReduce0(image1);t[0] += getTickCount() - tinit;//【方法二】利用 .ptr 和 * ++ image1 = imread("1.png");tinit = getTickCount();colorReduce1(image1);t[1] += getTickCount() - tinit;//【方法三】利用.ptr 和 * ++ 以及模操作image1 = imread("1.png");tinit = getTickCount();colorReduce2(image1);t[2] += getTickCount() - tinit;//【方法四】 利用.ptr 和 * ++ 以及位操作image1 = imread("1.png");tinit = getTickCount();colorReduce3(image1);t[3] += getTickCount() - tinit;//【方法五】 利用指针的算术运算image1 = imread("1.png");tinit = getTickCount();colorReduce4(image1);t[4] += getTickCount() - tinit;//【方法六】利用 .ptr 和 * ++以及位运算、image.cols * image.channels()image1 = imread("1.png");tinit = getTickCount();colorReduce5(image1);t[5] += getTickCount() - tinit;//【方法七】利用.ptr 和 * ++ 以及位运算(continuous)image1 = imread("1.png");tinit = getTickCount();colorReduce6(image1);t[6] += getTickCount() - tinit;//【方法八】利用 .ptr 和 * ++ 以及位运算 (continuous+channels)image1 = imread("1.png");tinit = getTickCount();colorReduce7(image1);t[7] += getTickCount() - tinit;//【方法九】 利用Mat_ iteratorimage1 = imread("1.png");tinit = getTickCount();colorReduce8(image1);t[8] += getTickCount() - tinit;//【方法十】 利用Mat_ iterator以及位运算image1 = imread("1.png");tinit = getTickCount();colorReduce9(image1);t[9] += getTickCount() - tinit;//【方法十一】利用Mat Iterator_image1 = imread("1.png");tinit = getTickCount();colorReduce10(image1);t[10] += getTickCount() - tinit;//【方法十二】 利用动态地址计算配合atimage1 = imread("1.png");tinit = getTickCount();colorReduce11(image1);t[11] += getTickCount() - tinit;//【方法十三】 利用图像的输入与输出image1 = imread("1.png");tinit = getTickCount();Mat result;colorReduce12(image1, result);t[12] += getTickCount() - tinit;image2 = result;//【方法十四】 利用操作符重载image1 = imread("1.png");tinit = getTickCount();colorReduce13(image1);t[13] += getTickCount() - tinit;//------------------------------}//输出图像   imshow("原始图像", image0);imshow("结果", image2);imshow("图像结果", image1);// 输出平均执行时间cout << endl << "-------------------------------------------" << endl << endl;cout << "\n【方法一】利用.ptr 和 []的方法所用时间为 " << 1000. * t[0] / getTickFrequency() / n << "ms" << endl;cout << "\n【方法二】利用 .ptr 和 * ++ 的方法所用时间为" << 1000. * t[1] / getTickFrequency() / n << "ms" << endl;cout << "\n【方法三】利用.ptr 和 * ++ 以及模操作的方法所用时间为" << 1000. * t[2] / getTickFrequency() / n << "ms" << endl;cout << "\n【方法四】利用.ptr 和 * ++ 以及位操作的方法所用时间为" << 1000. * t[3] / getTickFrequency() / n << "ms" << endl;cout << "\n【方法五】利用指针算术运算的方法所用时间为" << 1000. * t[4] / getTickFrequency() / n << "ms" << endl;cout << "\n【方法六】利用 .ptr 和 * ++以及位运算、channels()的方法所用时间为" << 1000. * t[5] / getTickFrequency() / n << "ms" << endl;cout << "\n【方法七】利用.ptr 和 * ++ 以及位运算(continuous)的方法所用时间为" << 1000. * t[6] / getTickFrequency() / n << "ms" << endl;cout << "\n【方法八】利用 .ptr 和 * ++ 以及位运算 (continuous+channels)的方法所用时间为" << 1000. * t[7] / getTickFrequency() / n << "ms" << endl;cout << "\n【方法九】利用Mat_ iterator 的方法所用时间为" << 1000. * t[8] / getTickFrequency() / n << "ms" << endl;cout << "\n【方法十】利用Mat_ iterator以及位运算的方法所用时间为" << 1000. * t[9] / getTickFrequency() / n << "ms" << endl;cout << "\n【方法十一】利用Mat Iterator_的方法所用时间为" << 1000. * t[10] / getTickFrequency() / n << "ms" << endl;cout << "\n【方法十二】利用动态地址计算配合at 的方法所用时间为" << 1000. * t[11] / getTickFrequency() / n << "ms" << endl;cout << "\n【方法十三】利用图像的输入与输出的方法所用时间为" << 1000. * t[12] / getTickFrequency() / n << "ms" << endl;cout << "\n【方法十四】利用操作符重载的方法所用时间为" << 1000. * t[13] / getTickFrequency() / n << "ms" << endl;waitKey();return 0;
}

运行结果

其中:方法5利用指针算术、方法八利用 .ptr 和*++ 以及位运算(continuous+channels)以及方法14利用操作符重载的方法最佳。

7.学习OpenCV基本用法

推荐两本学习OpenCV基本用法参考书:

1、《学习OpenCV3》

2、《OpenCV3编程入门》

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1486500.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

全国区块链职业技能大赛国赛考题前端功能开发

任务3-1:区块链应用前端功能开发 1.请基于前端系统的开发模板,在登录组件login.js、组件管理文件components.js中添加对应的逻辑代码,实现对前端的角色选择功能,并测试功能完整性,示例页面如下: 具体要求如下: (1)有明确的提示,提示用户选择角色; (2)用户可看…

2024年第二季度 DDoS 威胁趋势报告

2024 年上半年&#xff0c;Cloudflare 缓解了 850 万次 DDoS 攻击&#xff1a;第一季度 450 万次&#xff0c;第二季度 400 万次。总体而言&#xff0c;第二季度 DDoS 攻击数量环比下降了 11%&#xff0c;但同比增长了 20%。 DDoS 攻击分布&#xff08;按类型和手段&#xff09…

pytorch学习(十一)checkpoint

当训练一个大模型数据的时候&#xff0c;中途断电就可以造成已经训练几天或者几个小时的工作白做了&#xff0c;再此训练的时候需要从epoch0开始训练&#xff0c;因此中间要不断保存&#xff08;epoch&#xff0c;net&#xff0c;optimizer&#xff0c;scheduler&#xff09;等…

Java | Leetcode Java题解之第274题H指数

题目&#xff1a; 题解&#xff1a; class Solution {public int hIndex(int[] citations) {int left0,rightcitations.length;int mid0,cnt0;while(left<right){// 1 防止死循环mid(leftright1)>>1;cnt0;for(int i0;i<citations.length;i){if(citations[i]>mi…

Vue 3 实现左侧列表点击跳转滚动到右侧对应区域的功能

使用 Vue 3 实现左侧列表点击跳转到右侧对应区域的功能 1. 引言 在这篇博客中&#xff0c;我们将展示如何使用 Vue 3 实现一个简单的页面布局&#xff0c;其中左侧是一个列表&#xff0c;点击列表项时&#xff0c;右侧会平滑滚动到对应的内容区域。这种布局在很多应用场景中都…

金字塔思维:打造清晰有力的分析报告与沟通技巧

金字塔思维&#xff1a;打造清晰有力的分析报告与沟通技巧 在职场中&#xff0c;撰写一份条理清晰、逻辑严谨、说服力强的分析报告是每位职场人士必备的技能。然而&#xff0c;许多人在完成报告后常常感到思路混乱&#xff0c;表达不清。为了帮助大家解决这一问题&#xff0c;本…

VSCode部署Pytorch机器学习框架使用Anaconda(Window版)

目录 1. 配置Anaconda1.1下载安装包1. Anaconda官网下载2, 安装Anaconda 1.2 创建虚拟环境1.3 常用命令Conda 命令调试和日常维护 1.4 可能遇到的问题执行上述步骤后虚拟环境仍在C盘 2. 配置cuda2.1 查看显卡支持的cuda版本2.2 下载对应cuda版本2.3 下载对应的pytorch可能出现的…

学习React(状态管理)

随着你的应用不断变大&#xff0c;更有意识的去关注应用状态如何组织&#xff0c;以及数据如何在组件之间流动会对你很有帮助。冗余或重复的状态往往是缺陷的根源。在本节中&#xff0c;你将学习如何组织好状态&#xff0c;如何保持状态更新逻辑的可维护性&#xff0c;以及如何…

SQL 简单查询

目录 一、投影查询 1、指定特定列查询 2、修改返回列名查询 3、计算值查询 二、选择查询 1、使用关系表达式 2、使用逻辑表达式 3、使用 BETWEEN关键字 4、使用 IN关键字 5、使用 LIKE关键字 6、使用 IS NULL/ NOT NULL关键字 7、符合条件查询 三、聚合函数查询 一…

vuepress搭建个人文档

vuepress搭建个人文档 文章目录 vuepress搭建个人文档前言一、VuePress了解二、vuepress-reco主题个人博客搭建三、vuepress博客部署四、vuepress后续补充 总结 vuepress搭建个人文档 所属目录&#xff1a;项目研究创建时间&#xff1a;2024/7/23作者&#xff1a;星云<Xing…

Nuxt 使用指南:掌握 useNuxtApp 和运行时上下文

title: Nuxt 使用指南&#xff1a;掌握 useNuxtApp 和运行时上下文 date: 2024/7/21 updated: 2024/7/21 author: cmdragon excerpt: 摘要&#xff1a;“Nuxt 使用指南&#xff1a;掌握 useNuxtApp 和运行时上下文”介绍了Nuxt 3中useNuxtApp的使用&#xff0c;包括访问Vue实…

[Spring] Spring日志

&#x1f338;个人主页:https://blog.csdn.net/2301_80050796?spm1000.2115.3001.5343 &#x1f3f5;️热门专栏: &#x1f9ca; Java基本语法(97平均质量分)https://blog.csdn.net/2301_80050796/category_12615970.html?spm1001.2014.3001.5482 &#x1f355; Collection与…

python实现责任链模式

把多个处理方法串成一个list。下一个list的节点是上一个list的属性。 每个节点都有判断是否能处理当前数据的方法。能处理&#xff0c;则直接处理&#xff0c;不能处理则调用下一个节点&#xff08;也就是当前节点的属性&#xff09;来进行处理。 Python 实现责任链模式&#…

在浏览器中测试JavaScript代码方法简要介绍

在浏览器中测试JavaScript代码方法简要介绍 在浏览器中测试JavaScript代码是前端开发中的一个重要技能。方法如下&#xff1a; 1. 浏览器控制台 最简单和直接的方法是使用浏览器的开发者工具中的控制台&#xff08;Console&#xff09;。 步骤&#xff1a; 在大多数浏览器…

Unity 之 【Android Unity 共享纹理】之 Android 共享图片给 Unity 显示

Unity 之 【Android Unity 共享纹理】之 Android 共享图片给 Unity 显示 目录 Unity 之 【Android Unity 共享纹理】之 Android 共享图片给 Unity 显示 一、简单介绍 二、共享纹理 1、共享纹理的原理 2、共享纹理涉及到的关键知识点 3、什么可以实现共享 不能实现共享…

有关于链表带环的两道OJ题目

目录 1.判断链表是否带环 1.1快指针的速度为慢指针的2倍 1.2快指针的速度为慢指针的3倍 2.找出带环链表开始入环的第一个节点 2.1将快慢指针相遇的节点与后面分开&#xff0c;构造交叉链表 2.2记录快慢指针相遇节点&#xff0c;与头结点一起向后走&#xff0c;相遇点为入…

远程开启空调,享受即刻凉爽

随着夏季的热浪逐渐侵袭&#xff0c;我们都渴望回到家中那一刻&#xff0c;能感受到一丝丝的凉意。但是&#xff0c;有时候&#xff0c;即使我们提前开窗通风&#xff0c;房间里的温度依然像烤箱一样闷热难耐。那么&#xff0c;有没有一种方法&#xff0c;能让我们在外头酷暑难…

升级Nvidia CUDA 遇到 sub-process /usr/bin/dpkg returned an error code (1)

1.问题描述 在自己Ubuntu22.04的服务器环境上存在cuda版本为11.5&#xff0c;按照官网教程升级为12.1运行安装命令 sudo apt-get -y install cuda 报错&#xff1a;sub-process /usr/bin/dpkg returned an error code (1) 官网教程&#xff1a; https://developer.nvidia…

PCIE软件基础知识

什么是PCIE PCIe&#xff0c;全称 Peripheral Component Interconnect Express&#xff0c;是一种高速串行计算机扩展总线标准&#xff0c;用于连接计算机内部的硬件组件&#xff0c;如显卡、存储设备、网络适配器等。PCIe是一种点对点的双向通信标准&#xff0c;这意味着它在发…

微信小程序canvas 使用案例(一)

一、cavans 对象获取、上线文创建 1.wxml <!-- canvas.wxml --><canvas type"2d" id"myCanvas"></canvas> 2.js /*** 生命周期函数--监听页面加载*/onLoad(options) {const query wx.createSelectorQuery()query.select(#myCanvas).f…