def new_binaryMask(frame, x0, y0, width, height): cv2.rectangle(frame, (x0, y0), (x0 + width, y0 + height), (0, 255, 0)) # 画出截取的手势框图 roi = frame[y0:y0 + height, x0:x0 + width] # 获取手势框图 #cv2.imshow("roi", roi) # 显示手势框图 res = skinMask2(roi) # 进行肤色检测 # cv2.imshow("肤色检测后的图像", res) # 显示肤色检测后的图像 ###############服饰膨胀############################## kernel = np.ones((3, 3), np.uint8) # 设置卷积核 erosion = cv2.erode(res, kernel) # 腐蚀操作 # cv2.imshow("erosion", erosion) dilation = cv2.dilate(erosion, kernel) # 膨胀操作 # cv2.imshow("dilation", dilation) # # ##############轮廓提取####################################### # ret = new_Ca nny(dilation,res) # print(dilation.shape) ret, descirptor_in_use = fd.fourierDesciptor(dilation) black = fd.reconstruct(dilation,descirptor_in_use) # print(ret.shape) # cv2.imshow("black", black) # cv2.imshow("ret", ret) # print("fourier_result",fourier_result) # cv2.imshow("fourier_result", fourier_result) # print(ret_image.shape) # cv2.imshow("ret_image", ret_image) # cv2.imshow("fourier_result", fourier_result) return ret
def new_binaryMask(frame, x0, y0, width, height): cv2.rectangle(frame, (x0, y0), (x0 + width, y0 + height), (0, 255, 0)) # 画出截取的手势框图 roi = frame[y0:y0 + height, x0:x0 + width] # 获取手势框图 #cv2.imshow("roi", roi) # 显示手势框图 res = skinMask2(roi) # 进行肤色检测 cv2.imshow("res", res) # 显示肤色检测后的图像 ###############服饰膨胀############################## kernel = np.ones((3, 3), np.uint8) # 设置卷积核 erosion = cv2.erode(res, kernel) # 腐蚀操作 # cv2.imshow("erosion", erosion) dilation = cv2.dilate(erosion, kernel) # 膨胀操作 # cv2.imshow("dilation", dilation) ##############轮廓提取####################################### # binaryimg = cv2.Canny(res, 50, 200) # 二值化,canny检测 # h = cv2.findContours(binaryimg, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE) # 寻找轮廓 # contours = h[1] # 提取轮廓 # ret = np.ones(res.shape, np.uint8) # 创建黑色幕布 # cv2.drawContours(ret, contours, -1, (255, 255, 255), 1) # 绘制白色轮廓 # cv2.imshow("ret", ret) ret, fourier_result = fd.fourierDesciptor(dilation) #cv2.imshow("ret", ret) return ret
def binaryMask(frame, x0, y0, width, height): cv2.rectangle(frame, (x0, y0), (x0 + width, y0 + height), (0, 255, 0)) roi = frame[y0:y0 + height, x0:x0 + width] res = skinMask(roi) #肤色检测 ret, fourier_result = fd.fourierDesciptor(res) efd_result, K, T = efd.elliptic_fourier_descriptors(res) return roi, res, ret, fourier_result, efd_result
def binaryMask(frame, x0, y0, width, height): cv2.rectangle(frame,(x0,y0),(x0+width, y0+height),(0,255,0)) #画出截取的手势框图 roi = frame[y0:y0+height, x0:x0+width] #获取手势框图 res = skinMask(roi) #进行肤色检测 kernel = np.ones((3,3), np.uint8) #设置卷积核 res = cv2.erode(res, kernel) #腐蚀操作 res = cv2.dilate(res, kernel)#膨胀操作 ret, fourier_result = fd.fourierDesciptor(res) return roi, res, ret, fourier_result
def deom1(): cp = cv2.VideoCapture(file) if not cp.isOpened(): print("video open error!") fps = cp.get(cv2.CAP_PROP_FPS) frames = cp.get(cv2.CAP_PROP_FRAME_COUNT) width = int(cp.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cp.get(cv2.CAP_PROP_FRAME_HEIGHT)) video_writer = cv2.VideoWriter(filename=out_file, fourcc=fourcc, fps=fps, frameSize=(300, 300)) clf = joblib.load(model_path + "svm_efd_" + "train_model.m") # print(frames) for i in range(int(frames)): # for i in range(100): ret, frame = cp.read() if not ret: print("file end") reframe = frame_resize(frame, width, height) # video_writer.write(reframe) # cv2.imwrite('C:/Users/50493/Desktop/reframe.png', reframe) roi, res = pic.binaryMask(reframe, 0, 0, 300, 300) # cv2.imwrite('C:/Users/50493/Desktop/reframe.png', res) img, d = fd.fourierDesciptor(res) descirptor_in_use = abs(d) temp = descirptor_in_use[1] feature = [] for k in range(1, len(descirptor_in_use)): x_record = int(100 * descirptor_in_use[k] / temp) feature.append(x_record) returnVec = np.zeros((1, N)) for i in range(N): returnVec[0, i] = int(feature[i]) valTest = clf.predict(returnVec) if valTest == 11: cv2.putText(roi, "like", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 1) video_writer.write(roi)
def binaryMask(frame, x0, y0, width, height): cv2.rectangle(frame, (x0, y0), (x0 + width, y0 + height), (0, 255, 0)) #画出截取的手势框图 roi = frame[y0:y0 + height, x0:x0 + width] #获取手势框图 #roi = topolar(roi) #cv2.imshow("roi", roi) #显示手势框图 res = skinMask(roi) #进行肤色检测 #cv2.imshow("res", res) #显示肤色检测后的图像 ''' kernel = np.ones((3,3), np.uint8) #设置卷积核 res = cv2.erode(res, kernel) #腐蚀操作 res = cv2.dilate(res, kernel)#膨胀操作 cv2.imshow("res", res) #显示肤色检测后的图像 ''' ret, fourier_result = fd.fourierDesciptor(res) efd_result, K, T = efd.elliptic_fourier_descriptors(res) #ret = fd.reconstruct(res, fourier_result, MIN_DESCRIPTOR) return roi, res, ret, fourier_result, efd_result
def deom2(): cameraCapture = cv2.VideoCapture(0) cameraCapture.open(0) cv2.namedWindow('show') fps = cameraCapture.get(cv2.CAP_PROP_FPS) frames = cameraCapture.get(cv2.CAP_PROP_FRAME_COUNT) width = int(cameraCapture.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cameraCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)) # video_writer = cv2.VideoWriter(filename=out_file, fourcc=fourcc, fps=fps, # frameSize=(300, 300)) clf = joblib.load(model_path + "svm_efd_" + "train_model.m") ret, frame = cameraCapture.read() while ret: cv2.waitKey(1) ret, frame = cameraCapture.read() reframe = frame_resize(frame, width, height) roi, res = pic.binaryMask(reframe, 0, 0, 300, 300) img, d = fd.fourierDesciptor(res) descirptor_in_use = abs(d) temp = descirptor_in_use[1] feature = [] for k in range(1, len(descirptor_in_use)): x_record = int(100 * descirptor_in_use[k] / temp) feature.append(x_record) returnVec = np.zeros((1, N)) for i in range(N): returnVec[0, i] = int(feature[i]) valTest = clf.predict(returnVec) if valTest == 11: cv2.putText(roi, "like", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 1) # video_writer.write(roi) cv2.imshow('show', roi) cameraCapture.release()
#!/usr/bin/env python # -*-coding:utf-8 -*- import efd import fourierDescriptor as fd import cv2 import numpy as np path = './' + 'feature' + '/' path_img = './' + 'image' + '/' if __name__ == "__main__": for i in range(1, 11): for j in range(1, 201): roi = cv2.imread(path_img + str(i) + '_' + str(j) + '.png') descirptor_in_use = abs(fd.fourierDesciptor(roi)) fd_name = path + str(i) + '_' + str(j) + '.txt' # fd_name = path + str(i) + '.txt' with open(fd_name, 'w', encoding='utf-8') as f: temp = descirptor_in_use[1] for k in range(1, len(descirptor_in_use)): x_record = int(100 * descirptor_in_use[k] / temp) f.write(str(x_record)) f.write(' ') f.write('\n') print(i, '_', j, '完成') ''' for i in range(1, 11): for j in range(1, 21): roi = cv2.imread(path_img + str(i) + '_' + str(j) + '.png')