opencv learning 20: opening and closing operations and horizontal or vertical line extraction

1. Open operation

Open operation: first corrode the image, and then expand
the corroded
image. Open operation=corrosion+expansion. It is mainly used in binary images, as well as grayscale images.
Can eliminate background noise
morphologyEx
operation result = cv2.morphologyEx (source image img, cv2.MORPH_OPEN, convolution kernel k)
cv2.MORPH_OPEN: open operation

import cv2 as cv
import numpy as np


def open_demo(image):
    print(image.shape)
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
    cv.imshow("binary", binary)
    kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (15, 15))#cv.MORPH_ELLIPSE椭圆形,可以选择其他形状
    binary = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel)
    cv.imshow("open-result", binary)

src = cv.imread("C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/kaibi.png")  #读取图片位置
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
open_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()

Run screenshot:
eliminate the background noise
Insert picture description here

Two, closed operation

The image is expanded first and then corroded.
Helps close small holes or small black spots on foreground objects.
morphologyEx
operation result=cv2.morphologyEx(source image img,cv2.MORPH_CLOSE, convolution kernel k)
cv2.MORPH_CLOSE: Close operation
Choose the size of the convolution kernel reasonably, if it is too small to remove the black points of the foreground image, it is
mainly used in binary images , Grayscale images are also available.

import cv2 as cv
import numpy as np

def close_demo(image):
    print(image.shape)
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)
    cv.imshow("binary", binary)
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (15, 15))
    binary = cv.morphologyEx(binary, cv.MORPH_CLOSE, kernel)
    cv.imshow("close_demo", binary)

src = cv.imread("C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/kaibi.png")  #读取图片位置
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
close_demo(src)
cv.waitKey(0)

Run screenshot:
Eliminate small black spots in foreground
Insert picture description here

3. Extraction of horizontal or vertical lines

Extracting the horizontal and vertical lines in the image belongs to the application of basic morphological operations. The principle is to define a specific structural element according to the figure to be extracted, and then use this structural element to traverse the image and perform a series of morphological operations. Filter out other characteristic graphics to achieve the extraction effect.
Operation process:

  • Read the original image;

  • Convert to grayscale image;

  • Convert to binary image;

  • Define structural elements;

  • Open operation.

3.1 Open operation to extract horizontal line

ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)#线段去除
kernel = cv.getStructuringElement(cv.MORPH_RECT, (15, 1))#开操作提取水平线

code show as below:

import cv2 as cv
import numpy as np

def open_demo(image):
    print(image.shape)
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)#线段去除
    cv.imshow("binary", binary)
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (15, 1))#开操作提取水平线
    binary = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel)
    cv.imshow("open_demo", binary)



src = cv.imread("C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/kaibi1.png")  #读取图片位置
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
open_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()

Run screenshot:
Insert picture description here3.2 Open operation to extract vertical line

ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)#线段去除
kernel = cv.getStructuringElement(cv.MORPH_RECT, (1, 15))#开操作提取竖直线

code show as below:

import cv2 as cv
import numpy as np

def open_demo(image):
    print(image.shape)
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)#线段去除
    cv.imshow("binary", binary)
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (1, 15))#开操作提取竖直线
    binary = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel)
    cv.imshow("open_demo", binary)



src = cv.imread("C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/kaibi1.png")  #读取图片位置
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
open_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()

Run screenshot:
Insert picture description here3.3 Open operation to extract diagonal line

ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)#线段去除
kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))  # 开操作去斜线

code show as below:

import cv2 as cv
import numpy as np

def open_demo(image):
    print(image.shape)
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)#线段去除
    cv.imshow("binary", binary)
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))  # 开操作去斜线
    binary = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel)
    cv.imshow("open_demo", binary)



src = cv.imread("C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/kaibi2.png")  #读取图片位置
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
open_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()

Run screenshot:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44145452/article/details/112798784