我正在用opencv检测到蓝色。与此同时,我正在检测dlib面点。所以我给了他们蓝色,希望它们出现在我的脱粒中。问题是我的代码给点赋予了蓝色,并且使它们出现在cv框架中,但是没有出现在我的脱粒中。
下面是我到目前为止编写的代码:
from imutils import face_utils
import imutils
import numpy as np
import dlib
import cv2
thresh = 0.25
frame_check = 20
detect = dlib.get_frontal_face_detector()
predict = dlib.shape_predictor(
"shape_predictor_68_face_landmarks.dat") # Dat file is the crux of the code
cap = cv2.VideoCapture(0)
flag = 0
while True:
ret, frame = cap.read()
frame = imutils.resize(frame, width=450)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
subjects = detect(gray, 0)
for subject in subjects:
shape = predict(gray, subject)
shape = face_utils.shape_to_np(shape) # converting to NumPy Array
for (i, (x, y)) in enumerate(shape):
frame=cv2.circle(frame, (x, y), 2, (255, 0, 0), -1)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask2 = cv2.inRange(hsv, np.array([110, 50, 50]), np.array([130, 255, 255]))
res = cv2.bitwise_and(frame, frame, mask=mask2)
gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
median = cv2.GaussianBlur(gray, (5, 5), 0)
kernel_square = np.ones((5, 5), np.uint8)
dilation = cv2.dilate(median, kernel_square, iterations=2)
opening = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel_square)
ret, thresh = cv2.threshold(opening, 30, 255, cv2.THRESH_BINARY)
# thresh = thresh[y:y + h, x:x + w]
contours = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[1]
cv2.imshow("thresh", thresh)
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
cv2.destroyAllWindows()
cap.stop()
dlib可以将点转移到脱粒,还是我做错了。
答案 0 :(得分:0)
您应该在脱粒图像中再次绘制点,因为经过许多步骤之后,这些点可以被消除。像这样
...
shapes = []
for subject in subjects:
shape = predict(gray, subject)
shape = face_utils.shape_to_np(shape) # converting to NumPy Array
shapes.append(shape)
for (i, (x, y)) in enumerate(shape):
frame=cv2.circle(frame, (x, y), 2, (255, 0, 0), -1)
...
for shape in shapes:
for (i, (x, y)) in enumerate(shape):
thresh=cv2.circle(thresh, (x, y), 2, 255, -1) # thresh is a gray image