[Reproduced] OpenCV-Python series of top hat and black hat operations (22)

In the last tutorial, we left some problems, that is, the brightness will change after the image opening or closing operation, which will cause some troubles in the later image processing, so this tutorial we introduce the top hat and the OpenCV Black hat computing to solve these problems.

Top hat

Top hat calculations are often translated as "top hat" calculations. Is the difference between the original image and the result image of the "open operation" just introduced above, the mathematical expression is as follows:Insert picture description here

Because the result of the open operation is to enlarge the cracks or the local low-brightness area, the image after the open operation is subtracted from the original image, and the resulting effect image highlights the brighter area than the area around the original image. And this operation is related to the size of the selected core.

The cap operation is often used to separate patches that are brighter than neighboring ones. When an image has a large background, and small objects are more regular, you can use the top hat calculation to extract the background.

Now let’s perform some actual operations, let’s take a look at a picture first:
Insert picture description here

This is the actual case that we will talk about later-digital recognition on the card. First of all, we need to grayscale the image and then threshold it, so that it is convenient to extract the contour later. Let's use OTSU first. The algorithm is thresholded:

	view plaincopy to clipboardprint?
import cv2  
import numpy as np  
  
  
img = cv2.imread('credit_card_.png')  
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  
threshold = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV|  
                          cv2.THRESH_OTSU)[1]  
cv2.imshow("img",img)  
cv2.imshow("thres",threshold)  
cv2.waitKey(0)  
cv2.destroyAllWindows()  

Insert picture description here

The fact is that the situation is very bad. Such thresholding will not allow us to proceed with the next step. We look at the original image and find that the brightness of the font and the background in the lower right corner are a bit high, so this situation will occur. Now I want to separate the font from the background. Come out, we can use the top hat operation to brighten the foreground:

	view plaincopy to clipboardprint?
import cv2  
import numpy as np  
  
  
img = cv2.imread('credit_card_.png')  
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(21,21))  
tophat = cv2.morphologyEx(gray,cv2.MORPH_TOPHAT,kernel)  
threshold = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV|  
                          cv2.THRESH_OTSU)[1]  
cv2.imshow("img",img)  
cv2.imshow("thres",threshold)  
cv2.waitKey(0)  
cv2.destroyAllWindows()  

Insert picture description here

Let's see if this effect is much better? The digital font in the middle is completely separated. In fact, we often use the top hat operation in many places.

Black hat

The black hat operation is the difference between the result image of the "closed operation" and the original image. The mathematical expression is:
Insert picture description here

The effect image after the black hat operation highlights the darker area than the area around the original image outline, and this operation is related to the size of the selected core. Therefore, the black hat operation is used to separate patches that are darker than neighboring points. Very perfect contour rendering, let's also look at the example code:

view plaincopy to clipboardprint?
import cv2  
import numpy as np  
  
  
img = cv2.imread('close.jpg')  
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))  
tophat = cv2.morphologyEx(gray,cv2.MORPH_BLACKHAT,kernel)  
cv2.imshow("img",img)  
cv2.imshow("thres",tophat)  
cv2.waitKey(0)  
cv2.destroyAllWindows()  

Insert picture description here

Morphological gradient

Now let's look at the last operation in OpenCV morphology processing-morphological gradient, which is essentially the edge extraction of the image, which can also be said to be the result of expansion minus the result of corrosion. Let's look at the actual effect:

view plaincopy to clipboardprint?
import cv2  
import numpy as np  
  
  
img = cv2.imread('close.jpg')  
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))  
tophat = cv2.morphologyEx(gray,cv2.MORPH_GRADIENT,kernel)  
cv2.imshow("img",img)  
cv2.imshow("thres",tophat)  
cv2.waitKey(0)  
cv2.destroyAllWindows()  

Insert picture description here

This detects the edges of the image.

The end of this tutorial also means the end of the morphological processing part of OpenCV. In fact, we found that they can all be called with a function API:

cv2. morphologyEx()

We have already introduced it in the previous section. To sum up, it can perform these operations in total:

(1) MORPH_OPEN: open operation

(2) MORPH_CLOSE: closed operation

(3) MORPH_GRADIENT: Morphological gradient

(4) MORPH_TOPHAT: top hat

(5) MORPH_BLACKHAT: black hat

(6) MORPH_ERODE: Corrosion

(7) MORPH_DILATE: expansion

The processing of image morphology is very important, and everyone must be familiar with it.

Check the article summary page https://blog.csdn.net/weixin_44237705/article/details/107864965
More openvino technical information can be exchanged in the group~
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44237705/article/details/108355974