简体   繁体   English

如何将黑色背景上的白色框架中的间隙与 python opencv 连接起来?

[英]How can I connect a gap in a white frame on black background with python opencv?

I developed a frame detection algorithm in python using opencv that detects a metallic frame in a rgb image.我使用 opencv 在 python 中开发了一种框架检测算法,该算法检测 rgb 图像中的金属框架。 The output is a white frame on a black background. output 是黑底白框。 However, if the frame is partly covered, it contains gaps.但是,如果框架被部分覆盖,则它包含间隙。 How can I fill the gaps, preferably using opencv?如何填补空白,最好使用 opencv?

Example output of my algorithm:我的算法示例 output:

Frame with gap有间隙的框架

Desired output:所需的 output:

Frame without gap无间隙框架

Edit: This is how I detect the frame currently.编辑:这就是我当前检测框架的方式。

I have an rgb image of a crop canopy and a frame on top of it: Crop canopy image我有一个作物冠层的 rgb 图像和一个框架:作物冠层图像

My code detects bright parts in the image and removes noise such that only the frame should be left (which is the largest brigth object in the image).我的代码检测图像中的明亮部分并去除噪声,以便只留下帧(这是图像中最大的明亮 object)。 Below is a reproducible example when setting image path to the example image above:以下是将图像路径设置为上述示例图像时的可重现示例:

# import packages
import numpy as np
import cv2

kernel_size = 10
brightness_thres_low = 175
brightness_thres_high = 255
erode_it_1 = 6
dilate_it_1 = 25
erode_it_2 = 20


# read image
img = cv2.imread(image_path)

# find bright pixel values that indicate frame and set to bw
thres = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), brightness_thres_low, brightness_thres_high, cv2.THRESH_BINARY_INV)[1]
thres = np.invert(thres)
        
# adapt image to only contain frame
kernel = np.ones((kernel_size, kernel_size), np.uint8)
img_adapted = cv2.erode(thres,kernel, iterations = erode_it_1)
img_adapted = cv2.dilate(img_adapted,kernel, iterations = dilate_it_1)
img_adapted = cv2.erode(img_adapted, kernel, iterations = erode_it_2)
        

If the frame is fully in the image, it gets detected fine.如果框架完全在图像中,则可以很好地检测到它。 But as you can see in the example rgb image, part of the frame is covered by a plant.但正如您在示例 rgb 图像中看到的那样,部分框架被植物覆盖。 This results in an incomplete detection of the frame.这导致不完整的帧检测。 I would like to fill this gap in the frame in a generic way such that it can be applied to any gap of any processed rgb image.我想以一种通用的方式填补框架中的这个空白,以便它可以应用于任何已处理的 rgb 图像的任何空白。

Here is some code you can start with:以下是一些您可以开始的代码:

import numpy as np
import cv2

kernel_size = 10
erode = 2
thres_low = 175
thres_high = 255

img = cv2.imread("img.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, thres = cv2.threshold(gray, thres_low, thres_high, cv2.THRESH_BINARY)

kernel = np.ones((kernel_size, kernel_size), np.uint8)
thres = cv2.erode(thres, kernel, iterations=erode)   # erode fine contours

contours, _ = cv2.findContours(thres, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)  # find contours
all_contours = np.vstack(contours)   # concatenate all contours
hull = cv2.convexHull(all_contours)  # get convex hull of all contours
peri = cv2.arcLength(hull, True)     # get perimeter length of hull for the next step
approx = cv2.approxPolyDP(hull, 0.05 * peri, True)    # simplify hull
cv2.drawContours(img, [approx], 0, (0, 0, 255), 10)   # draw the contour

cv2.imwrite("out.png", img)

Output image: Output 图像:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM