Zi 字媒體
2017-07-25T20:27:27+00:00
fanfuhan OpenCV 教學066 ~ opencv-066-圖像形態學(開閉操作時候結構元素應用演示)
資料來源: https://fanfuhan.github.io/
https://fanfuhan.github.io/2019/04/20/opencv-066/
GITHUB:https://github.com/jash-git/fanfuhan_ML_OpenCV
OpenCV中圖像形態學開操作與閉操作,根據結構元素的不同可以實現不同的二值圖像處理效果,我們可以通過下面的結構元素對圖像進行開操作,提取二值圖像中水平與垂直線,這個方法比霍夫直線檢測要好用得多, 在一些應用場景中會特別有用,圖像分析、OCR佈局分析中形態學操作十分重要,我們通過兩個例子來說明開閉操作的作用。
1開操作提取水平線,實現填空題橫線位置提取
結構元素大小為20×1
步驟:
01.轉灰度
02.轉二值,可選降噪
03.形態學操作,提取(偵測/尋找/檢測)水平線
04.輪廓發現,確定位置
2閉操作實現不同層次的輪廓填充
結構元素分為三種:
矩形結構元素35×35大小
矩形結構元素30×30大小
圓形結構元素30×30大小
C++
#include
#include
using namespace std ;
using namespace cv;
void open_demo () ;
void close_demo () ;
/*
*圖像形態學(開閉操作時候結構元素應用演示)
*/
int main ()
{
//open_demo();
close_demo();
waitKey( 0 );
return 0 ;
}
void close_demo ()
{
//讀取圖像
Mat src = imread( "../images/morph3.png" );
imshow( "input" , src);
//二值圖像
Mat gray, binary;
cvtColor(src, gray, COLOR_BGR2GRAY);
threshold(gray, binary, 0 , 255 , THRESH_BINARY | THRESH_OTSU);
imshow( "binary" , binary);
//閉操作//
Mat se = getStructuringElement(MORPH_ELLIPSE, Size(30, 30), Point(-1, -1));
//Mat se = getStructuringElement(MORPH_RECT, Size(30, 30), Point(-1 , -1));
//Mat se = getStructuringElement(MORPH_RECT, Size( 35 , 35 ), Point( -1 , -1 ));
morphologyEx(binary, binary, MORPH_CLOSE, se);
imshow( "close_demo rect=35,35 " , binary);
}
void open_demo ()
{
//讀取圖像
Mat src = imread( "../images/fill.png" );
imshow( "input" , src);
//二值圖像
Mat gray, binary;
cvtColor(src, gray, COLOR_BGR2GRAY);
threshold(gray, binary, 0 , 255 , THRESH_BINARY_INV | THRESH_OTSU);
imshow( "binary" , binary);
//開操作
Mat se = getStructuringElement(MORPH_RECT, Size( 25 , 1 ), Point( -1 , -1 ));
morphologyEx(binary, binary, MORPH_OPEN, se);
imshow( "open_op" , binary);
//繪製填空位置
vector < vector > contours;
vector hierarhy;
findContours(binary, contours, hierarhy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point( -1 , -1 ));
for ( size_t t = 0 ; t < contours.size(); t++)
{
Rect roi = boundingRect(contours[t]);
roi.y = roi.y - 10 ;
roi.height = 12 ;
rectangle(src, roi, Scalar( 0 , 0 , 255 ));
}
//顯示結果
imshow( "open_demo" , src);
}
Python
import cv2 as cv
import numpy as np
def open_demo () :
src = cv.imread( "D:/images/fill.png" )
cv.namedWindow( "input" , cv.WINDOW_AUTOSIZE)
cv.imshow( "input" , src)
#圖像二值化
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0 , 255 , cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
cv.imwrite( "D:/binary1.png " , binary)
cv.imshow( "binary1" , binary)
#開操作
se1 = cv.getStructuringElement(cv.MORPH_RECT, ( 20 , 1 ), ( -1 , -1 ))
binary = cv.morphologyEx(binary, cv.MORPH_OPEN, se1)
cv.imshow( "binary" , binary )
cv.imwrite( "D:/binary2.png" , binary)
#提取輪廓
out, contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) for c in range(len(contours)): x, y, w, h = cv.boundingRect(contours[c] ) y = y - 10 h = 12 cv.rectangle(src, (x, y), (x+w, y+h), ( 0 , 0 , 255 ), 1 , 8 , 0 ) cv.imshow( " result" , src)
def close_demo () :
src = cv.imread( "D:/images/morph3.png" )
cv.namedWindow( "input" , cv.WINDOW_AUTOSIZE)
cv.imshow( "input" , src)
#圖像二值化
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0 , 255 , cv.THRESH_BINARY | cv.THRESH_OTSU)
#閉操作
se = cv.getStructuringElement(cv.MORPH_ELLIPSE, ( 15 , 15 ), ( -1 , -1 ))
binary = cv.morphologyEx(binary, cv.MORPH_CLOSE, se)
cv.imwrite( "D:/close .png" , binary)
cv.imshow( "close" , binary)
close_demo()
cv.waitKey( 0 )
cv.destroyAllWindows()
寫了
5860316篇文章,獲得
23313次喜歡