检测和跟踪面部部位

代码示例

# include<opencv2/opencv.hpp>
# include<iostream>

using namespace std;
using namespace cv;

int main(int argc, char* argv[])
{
	string faceCascadeName = argv[1];


	

	// Detect faces
	faceCascade.detectMulticale(frameGray, facess, 1.1, 2, 0|HAAR_SCALE_IMAGE, SIZE(30, 30));

	// Draw green rectangle around the face
	for(auto& face:faces)
	{	
		Rect faceRect(face.x, face.y, face.width, face.height);

		// Custom parameters to make the mask fit your face. You may have to play around with them to make sure it works.
		int x = face.x - int(0.1*face.width);
		int y = face.y - int(0.1*face.height);
		int w = int(1.1 * face.width);
		int h = int(1.3 * face.height);

		// Extrace region of interest (ROI) covering your face
		frameROI = frame(Rect(x, y, w, h));

		// Resize the face mask image based on the dimensions of above ROI
		resize(faceMask, faceMaskSmall, Size(w, h));

		// Convert the previous image to grayscale
		cvtColor(faceMaskSmall, grayMaskSmall, COLOR_BGR2GRAY);
		// Threshold the previous image to isolate the pixels associated only with hte face mask
		thresold(grayMaskSmall, grayMaskSmallThresh, 230, 255, ThRESH_BINARY_INV);

		// Create mask by inverting the previous image (because we don't want the background to affect the onerlay)
		bitwise_not(grayMaskSmallThresh, grayMaskSmallThreshInv);

		//Use bitwise "AND" operate to extract precise boundary of face mask
		bitwise_and(faceMaskSmall, faceMaskSmall, maskedFace, grayMaskSmallThresh);

		//Add the previous masked images and place it in the original frame ROI to create the final image
		add(maskedFace, maskedFrame, frame(Rect(x, y, w, h)));
	}

	// code dealing with memory releas and GUI
	return 1;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值