FindContours仅返回带有白色像素的黑色图像

问题描述

我刚刚开始使用Python和OpenCV。抱歉,如果这个问题很琐碎,但我已经坚持了三天。

我有一个要处理的手形图像,可以伸出腕线并在此时切割图像。最后,我只需要没有前臂的手的二进制图片。我发现有一篇论文是在python中进行的。

我目前正在尝试这样做。一切正常,直到我得到距离变换的画面。由于我现在需要手的中心(最大的斑点),因此本文建议“查找轮廓”并创建手掌的蒙版。当我尝试这样做时,“绘制轮廓”会返回带有单个白色像素的黑色图像。

我返回了元组的长度,以查看问题所在。但是我仍然没有头绪。将“查找轮廓”分配给一个输出时,该元组为两个;如果将层次结构添加输出变量,则该元组一个

预先感谢您的帮助。 Input picture Output

为什么不返回轮廓?这是我的代码

    import cv2 
    import numpy as np 
    from PIL import Image 
    from matplotlib import pyplot as plt 
    import argparse
    import glob
    import os
    from scipy import ndimage
    import cvlib as cv
    from cvlib.object_detection import draw_bBox
    from PIL import Image 
    from shutil import copyfile 
    import imutils as im
    import functools 
        
    # Import of image
    if os.path.exists('/home/jovyan/my_hand.jpg'):
        handpic = cv2.imread('/home/jovyan/my_hand.jpg')
    else:
        print('fail')
            
    rows,columns,numberOfColourChannels = handpic.shape
    #convert to greyscale
    if numberOfColourChannels is 3:
        handpic_gray = cv2.cvtColor(handpic,cv2.COLOR_BGR2GRAY)
            
    #Blurring and binary thresholding 
    blur = cv2.GaussianBlur(handpic_gray,(29,29),0)
    ret3,handpic_gray_gauss = cv2.threshold(blur,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
        
    # Mask used to flood filling.
    im_floodfill = handpic_gray_gauss
    im_floodfill = cv2.bitwise_not(im_floodfill)
    mg_fill_holes = ndimage.binary_fill_holes(im_floodfill).astype(int)
        
    #filling holes
    h,w = mg_fill_holes.shape[:2]
    crop_img = mg_fill_holes[0:h,0:3500]
        
    # Perform the distance transform algorithm
    u8 = crop_img.astype(np.uint8)
    dist = cv2.distanceTransform(u8,cv2.disT_L2,3)
    # normalize the distance image for range = {0.0,1.0}
    cv2.normalize(dist,dist,1.0,cv2.norM_MINMAX)
        
    # Detect and draw contours
    # Necessary for detecting palm center
    palmMask = dist > 130
    distneu = dist.astype(np.uint8)
    cnts,hierarchy = cv2.findContours(distneu.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    # draw Contour[enter image description here][1]
    cv2.drawContours(distneu,cnts,-1,(255,255),3,cv2.LINE_AA,hierarchy)
    plt.imshow(distneu,cmap="gray")

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)