(_cai_) opencv学习笔记(1):图像形态学计算的方式 morphology函数的应用
1.腐蝕
概述:腐蝕掉圖像的細節。若圖像有很多毛刺,通過腐蝕操作,可以將毛刺消除。
????????下面所說的“值”可以簡單理解為RGB三個通道[0,255],值越小,亮度越低,值越大,亮度越高。
????????我們首先定義了一個3*3的矩陣作為核,我們不關心矩陣的值,只關心他的大小。在圖像中用核選中3*3的像素塊,若像素塊中的值相差很大(我們可以將其想象成邊界),值大的像素點會被值小的像素點給取代。這種計算操作對于圖像的邊緣是極為敏感的。
函數:erode(cv::InputArray src, cv::OutputArray dst, cv::InputArray kernel)
參數:在腐蝕操作中,我們主要關注以下三個參數。另一些關于邊界填充的參數這里不做介紹。
| 1.cv::InputArray src | 輸入圖像 |
| 2.OutputArray dst | 輸出圖像 |
| 3.InputArray kernel | 核:n*n的矩陣。n越大,腐蝕的越厲害 |
代碼:?
#include <iostream> #include <opencv.hpp> #include <core/core.hpp> #include <highgui/highgui.hpp>using namespace cv; using namespace std;int main() {Mat image_1 = imread("lena.jpg");Mat res;//定義一個3*3大小的方形核Mat element = getStructuringElement(MORPH_RECT, Size(3, 3));erode(image_1,res,element);imshow("lena", image_1);imshow("res",res);waitKey(0);return 0; }輸出結果:可以注意到很多毛刺都變得更加細,或者被消除。
2.膨脹
概述:與腐蝕相反,膨脹后的圖片會將放大圖像的細節。
????????其實原理和腐蝕原理一樣。所說的“值”可以簡單理解為RGB三個通道[0,255],值越小,亮度越低,值越大,亮度越高。
????????我們首先定義了一個3*3的矩陣作為核,我們不關心矩陣的值,只關心他的大小。在圖像中用核選中3*3的像素塊,若像素塊中的值相差很大(我們可以將其想象成邊界),值小的像素點會被值大的像素點給取代。也就是亮度高的像素點會被放大取代亮度低的背景像素點。
函數:dilate(cv::InputArray src, cv::OutputArray dst, cv::InputArray kernel)
參數:在膨脹操作中,我們也只關注以下三個參數。
| 1.cv::InputArray src | 輸入圖像 |
| 2.OutputArray dst | 輸出圖像 |
| 3.InputArray kernel | 核:n*n的矩陣。n越大,腐蝕的越厲害 |
代碼:?
#include <iostream> #include <opencv.hpp> #include <core/core.hpp> #include <highgui/highgui.hpp>using namespace cv; using namespace std;int main() {Mat image_1 = imread("lena.jpg");Mat res;//定義一個3*3大小的方形核Mat element = getStructuringElement(MORPH_RECT, Size(3, 3));dilate(image_1,res,element);imshow("lena", image_1);imshow("res",res);waitKey(0);return 0; }輸出結果:第一眼看,圖像變得更加模糊。但是仔細觀察會發現,一些噪聲點(白點),發絲等細節被放大。
3.開運算
?概述:先對圖像進行腐蝕,再對圖像進行膨脹。
函數:形態學計算(開運算、閉運算、梯度運算、禮帽、黑帽)等操作均使用該函數。
cv::morphologyEx(cv::InputArray src, cv::OutputArray dst, int op, cv::InputArray kernel,)
參數:我們關注以下四個參數。
| 1.cv::InputArray src | 輸入圖像 |
| 2.OutputArray dst | 輸出圖像 |
| 3.int op | 形態操作類型(MORPH_OPEN ) |
| 4.InputArray kernel | 核:n*n的矩陣。n越大,腐蝕的越厲害 |
代碼:
#include <iostream> #include <opencv.hpp> #include <core/core.hpp> #include <highgui/highgui.hpp>using namespace cv; using namespace std;int main() {Mat image_1 = imread("lena.jpg");Mat res;//定義一個3*3大小的方形核Mat element = getStructuringElement(MORPH_RECT, Size(3, 3));morphologyEx(image_1,res,MORPH_OPEN,element);imshow("lena", image_1);imshow("res",res);waitKey(0);return 0; }輸出結果:
4.閉運算
概述:先對圖像進行膨脹,再對圖像進行腐蝕。
函數:形態學計算(開運算、閉運算、梯度運算、禮帽、黑帽)等操作均使用該函數。
cv::morphologyEx(cv::InputArray src, cv::OutputArray dst, int op, cv::InputArray kernel,)
參數:我們關注以下四個參數。
| 1.cv::InputArray src | 輸入圖像 |
| 2.OutputArray dst | 輸出圖像 |
| 3.int op | 形態操作類型(MORPH_CLOSE) |
| 4.InputArray kernel | 核:n*n的矩陣。n越大,腐蝕的越厲害 |
代碼:
#include <iostream> #include <opencv.hpp> #include <core/core.hpp> #include <highgui/highgui.hpp>using namespace cv; using namespace std;int main() {Mat image_1 = imread("lena.jpg");Mat res;//定義一個3*3大小的方形核Mat element = getStructuringElement(MORPH_RECT, Size(3, 3));morphologyEx(image_1,res,MORPH_CLOSE,element);imshow("lena", image_1);imshow("res",res);waitKey(0);return 0; }輸出結果:
5.梯度運算
概述:通過對腐蝕和膨脹的理解,我們可以認識到由于邊界上色彩的差異很大,所以導致腐蝕和膨脹在邊界區域的影響很大。簡單化理解就是腐蝕是腐蝕邊界上的細節,膨脹也是放大邊界的細節,兩者相減,就可以算出圖像的邊界信息。
代碼:
#include <iostream> #include <opencv.hpp> #include <core/core.hpp> #include <highgui/highgui.hpp>using namespace cv; using namespace std;int main() {Mat image_1 = imread("lena.jpg");Mat res;//定義一個3*3大小的方形核Mat element = getStructuringElement(MORPH_RECT, Size(3, 3));morphologyEx(image_1,res,MORPH_GRADIENT,element);imshow("lena", image_1);imshow("res",res);waitKey(0);return 0; }輸出結果:
6.禮帽與黑帽
概述:禮帽運算=原始圖像-開運算生成的圖像
?代碼:
#include <iostream> #include <opencv.hpp> #include <core/core.hpp> #include <highgui/highgui.hpp>using namespace cv; using namespace std;int main() {Mat image_1 = imread("lena.jpg");Mat res;//定義一個3*3大小的方形核Mat element = getStructuringElement(MORPH_RECT, Size(3, 3));morphologyEx(image_1,res,MORPH_TOPHAT,element);imshow("lena", image_1);imshow("res",res);waitKey(0);return 0; }輸出結果:
概述:禮帽運算=閉運算生成的圖像-原始圖像
?代碼:
#include <iostream> #include <opencv.hpp> #include <core/core.hpp> #include <highgui/highgui.hpp>using namespace cv; using namespace std;int main() {Mat image_1 = imread("lena.jpg");Mat res;//定義一個3*3大小的方形核Mat element = getStructuringElement(MORPH_RECT, Size(3, 3));morphologyEx(image_1,res,MORPH_BLACKHAT,element);imshow("lena", image_1);imshow("res",res);waitKey(0);return 0; }輸出結果:
總結
以上是生活随笔為你收集整理的(_cai_) opencv学习笔记(1):图像形态学计算的方式 morphology函数的应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 魔方APP项目-01-移动端开发相关概念
- 下一篇: Android 仿直播特效点赞飘爱心