Răsfoiți Sursa

OpenCV图像处理 学习代码

蘭雅sRGB 1 lună în urmă
părinte
comite
6a46e2be06
8 a modificat fișierele cu 160 adăugiri și 1 ștergeri
  1. 16 0
      .clang-format
  2. 14 1
      README.md
  3. 10 0
      opencv/cv01_img.cpp
  4. 26 0
      opencv/cv02_point.cpp
  5. 37 0
      opencv/cv03_rect.cpp
  6. 48 0
      opencv/cv04_removeShadow.cpp
  7. BIN
      opencv/img/lena.webp
  8. 9 0
      opencv/version.cpp

+ 16 - 0
.clang-format

@@ -0,0 +1,16 @@
+BasedOnStyle: LLVM
+IndentWidth: 4
+UseTab: Never
+AlignConsecutiveDeclarations: true
+AlignConsecutiveAssignments: true
+BreakBeforeBraces: Stroustrup
+ColumnLimit: 120
+AllowShortBlocksOnASingleLine: true
+AllowShortCaseLabelsOnASingleLine: true
+AllowShortFunctionsOnASingleLine: Inline
+AllowShortIfStatementsOnASingleLine: true
+AllowShortLoopsOnASingleLine: true
+SpaceBeforeParens: ControlStatements
+SpacesInParentheses: false
+SpacesInAngles: false
+SpacesBeforeTrailingComments: 1

+ 14 - 1
README.md

@@ -49,4 +49,17 @@ Path=C:\MSVC2022\bin;%PATH%
 
 ## [使用GPT 3.5 学习C语言: 25个简单代码让你了解 C 语言基本语法](./base/)
 
-## [C/C++零基础入门 学习收集的示例代码](./examples/)
+## [C/C++零基础入门 学习收集的示例代码](./examples/)
+
+## [OpenCV图像处理 学习代码](./opencv/)
+```
+# OpenCV 官方下载地址:
+https://opencv.org/releases/
+
+# 原始文件:
+https://github.com/opencv/opencv/releases/download/4.10.0/opencv-4.10.0-windows.exe
+
+
+# 安装位置: 解压到 MSVC2022 编译器目录
+C:\MSVC2022
+```

+ 10 - 0
opencv/cv01_img.cpp

@@ -0,0 +1,10 @@
+#include <opencv2/opencv.hpp> //头文件
+using namespace cv;           // 包含cv命名空间
+
+#pragma comment(lib, "opencv_world4100.lib")
+int main()
+{
+    Mat img = imread("./img/lena.webp"); // 【1】读入一张图片
+    imshow("lena.webp", img);            // 【2】在窗口中显示载入的图片
+    waitKey(6000);                       // 【3】等待6000ms后窗口自动关闭
+}

+ 26 - 0
opencv/cv02_point.cpp

@@ -0,0 +1,26 @@
+#include <iostream>
+#include <opencv2/opencv.hpp>
+#include <vector>
+
+#pragma comment(lib, "opencv_world4100.lib")
+using namespace std;
+using namespace cv;
+
+int main()
+{
+    Point2f p(6, 2);
+    cout << "【2维点】p = " << p << ";\n" << endl;
+
+    Point3f p3f(8, 2, 0);
+    cout << "【3维点】p3f = " << p3f << ";\n" << endl;
+
+    vector<float> v;
+    v.push_back(3);
+    v.push_back(5);
+    v.push_back(7);
+
+    vector<Point2f> points(20);
+    for (size_t i = 0; i < points.size(); ++i) points[i] = Point2f((float)(i * 5), (float)(i % 7));
+
+    cout << "【二维点向量】points = " << points << ";";
+}

+ 37 - 0
opencv/cv03_rect.cpp

@@ -0,0 +1,37 @@
+#include <opencv2/opencv.hpp>
+#pragma comment(lib, "opencv_world4100.lib")
+using namespace cv;
+
+int main()
+{
+    Mat  image = imread("./img/lena.webp");
+    Rect rect1(256, 256, 128, 128);
+    Rect rect2(224, 224, 128, 128);
+
+    Mat roi1;
+    image(rect1).copyTo(roi1); // 将image中rect1的部分复制到roi1中
+    imshow("1", roi1);
+    waitKey(0);
+
+    Mat roi2;
+    image(rect2).copyTo(roi2); // 将image中rect2复制到roi2中
+    imshow("2", roi2);
+    waitKey(0);
+
+    cv::Rect rect3 = rect1 & rect2; // rect1和rect2的交集
+    Mat      roi3;
+    image(rect3).copyTo(roi3);
+    imshow("3", roi3);
+    waitKey(0);
+
+    Rect rect4 = rect1 | rect2; // rect1和rect2的并集(两幅图的最小外接矩形)
+    Mat  roi4;
+    image(rect4).copyTo(roi4);
+    imshow("4", roi4);
+    waitKey(0);
+
+    Rect rect5(10, 10, 128, 128);
+    roi1.copyTo(image(rect5)); // 将rect1复制到image的指定区域rect5中
+    imshow("5", image);
+    waitKey(0);
+}

+ 48 - 0
opencv/cv04_removeShadow.cpp

@@ -0,0 +1,48 @@
+#include <opencv2/opencv.hpp>
+
+#pragma comment(lib, "opencv_world4100.lib")
+using namespace cv;
+using namespace std;
+
+int main(int argc, char *argv[])
+{
+    if (argc != 2) {
+        puts("Usage: cv04_removeShadow image\n");
+        return 8;
+    }
+
+    Mat src = imread(argv[1]);
+    // 1.将图像转为灰度图
+    Mat gray;
+    cvtColor(src, gray, COLOR_BGR2GRAY);
+    // imshow("gray", gray);
+
+    // 定义腐蚀和膨胀的结构化元素和迭代次数
+    Mat element   = getStructuringElement(MORPH_RECT, Size(3, 3));
+    int iteration = 9;
+
+    // 2.将灰度图进行膨胀操作
+    Mat dilateMat;
+    morphologyEx(gray, dilateMat, MORPH_DILATE, element, Point(-1, -1), iteration);
+    // imshow("dilate", dilateMat);
+
+    // 3.将膨胀后的图再进行腐蚀
+    Mat erodeMat;
+    morphologyEx(dilateMat, erodeMat, MORPH_ERODE, element, Point(-1, -1), iteration);
+    // imshow("erode", erodeMat);
+
+    // 4.膨胀再腐蚀后的图减去原灰度图再进行取反操作
+    Mat calcMat = ~(erodeMat - gray);
+    // imshow("calc", calcMat);
+
+    // 5.使用规一化将原来背景白色的改了和原来灰度图差不多的灰色
+    Mat removeShadowMat;
+    normalize(calcMat, removeShadowMat, 0, 200, NORM_MINMAX);
+    // imshow("dst", removeShadowMat);
+
+    // waitKey(0);
+
+    imwrite("removeShadow.png", removeShadowMat);
+
+    return 0;
+}

BIN
opencv/img/lena.webp


+ 9 - 0
opencv/version.cpp

@@ -0,0 +1,9 @@
+#include <iostream>
+#include <opencv2/opencv.hpp> //头文件
+using namespace cv;           // 包含cv命名空间
+#pragma comment(lib, "opencv_world4100.lib")
+int main()
+{
+    std::cout << getVersionString() << std::endl;
+    return 0;
+}