opencv 如何从模糊的灰色角落中分割出较暗的斑点?

w41d8nur  于 2022-12-04  发布在  其他
关注(0)|答案(1)|浏览(153)

我试着把暗灰色的斑点从角落的模糊灰色区域中分割出来,我做了二进制阈值和形态学操作,它在中间的斑点上效果很好,但是在角落我遇到了一点麻烦。
black and white blob image

# Binary Thresholding
ret,threshImg = cv2.threshold(denoiseImg, 220, 255,cv2.THRESH_BINARY)
threshImg = cv2.bitwise_not(threshImg)

# Morphological Operation
# Initialization of kernel size
kernel2 = np.ones((2,2), np.uint8)
kernel5 = np.ones((5,5), np.uint8)

# Morphological Dilation
dilationImg = cv2.dilate(threshImg, kernel2, iterations = 1) #

# Morphological Closing
closingImg = cv2.morphologyEx(dilationImg, cv2.MORPH_CLOSE, kernel5)  # uses closing to fill gaps in the foreground
closingImg = cv2.bitwise_not(closingImg)

这就是结果。segmented blob

jq6vz3qz

jq6vz3qz1#

你可以在Python/OpenCV中做除法规范化来缓解这个问题。你基本上是模糊图像,然后用模糊的版本除以图像。
输入:

import cv2
import numpy as np

# read the image
img = cv2.imread('dark_spots.jpg')

# convert to gray
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# blur
smooth = cv2.GaussianBlur(gray, None, sigmaX=10, sigmaY=10)

# divide gray by morphology image
division = cv2.divide(gray, smooth, scale=255)

# save results
cv2.imwrite('dark_spots_division.jpg',division)

# show results
cv2.imshow('smooth', smooth)  
cv2.imshow('division', division)  
cv2.waitKey(0)
cv2.destroyAllWindows()

结果:

相关问题