Python to remove the shadow part

1. Introduce the library.
cv2 finds Opencv-python in the library
For those who cannot add the library, please refer to:
https://blog.csdn.net/ttumetai/article/details/114981381
insert image description here
original image

import cv2
import numpy as np

kernel = np.ones((3, 3), np.uint8)
original_img = cv2.imread(r'D:\Users\sk\Desktop/Test.jpg')
# 计算灰白色部分像素的均值-----------------------------去阴影操作 后续可调节参数
pixel = int(np.mean(original_img[original_img > 140]))
# 把灰白色部分修改为与背景接近的颜色   
original_img[original_img > 25] = pixel
#----------------------------------------------------------------------------
gray_img = cv2.cvtColor(original_img, cv2.COLOR_BGR2GRAY)  # 灰度处理

cv2.imshow('Gray_img', gray_img)
# 二值化阈值处理(CV2.THRESH_BINARY)
# 反二值化阈值处理(CV2.THRESH_BINARY_INV)
# 截断阈值化处理(CV2.THRESH_TRUNC)
# 超阈值零处理(CV2.THRESH_TOZERO_INV)
# 低阈值零处理(CV2.THRESH_TOZERO)
ret, th1 = cv2.threshold(gray_img, 45, 255, cv2.THRESH_BINARY_INV)
cv2.imshow('th1', th1)

# 开运算
# erosion = cv2.erode(th1, kernel, iterations=1)  # 腐蚀
# dilation = cv2.dilate(erosion, kernel, iterations=1)  #膨胀

# 闭运算
dilation = cv2.dilate(th1, kernel, iterations=3)  #膨胀
erosion = cv2.erode(dilation, kernel, iterations=1)  # 腐蚀

cv2.imshow('erosion', th1)
cv2.imshow('dilation', dilation)
cv2.imshow('image', pixel)
cv2.waitKey(0)

Reference:
https://www.jb51.net/article/204840.htm

Guess you like

Origin blog.csdn.net/quailchivalrous/article/details/122321412