我正在尝试获取距离变换图像,但是结果很差。
图像:
代码:
import cv2
import imutils
import numpy as np
photo = 'dog.jpg'
img = cv2.imread(photo)
ybprc = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
inrangeHsv = cv2.inRange(ybprc, np.array([0, 135, 85]), np.array([255, 180, 135]))
retval, otsu = cv2.threshold(inrangeHsv, 150, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
cnts = cv2.findContours(otsu.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
c = max(cnts, key=cv2.contourArea)
mask = cv2.drawContours(otsu, [c], -1, (0,255,0), 2)
out = cv2.distanceTransform(mask, distanceType=cv2.DIST_L2, maskSize=5)
cv2.imshow("distance_transform", out)
cv2.waitKey(0)
cv2.destroyAllWindows()
但是结果应该更像:
我应该如何解决?
您的问题是imshow()无法正确显示图像。您需要执行imwrite()。
这在Python / OpenCV中对我有用。
输入:
import cv2
import numpy as np
# read input
img = cv2.imread('dog.jpg')
# convert to YCbCr
ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
# use inRange thresholding
thresh = cv2.inRange(ycrcb, np.array([0, 135, 85]), np.array([255, 180, 135]))
# get outer contour
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
big_contour = max(cnts, key=cv2.contourArea)
# draw filled contour on black background
filled = np.zeros_like(thresh)
cv2.drawContours(filled, [big_contour], -1, (255), cv2.FILLED)
# get distance transform
result = filled.copy()
result = cv2.distanceTransform(result, distanceType=cv2.DIST_L2, maskSize=3, dstType=cv2.CV_8U)
# save result
cv2.imwrite('dog_thresh.png', thresh)
cv2.imwrite('dog_filled_contour.png', filled)
cv2.imwrite('dog_distance.png', result)
# show results
# note result image will look binary
cv2.imshow("thresh", thresh)
cv2.imshow("filled", filled)
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
阈值图像:
填充轮廓:
距离结果: