Detecting Skin in Images & Video Using Python and OpenCV皮肤检测
2017-08-20 18:58
531 查看
简介
使用video或者摄像头,检测皮肤区域,我们可以获得区域内的皮肤图像。skindetector.py
# import the necessary packages from pyimagesearch import imutils import numpy as np import argparse import cv2 # construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-v", "--video", help = "path to the (optional) video file") args = vars(ap.parse_args()) # define the upper and lower boundaries of the HSV pixel # intensities to be considered 'skin' lower = np.array([0, 48, 80], dtype = "uint8") upper = np.array([20, 255, 255], dtype = "uint8")
这里,我们定义了HSV的颜色空间,最低为[0, 48, 80],最高为[20, 255, 255],注意不是RGB的。
选择的设备或video
# if a video path was not supplied, grab the reference # to the gray if not args.get("video", False): camera = cv2.VideoCapture(0) # otherwise, load the video else: camera = cv2.VideoCapture(args["video"])
在第20行有“–video”表示选择用video做检测,如果使用cv2.VideoCapture=0的参数,表示采用了摄像头设备。
开始读取每一帧图像
# keep looping over the frames in the video while True: # grab the current frame (grabbed, frame) = camera.read() # if we are viewing a video and we did not grab a # frame, then we have reached the end of the video if args.get("video") and not grabbed: break # resize the frame, convert it to the HSV color space, # and determine the HSV pixel intensities that fall into # the speicifed upper and lower boundaries frame = imutils.resize(frame, width = 400) converted = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) skinMask = cv2.inRange(converted, lower, upper) # apply a series of erosions and dilations to the mask # using an elliptical kernel kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11)) skinMask = cv2.erode(skinMask, kernel, iterations = 2) skinMask = cv2.dilate(skinMask, kernel, iterations = 2) # blur the mask to help remove noise, then apply the # mask to the frame skinMask = cv2.GaussianBlur(skinMask, (3, 3), 0) skin = cv2.bitwise_and(frame, frame, mask = skinMask) # show the skin in the image along with the mask cv2.imshow("images", np.hstack([frame, skin])) # if the 'q' key is pressed, stop the loop if cv2.waitKey(1) & 0xFF == ord("q"): break # cleanup the camera and close any open windows camera.release() cv2.destroyAllWindows()
camera.read() 函数 返回的是元组数据格式,包含两个变量 grabbed 和 frame . grabbed是boolean逻辑值,表示是否读取到最后结束了,表明当前这一帧图像是否成功读取。imutils.resize是将当前帧图片在不压缩的情况下,缩小到width = 400。GaussianBlur进行了高斯滤波,cv2.erode与cv2.dilate是腐蚀与膨胀操作。
相关文章推荐
- 用 Python 和 OpenCV 检测图片上的条形码Detecting Barcodes in Images with Python and OpenCV
- 自然图像中的logo识别和定位:Logo localization andrecognition in natural images using homographic class graphs
- OpenCV Using Python——RGB颜色空间和YCbCr颜色空间的混合肤色检测
- Mouse and keyboard operation in windows using python
- how to config the tftpd-hpa server in the ubuntu using for remote tftp client '-p' and '-r' command
- State of the "Art": A Taxonomy of Artistic Stylization Techniques for Images and Video(二)
- install opencv3 with python2.7 bindings on Mac using Hombrew and git
- Python&OpenCV - 读写(read&write)视频(video) 详解 及 代码
- State of the "Art": A Taxonomy of Artistic Stylization Techniques for Images and Video(三)
- [ReactVR] Animate Text, Images, Views, and 3D Elements Using the Animated Library in React VR
- 用Python和OpenCV实现RootSIFT--Implementing RootSIFT in Python and OpenCV
- Caustics Mapping: Explanation and Implementation in DirectX 9 Figure 1: Images rendered using
- 【计算机视觉】<前景检测>ViBe - a powerful technique for background detection and subtraction in video sequences
- real-time-drone-object-tracking-using-python-and-opencv
- OpenCV Using Python——HSV颜色空间和改进的YCbCr颜色空间中的肤色检测
- An Introduction to Interactive Programming in Python (Part 2) week 6 (Classes and Tied images)
- Python数据处理 PCA/ZCA 白化(UFLDL教程:Exercise:PCA_in_2D&PCA_and_Whitening)
- How to read and save Images in a Sql Server Database using ADO.NET and C#
- State of the "Art": A Taxonomy of Artistic Stylization Techniques for Images and Video(六)
- 关于专利APPROACH FOR RESOLVING OCCLUSIONS, SPLITS AND MERGES IN VIDEO IMAGES