gpt4 book ai didi

python - 轮廓检测 : reduce glare on image opencv/python

转载 作者:行者123 更新时间:2023-12-04 17:19:33 39 4
gpt4 key购买 nike

我正在为下面的图像进行轮廓检测,但是由于闪电条件,图像显示眩光的地方检测不完整。我正在尝试删除它们以获得更好的轮廓检测。

这是原图

image with glare

这里是灰化 + 阈值图像,在其上运行 cv2.connectedComponentsWithStats 以检测对象。我已将需要减少曝光的区域框起来。 (因为我使用的是反向 THRESH_BINARY_INV 过滤器,所以这些区域显示为黑色)。

grayed + thresholded image

正如您在下文中看到的对象检测区域不完整,cv2.connectedComponentsWithStats 将不会检测对象的完整区域

Object areas

然后当然在裁剪轮廓组件上计算的轮廓本身也是错误的:

Cropped outlined

当然轮廓本身是错误的:

Wrong contour due to glare

这是我到目前为止所做的:

def getFilteredContours(image, minAreaFilter=20000) -> np.array:
ret = []
ctrs,_ = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
ctrs = sorted(ctrs, key=cv2.contourArea, reverse=True)
for i, c in enumerate(ctrs):
# Calculate the area of each contour
area = cv2.contourArea(c)
if area < minAreaFilter:
break
ret.append(c)
return ret

birdEye = cv2.imread(impath)

gray = cv2.cvtColor(birdEye, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
threshImg = cv2.threshold(gray, 180, 255, cv2.THRESH_BINARY_INV)[1]
(numLabels, labels, stats, centroids) = cv2.connectedComponentsWithStats(
threshImg, 4, cv2.CV_32S)

#then for each identified component we extract the component and get the contour

filteredIdx = getFilteredLabelIndex(stats)

for labelId in filteredLabelId:
componentMask = (labels == i).astype("uint8") * 255
ctrs, _ = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
ctrs = sorted(ctrs, key=cv2.contourArea, reverse=True)
ctr = max(ctrs, key=cv2.contourArea)
cv2.drawContours(birdEye, [cntrs], -1, (255, 0, 255), 3)

cv2.imshow("original contour", birdEye)

cv2.waitKey(0)
cv2.destroyAllWindows()

欢迎提出任何建议,

谢谢

帕特

最佳答案

您可以使用 floodFill用于先填充背景。

cv2.floodFill 应用您的样本图像可获得良好的结果。
结果很好,因为背景相对均匀。
floodFill 使用颜色信息,与仅使用亮度的其他算法相反。
背景有轻微的亮度梯度,“洪水填充”算法处理得很好。

您可以使用以下阶段:

  • 将所有(暗)值(例如低于 10)替换为 10 - 避免对象内部存在黑色像素的问题。
  • 使用 cv2.floodFill 为背景填充黑色。
    使用左上角作为“背景”种子颜色(假设像素 [10,10] 不在对象中)。
  • 转换为灰度。
  • 应用阈值 - 将所有大于零的像素转换为 255。
  • 使用开运算(形态学运算)去除小的异常值。
  • 找到轮廓。

代码示例:

import cv2
import numpy as np

birdEye = cv2.imread(r"C:\Rotem\tools.jpg")

# Replace all (dark) values below 10 with 10 - avoiding issues where there are black pixels inside an object
birdEye = np.maximum(birdEye, 10)

foreground = birdEye.copy()

seed = (10, 10) # Use the top left corner as a "background" seed color (assume pixel [10,10] is not in an object).

# Use floodFill for filling the background with black color
cv2.floodFill(foreground, None, seedPoint=seed, newVal=(0, 0, 0), loDiff=(5, 5, 5, 5), upDiff=(5, 5, 5, 5))

# Convert to Grayscale
gray = cv2.cvtColor(foreground, cv2.COLOR_BGR2GRAY)

# Apply threshold
thresh = cv2.threshold(gray, 1, 255, cv2.THRESH_BINARY)[1]

# Use opening for removing small outliers
thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)))

# Find contours
cntrs, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

# Draw contours
cv2.drawContours(birdEye, cntrs, -1, (255, 0, 255), 3)

# Show images for testing
cv2.imshow('foreground', foreground)
cv2.imshow('gray', gray)
cv2.imshow('thresh', thresh)
cv2.imshow('birdEye', birdEye)

cv2.waitKey()
cv2.destroyAllWindows()

前景:
foreground

birdEye 输出:
birdEye output

关于python - 轮廓检测 : reduce glare on image opencv/python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67099645/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com