如何解决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 举报,一经查实,本站将立刻删除。