不忘初心,方得始终,念念不忘,必有回响 —— 《杭高语录》
头文件 1 2 3 4 5 6 7 #include <opencv2/core.hpp> #include <opencv2/imgproc.hpp> #include <oprencv2/highgui.hpp> #include <iostream> using namespace cv;using namespace std ;
OpenCV库
分为多个模块:
opencv_core
模块包含库的核心功能;
opencv_imgproc
模块包含主要的图像处理函数;
opencv_highgui
模块提供了读写图像和视频的函数以及一些用户交互函数。
图片基操 首先,需要确认调试属性为 Debug x64
装载、显示
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <opencv2/core.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/highgui.hpp> #include <iostream> using namespace cv;using namespace std ;int main () { string path = "D:/Summer Holiday Practice/opencv learning/Resources/OpenCVBook/puppy.bmp" ; Mat image = imread(path); if (image.empty()) { cout << "Read Fail ! ! !" << endl ; return -1 ; } cout << "This image is " << image.rows << " x " << image.cols << endl ; namedWindow("Original Image" ); imshow("Original Image" , image); waitKey(0 ); return 0 ; }
水平翻转、垂直翻转 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 #include <opencv2/core.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/highgui.hpp> #include <iostream> using namespace cv;using namespace std ;int main () { string path = "D:/Summer Holiday Practice/opencv learning/Resources/OpenCVBook/puppy.bmp" ; Mat image = imread(path); if (image.empty()) { cout << "Read Fail ! ! !" << endl ; return -1 ; } namedWindow("Original Image" ); imshow("Original Image" , image); Mat result1, result2; flip(image, result1, 1 ); flip(image, result2, 0 ); namedWindow("Result 1" ); namedWindow("Result 2" ); imshow("Result 1" , result1); imshow("Result 2" , result2); waitKey(0 ); return 0 ; }
存储 我的结果保存在 C:\Users\15595\source\repos\Practice
(Practice 是工程的名字)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include <opencv2/core.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/highgui.hpp> #include <iostream> using namespace cv;using namespace std ;int main () { string path = "D:/Summer Holiday Practice/opencv learning/Resources/OpenCVBook/puppy.bmp" ; Mat image = imread(path); if (image.empty()) { cout << "Read Fail ! ! !" << endl ; return -1 ; } namedWindow("Original Image" ); imshow("Original Image" , image); Mat result1; flip(image, result1, 1 ); namedWindow("Result 1" ); imwrite("Result.jpg" , result1); imshow("Result 1" , result1); waitKey(0 ); return 0 ; }
imwrite
函数要写明写入的格式,.jpg
.tiff
…
转换成灰度图 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include <opencv2/core.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/highgui.hpp> #include <iostream> using namespace cv;using namespace std ;int main () { string path = "D:/Summer Holiday Practice/opencv learning/Resources/OpenCVBook/puppy.bmp" ; Mat image = imread(path); if (image.empty()) { cout << "Read Fail ! ! !" << endl ; return -1 ; } cout << "0" << endl ; Mat grayImg; cvtColor(image, grayImg, COLOR_BGR2GRAY); namedWindow("Original" ); namedWindow("Gray" ); imshow("Original" , image); imshow("Gray" , grayImg); waitKey(0 ); return 0 ; }
绘制基本形状 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #include <opencv2/core.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/highgui.hpp> #include <iostream> using namespace cv;using namespace std ;int main () { string path = "D:/Summer Holiday Practice/opencv learning/Resources/OpenCVBook/puppy.bmp" ; Mat image = imread(path); if (image.empty()) { cout << "Read Fail ! ! !" << endl ; return -1 ; } circle(image, Point(155 , 110 ), 65 , 0 , 3 ); putText(image, "This is a dog." , Point(40 , 200 ), FONT_HERSHEY_PLAIN, 2.0 , 255 , 2 ); namedWindow("Original" ); imshow("Original" , image); waitKey(0 ); return 0 ; }
复制 浅复制和计数引用:图像赋值,图像指向同一个内存块。
深复制:copyTo
/ convertTo
定义感兴趣区域 (给图片加一个logo吧!)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #include <opencv2/core.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/highgui.hpp> #include <iostream> using namespace cv;using namespace std ;int main () { string path = "D:/Summer Holiday Practice/opencv learning/Resources/OpenCVBook/puppy.bmp" ; Mat image = imread(path); if (image.empty()) { cout << "Read Fail ! ! !" << endl ; return -1 ; } Mat logo = cv::imread("D:/Summer Holiday Practice/opencv learning/Resources/OpenCVBook/smalllogo.png" ); Mat imageROI (image, Rect(image.cols - logo.cols, image.rows - logo.rows, logo.cols, logo.rows)) ; logo.copyTo(imageROI); namedWindow("Original" ); imshow("Original" , image); waitKey(0 ); return 0 ; }
使用图像掩码 (刚刚的logo黑一片,丑丑)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include <opencv2/core.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/highgui.hpp> #include <iostream> using namespace cv;using namespace std ;int main () { string path = "D:/Summer Holiday Practice/opencv learning/Resources/OpenCVBook/puppy.bmp" ; Mat image = imread(path); if (image.empty()) { cout << "Read Fail ! ! !" << endl ; return -1 ; } Mat logo = cv::imread("D:/Summer Holiday Practice/opencv learning/Resources/OpenCVBook/smalllogo.png" ); Mat imageROI = image(cv::Rect(image.cols - logo.cols, image.rows - logo.rows,logo.cols, logo.rows)); Mat mask (logo) ; logo.copyTo(imageROI, mask); namedWindow("Original" ); imshow("Original" , image); waitKey(0 ); return 0 ; }
原理 掩码试一个8位图像,如果掩码中某个位置的值不为0,在这个位置上的操作就会起作用,如果掩码中某些像素的值为0,那么对图像中相应位置的操作将不起作用。
在上面的代码中,在调用 copyTo
时,使用掩码,可以只复制标志中白色的部分。