我有一个图像,在整个图像中具有不同的亮度。我想预测图像中存在的所有点。将图像转换为灰度,并应用THRESH_BINARY_INV
和THRESH_OTSU
来获得二进制图像。然后执行连接组件分析,并创建一个掩码来过滤掉小组件。由于亮度变化,cv2.simpleBlobDetector()
无法找到图像中的所有点。即使在调整其参数后也是如此。
另一种方法是将图像变亮并转换为灰度,然后微调阈值以获得二值图像。然后将其传递给LoG,DoG和DoH检测器,以找到图像中的所有点。找到Hessian行列式(DoH)似乎表现得很好,所以微调了它的参数,得到了比cv2.simpleBlobDetector()
更好的结果。然而,它无法找到所有的点,图像。如果有人有一些有用的见解可以分享,那将是非常有用的。请在下面找到要处理的图片样本。
不同类型探测器的代码:
# Blob detection algorithms
blobs_log = blob_log(binary_image, max_sigma=20, num_sigma=10, threshold=.05)
blobs_log[:, 2] = skimage.feature.blobs_log[:, 2] * np.sqrt(2)
blobs_dog = blob_dog(binary_image, max_sigma=20, threshold=.1)
blobs_dog[:, 2] = skimage.feature.blobs_dog[:, 2] * np.sqrt(2)
blobs_doh = skimage.feature.blob_doh(binary_image, max_sigma=20, threshold=.03)
要处理的图像:
结果:蓝色圆点代表待检测目标,圆点周围的红色圆圈代表已被检测器检测到:
通过将sigma值调整为0.65生成的图像。
blob_doh(binary_image,min_sigma=0.65,threshold=.0325)
转换为B通道的LAB和阈值。
# Convert the image to LAB color space
lab_image = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
# Extract the B channel (representing the blue-yellow axis)
b_channel = lab_image[:, :, 2]
# Define a threshold value (adjust as needed)
threshold_value = 75 # You can adjust this threshold value
# Apply thresholding on the B channel to make blobs darker than the background
binary_image = cv2.threshold(b_channel, threshold_value, 255, cv2.THRESH_BINARY_INV)[1]
cv2_imshow(binary_image)
#Find contours in the binary image
contours, _ = cv2.findContours(binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Create a copy of the original image to draw circles
result_image = img.copy()
# Draw red circles around the detected dots on the result image
for contour in contours:
# Calculate the center and radius of the minimum enclosing circle
(x, y), radius = cv2.minEnclosingCircle(contour)
center = (int(x), int(y))
radius = int(radius)
# Draw a red circle with a thickness of 2
cv2.circle(result_image, center, radius, (0, 0, 255), 1)
# Display the result
cv2_imshow(result_image)
对同一LAB转换图像使用doh检测器。
from skimage.feature import blob_doh
blobs = blob_doh(binary_image,min_sigma=0.65,threshold=.0325)
# Scale the blob coordinates to match the original image size
blobs[:, 0] *= img.shape[1] / binary_image.shape[1]
blobs[:, 1] *= img.shape[0] / binary_image.shape[0]
# Draw circles around the detected blobs on the result image
for blob in blobs:
y, x, r = blob
cv2.circle(img, (int(x), int(y)), int(r), (0, 0, 255), 1) # Draw a red circle with a thickness of 2
# Display the result image with detected blobs
cv2_imshow(img)
1条答案
按热度按时间zlwx9yxi1#
不同的亮度会使阈值处理变得困难。您可以尝试使用自适应阈值处理逐段(例如40x40像素)对图像进行阈值处理。这将保留亮区和暗区中的点。
你也可以尝试用高斯模糊和大的核函数来模糊图像。你可以用这个结果来补偿原始图像中的亮度锥。