Python 3.x 我用opencv python检测到了照片中的眩光点。我怎样才能移除它们?

Python 3.x 我用opencv python检测到了照片中的眩光点。我怎样才能移除它们?,python-3.x,image-processing,opencv-python,Python 3.x,Image Processing,Opencv Python,在代码下面,我用来检测图像中的眩光点。我执行了一系列的腐蚀和膨胀来移除并对阈值进行连接成分分析。如何使用openc python删除它们 path = "desire image path" image = cv2.imread(path) gray = cv2.cvtColor( image, cv2.COLOR_BGR2GRAY ) blurred = cv2.GaussianBlur( gray, (11, 11), 0 ) # threshold the image to reveal

在代码下面,我用来检测图像中的眩光点。我执行了一系列的腐蚀和膨胀来移除并对阈值进行连接成分分析。如何使用openc python删除它们

path = "desire image path"
image = cv2.imread(path)
gray = cv2.cvtColor( image, cv2.COLOR_BGR2GRAY )
blurred = cv2.GaussianBlur( gray, (11, 11), 0 )

# threshold the image to reveal light regions in the
# blurred image
thresh = cv2.threshold( blurred, 200, 255, cv2.THRESH_BINARY )[1]

# perform a series of erosions and dilations to remove
# any small blobs of noise from the thresholded image
thresh = cv2.erode( thresh, None, iterations=2 )
thresh = cv2.dilate( thresh, None, iterations=4 )

# perform a connected component analysis on the thresholded
# image, then initialize a mask to store only the "large"
# components
labels = measure.label( thresh, neighbors=8, background=0 )
mask = np.zeros( thresh.shape, dtype="uint8" )
# loop over the unique components
for label in np.unique( labels ):
    # if this is the background label, ignore it
    if label == 0:
        continue
    # otherwise, construct the label mask and count the
    # number of pixels
    labelMask = np.zeros( thresh.shape, dtype="uint8" )
    labelMask[labels == label] = 255
    numPixels = cv2.countNonZero( labelMask )
    # if the number of pixels in the component is sufficiently
    # large, then add it to our mask of "large blobs"
    if numPixels > 300:
        mask = cv2.add( mask, labelMask )

# find the contours in the mask, then sort them from left to
# right
cnts = cv2.findContours( mask.copy(), cv2.RETR_EXTERNAL,
                         cv2.CHAIN_APPROX_SIMPLE )
cnts = imutils.grab_contours( cnts )
cnts = contours.sort_contours( cnts )[0]
# loop over the contours
for (i, c) in enumerate( cnts ):
    # draw the bright spot on the image
    (x, y, w, h) = cv2.boundingRect( c )
    ((cX, cY), radius) = cv2.minEnclosingCircle( c )
    cv2.circle( image, (int( cX ), int( cY )), int( radius ),
                (0, 0, 255), 3 )
    cv2.putText( image, "#{}".format( i + 1 ), (x, y - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2 )
    # image = remove_glare( image, x, y )
# show the output image
cv2.imshow( "Image", image )
cv2.imwrite("spots_detected.jpg",image)
cv2.waitKey( 0 )


OpenCV库附带两种修复算法:

  • cv2.INPAINT_TELEA
    一种基于快速行进法的图像修复技术(TELEA,2004)

  • cv2.修复
    Navier-stokes,流体动力学,图像和视频修复(Bertalmío等人,2001年)


使用斑点作为遮罩创建具有相同大小的二值图像。然后运行
cv2。修复(图像、遮罩、半径、方法)
并查看结果。

OpenCV库附带两种修复算法:

  • cv2.INPAINT_TELEA
    一种基于快速行进法的图像修复技术(TELEA,2004)

  • cv2.修复
    Navier-stokes,流体动力学,图像和视频修复(Bertalmío等人,2001年)


使用斑点作为遮罩创建具有相同大小的二值图像。然后运行
cv2.inpaint(图像、遮罩、半径、方法)
并查看结果。

您不能“删除”它们,因为您将用什么替换它们?2D光栅图像不包含光场信息——你最好尝试构建Photoshop的内容感知填充或修复工具,但Adobe花了几年时间和数百万美元开发(结果往往是错误的)-这类图像处理工作并非琐碎。
再次拍照
。严重眩光无法消除。一旦颜色更改为纯白色,就无法恢复颜色。您可以尝试将眩光点转换为遮罩图像,并将其与cv2.inpaint()一起使用以恢复一些纹理。@fmw42“您可以尝试将眩光点转换为遮罩图像,并与cv2.inpaint()一起使用以恢复一些纹理”既然我是opencv的新手,你能解释清楚吗?你能提供一些代码示例来帮助我吗?你不能“删除”它们,因为你会用什么来替换它们?2D光栅图像不包含光场信息——你最好尝试构建Photoshop的内容感知填充或修复工具,但Adobe花了几年时间和数百万美元开发(结果往往是错误的)-这类图像处理工作并非琐碎。
再次拍照
。严重眩光无法消除。一旦颜色更改为纯白色,就无法恢复颜色。您可以尝试将眩光点转换为遮罩图像,并将其与cv2.inpaint()一起使用以恢复一些纹理。@fmw42“您可以尝试将眩光点转换为遮罩图像,并与cv2.inpaint()一起使用以恢复一些纹理”既然我是opencv的新手,你能解释清楚这一点吗?你能提供一些代码示例来帮助这些算法在修复位置较大时失败吗?这些算法在修复位置较大时失败