opencv learning 21: top hat, black hat, morphological gradient

Insert picture description hereInsert picture description hereInsert picture description here

1. TOPHAT

Also known as the top hat, it is the difference between the original image and the image obtained after the open operation.
Top hat image=original image-open operation image to
get noisy image.
Open operation: first corrosion and then expansion.
Usage: morphologyEx
cv2.MORPH_TOPHAT
result=cv2.morphologyEx (original image, cv2.MORPH_TOPHAT, convolution kernel)
convolution kernel example: k= The code of np.ones((10,10),np.uint8)
is as follows:

import cv2 as cv
import numpy as np

def top_hat_demo(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (15, 15))
    dst = cv.morphologyEx(gray, cv.MORPH_TOPHAT, kernel)
    cv.imshow("tophat", dst)

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)
top_hat_demo(src)
#black_hat_demo(src)
#hat_binary_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()

Run screenshot:
Insert picture description here

Second, black hat BLACKHAT

Black hat image=closed operation image-original image
Obtain small holes inside the image, or small black dots in the foreground.
Closed operation: expand the image first, and then corrode. Helps close small holes or small black spots on foreground objects.
Use object: Binary image
Use method: morphologyEx
cv2.MORPH_BLACKHAT
result = cv2.morphologyEx (original image, cv2.MORPH_BLACKHAT, convolution kernel)
Convolution kernel example: k=np.ones((10,10),np.uint8 ) The
code is as follows:

import cv2 as cv
import numpy as np

def black_hat_demo(image):
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (15, 15))
    dst = cv.morphologyEx(gray, cv.MORPH_BLACKHAT, kernel)
    cimage = np.array(gray.shape, np.uint8)
    cimage = 100;
    dst = cv.add(dst, cimage)
    cv.imshow("blackhat", dst)

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)
top_hat_demo(src)
#black_hat_demo(src)
#hat_binary_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()

Run screenshot: The
code is based on grayscale or binary image.
With different sizes of convolution kernels, the noise white points obtained are different.
Insert picture description here

Three, gradient operation

The morphological gradient is the difference between expansion and erosion, and the result looks like the outline of the foreground object. There are three common calculated gradients:
Basic gradient: The
basic gradient is the difference image obtained by subtracting the corroded image from the dilated image, called the gradient image, which is also the method of calculating the morphological gradient supported in OpenCV, and this method The resulting gradient is also called the basic gradient.

Internal gradient: the
difference image is obtained by subtracting the corroded image from the original image, which is called the internal gradient of the image

External gradient:
The difference image obtained by subtracting the original image from the expanded image is called the external gradient of the image.

Extract object edge
basic gradient operation result=cv2.morphologyEx(source image img, cv2.MORPH_GRADIENT, convolution kernel k)
3.1 Basic gradient The
code is as follows:

import cv2 as cv
import numpy as np

def gradient2_demo(image):
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
    dm = cv.dilate(image, kernel)
    em = cv.erode(image, kernel)
    dst1 = cv.subtract(image, em) # internal gradient
    dst2 = cv.subtract(dm, image) # external gradient
    cv.imshow("internal", dst1)
    cv.imshow("external", dst2)



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)
gradient2_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()

Run screenshot:
Insert picture description here3.2 Internal gradient and external gradient

import cv2 as cv
import numpy as np

def gradient2_demo(image):
    kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
    dm = cv.dilate(image, kernel)
    em = cv.erode(image, kernel)
    dst1 = cv.subtract(image, em) # internal gradient
    dst2 = cv.subtract(dm, image) # external gradient
    cv.imshow("internal", dst1)
    cv.imshow("external", dst2)



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)
cv.imshow("input image", src)
gradient2_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()

Run screenshot:
Insert picture description here

Guess you like

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