生活随笔
收集整理的這篇文章主要介紹了
opencv图片处理和摄像头边缘检测
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
腐蝕
結果 代碼 erode函數
#include "pch.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv
; int main ( ) { Mat srcImage
= imread ( "cat15.jpg" , 2 | 4 ) ; imshow ( "原始圖" , srcImage
) ; waitKey ( 0 ) ; Mat element
= getStructuringElement ( MORPH_RECT
, Size ( 15 , 15 ) ) ; Mat dstImage
; erode ( srcImage
, dstImage
, element
) ; imshow ( "效果圖" , dstImage
) ; waitKey ( 0 ) ; return 0 ;
}
圖像模糊
結果 代碼 均值濾波blur()函數的使用
#include "pch.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv
; int main ( ) { Mat srcImage
= imread ( "catandmao.jpg" ) ; imshow ( "均值濾波原始圖" , srcImage
) ; Mat dstImage
; blur ( srcImage
, dstImage
, Size ( 7 , 7 ) ) ; imshow ( "均值濾波效果圖" , dstImage
) ; waitKey ( 0 ) ;
}
邊緣檢測
結果 代碼 canny算子
#include "pch.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv
; int main ( ) { Mat srcImage
= imread ( "catandmao.jpg" ) ; imshow ( "原始圖" , srcImage
) ; Mat edge
, greyImage
; cvtColor ( srcImage
, greyImage
, CV_BGR2GRAY
) ; blur ( greyImage
, edge
, Size ( 3 , 3 ) ) ; Canny ( edge
, edge
, 3 , 9 , 3 ) ; imshow ( "邊緣檢測,效果圖" , edge
) ; waitKey ( 0 ) ; }
視頻顯示
讀取并播放視頻 代碼
#include "pch.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv
; int main ( ) { VideoCapture
capture ( "H:\\how.mp4" ) ; while ( 1 ) { Mat frame
; capture
>> frame
; imshow ( "讀取視頻" , frame
) ; waitKey ( 30 ) ; } }
打開攝像頭
代碼
#include "pch.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv
;
int main ( ) { VideoCapture
capture ( 0 ) ; while ( 1 ) { Mat frame
; capture
>> frame
; imshow ( "讀取視頻" , frame
) ; waitKey ( 30 ) ; }
}
攝像頭配合邊緣檢測
結果 代碼
#include "pch.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv
;
int main ( ) { VideoCapture
capture ( 0 ) ; Mat edges
; while ( true ) { Mat frame
; capture
>> frame
; cvtColor ( frame
, edges
, CV_BGR2GRAY
) ; blur ( edges
, edges
, Size ( 7 , 7 ) ) ; Canny ( edges
, edges
, 0 , 30 , 3 ) ; imshow ( "canny邊緣化之后的視頻" , edges
) ; if ( waitKey ( 30 ) >= 0 ) break ; } return 0 ;
}
注:以上基于x64,debug模式
總結
以上是生活随笔 為你收集整理的opencv图片处理和摄像头边缘检测 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。