本文內容參考《OpenCV3編程入門》,目的是對OpenCV圖像處理有一個初步的認識,瞭解OpenCV圖像處理的基本操作。
1. 圖像顯示
圖像顯示非常簡單,不廢話,直接上代碼。
//
// main.cpp
// cvpro
//
// Created by apple on 2020/2/29.
// Copyright © 2020年 apple. All rights reserved.
//
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, const char * argv[]) {
// insert code here...
std::string path = "/Volumes/本地磁盤/Picture/頭條號/影視/想見你/XJN-200206-20.png";
Mat image = imread(path);
imshow("origin", image);
waitKey(0);
system("pause");
return 0;
}
其中,imread
函數載入圖像並將圖像數據存儲在數據結構Mat類中;imshow
函數顯示圖像。#include <opencv2/opencv.hpp>
爲OpenCV頭文件的包含;using namespace cv;
爲命名空間的包含;waitKey
函數等待按鍵按下,關閉圖像窗口。
這個測試用例相對比較簡單,這裏不再展示測試結果。
2. 圖像腐蝕
圖像腐蝕,即用圖像中的暗色部分“腐蝕”掉圖像中的高亮部分,是最基本的形態學運算之一。
//
// main.cpp
// cvpro
//
// Created by apple on 2020/2/29.
// Copyright © 2020年 apple. All rights reserved.
//
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
using namespace cv;
int main(int argc, const char * argv[]) {
// insert code here...
std::string path = "/Volumes/本地磁盤/Picture/頭條號/影視/想見你/XJN-200206-20.png";
Mat image = imread(path);
imshow("origin", image);
Mat element=getStructuringElement(MORPH_RECT, Size(15,15));
Mat dstimage;
erode(image, dstimage, element);
imshow("erode", dstimage);
waitKey(0);
system("pause");
return 0;
}
其中,前幾行表示載入並顯示圖像,getStructuringElement
函數的返回值爲Mat
類型的結構元素,是一個指定形狀和尺寸的內核矩陣;erode
函數進行圖像腐蝕操作。原始圖像和腐蝕圖像如下所示:
3. 圖像模糊
OpenCV對圖像進行均值濾波操作,可以模糊一幅圖像。
//
// main.cpp
// cvpro
//
// Created by apple on 2020/2/29.
// Copyright © 2020年 apple. All rights reserved.
//
#include <iostream>
//#include <opencv2/opencv.hpp>
//#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
//#include <opencv2/videoio.hpp>
using namespace cv;
int main(int argc, const char * argv[]) {
// insert code here...
std::string path = "/Volumes/本地磁盤/Picture/頭條號/影視/想見你/XJN-200206-20.png";
Mat image = imread(path);
imshow("origin", image);
Mat dstimage;
blur(image, dstimage, Size(17,17));
imshow("erode", dstimage);
waitKey(0);
system("pause");
return 0;
}
其中,blur
函數的功能是進行均值濾波操作,比較簡單。不再展示原圖,這裏只給出模糊處理後的圖像,如下所示:
4. canny邊緣檢測
OpenCV進行canny邊緣檢測,首先載入原圖,將其轉換爲灰度圖像,再用blur
函數進行圖像模糊降低噪聲,然後用canndy
函數進行邊緣檢測,最後顯示圖像。
//
// main.cpp
// cvpro
//
// Created by apple on 2020/2/29.
// Copyright © 2020年 apple. All rights reserved.
//
#include <iostream>
#include <opencv2/opencv.hpp>
//#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
//#include <opencv2/highgui.hpp>
//#include <opencv2/videoio.hpp>
using namespace cv;
int main(int argc, const char * argv[]) {
// insert code here...
std::string path = "/Volumes/本地磁盤/Picture/頭條號/影視/想見你/XJN-200206-20.png";
Mat image = imread(path);
imshow("origin", image);
Mat dstimage,edge,grayimage;
dstimage.create(image.size(), image.type());
cvtColor(image, grayimage, COLOR_BGR2GRAY);
blur(grayimage, edge, Size(3,3));
Canny(edge, dstimage, 3, 9.3);
imshow("erode", dstimage);
waitKey(0);
system("pause");
return 0;
}
其中,首先載入原始圖像,用 create
創建與原始圖像同類型和大小的矩陣(sdt),cvtcolor
函數將原始圖像轉換爲灰度圖像,blur
函數對灰度圖像進行模糊處理降噪,Canny
函數進行邊緣檢測,並將輸出數據存入dstimage
中。邊緣檢測的效果圖如下: