image - OpenCV-删除图像中的独立短线

标签 image opencv image-processing line

如何删除短而断开的线,但保留所有其他连接的线,如下图所示?

There is an independent short line in top right corner image

最佳答案

如果镜像始终保持良好连接,则可以根据组件的大小选择组件。我在Python中的代码(可能是一种更简单的方法,但这就是我的方法):

#get all connected components in the image with their stats (including their size, in pixel)
nb_edges, output, stats, _ = cv2.connectedComponentsWithStats(img, connectivity=8) 
#output is an image where every component has a different value    
size=stats[1:,-1] #extracting the size from the statistics

#selecting bigger components
for e in range(0,nb_edges-1):
    #replace this line depending on your application, here I chose to keep
    #all components above the mean size of components in the image
    if size[e]>=np.mean(size):
        th_up = e + 2
        th_do = th_up

        #masking to keep only the components which meet the condition
        mask = cv2.inRange(output, th_do, th_up)
        result = cv2.bitwise_xor(original_img, mask)

关于image - OpenCV-删除图像中的独立短线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41675940/

相关文章:

c - 如何将 OpenCV cv::Mat 转换为可以输入 Vlfeat vl_dsift_process 的 float*?

matlab - 如何在 Matlab 中围绕图像中对象的垂直轴进行旋转?

algorithm - 旧彩色电影效果和海报效果

html - 选择所有PNG图像

php - 将 HTML 转换为图像

php - 如何使用连接将多个图像添加到报价表?

c++ - ndk 使用 opencv 构建

c++ - imread 找不到图像

java - 是否可以使用 Sikuli 来断言图像在无 GUI 模式下是相同的?

html - 图片旁边的文字不适用于特定图片。为什么?