Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does python opencv detect a face?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to detect human face by python opencv". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to detect human face by python opencv".

How the program detects human face

Of course, there are many ways to recognize faces, especially for simple and direct types such as two-inch photos. However, there is really no need for face detection, you can take another look at the photos:

In addition to the blue of the background is the white of the clothes, the black of the hair and the yellow of the skin, so it is easy to distinguish, you can use the histogram backprojection, about the histogram backprojection is not clear, you can click on this article: alas, learn the histogram again: histogram backprojection

Int main () {/ / [1] reads the original picture and projection template Mat srcImage, dstImage; srcImage = imread ("Portrait .jpg", 33); imshow ("original Image", srcImage)

Mat RoiImage = imread ("projection template .png"); / / [2] convert HSV model Mat HsvImage, RoiImage_HSV; cvtColor (srcImage, HsvImage, COLOR_BGR2HSV); cvtColor (RoiImage, RoiImage_HSV, COLOR_BGR2HSV); / / [3] calculate highway histogram MatND roiHist; / / histogram object int dims = 2; / / number of features (histogram dimension) float hranges [] = {0180} / / the value range of feature space float Sranges [] = {0256}; const float * ranges [] = {hranges,Sranges}; int size [] = {20 dims 32}; / / the array int channels [] = {0c1} that stores the size of the histogram of each dimension; / / the number of channels calcHist (& RoiImage_HSV, 1, channels, Mat (), roiHist, dims, size, ranges) / / [4] histogram normalized normalize (roiHist, roiHist, 0,255, NORM_MINMAX)

/ / [5] reverse projection Mat proImage; / / projection output image calcBackProject (& HsvImage, 1, channels, roiHist, proImage, ranges); imshow ("projection", proImage); / / Image mask Mask operation Mat maskImage; threshold (proImage, maskImage, 1,255, THRESH_BINARY); / / binarization of a pair of mask to further process imshow ("binary pickling film", maskImage); dstImage = Mat::zeros (srcImage.size (), CV_8UC3) SrcImage.copyTo (dstImage, maskImage); imshow ("mask operation", dstImage)

WaitKey (); return 0;}

This code uses the histogram to get a mask image, in which the face part is 255 and the rest is 0:

The left picture is the projection (grayscale image) obtained after the histogram backprojection, and the right picture is the binary mask obtained from the binarization of the projection image.

Then call the custom coding function to code the marked mask area:

/ / Custom coding function Mat MosaicImage = trans2Mosaic (srcImage, maskImage, 10); imshow ("mosaic effect", MosaicImage)

Isn't it beautiful! I feel like I've become the diamond queen of X-Men.

Custom function code, function input sequence for the original image, image pickling film, mosaic size:

/ / original image, pickling film, mosaic size Mat trans2Mosaic (Mat srcImage, Mat maskImage, int half_patch) {Point2d point;// Mat frame, mask; srcImage.copyTo (frame); maskImage.copyTo (mask); int height = frame.rows;// get the length and width of the image int width = frame.cols; / / traversal image, step size is mosaic size for (int x = 0; x < height; x = x + half_patch) {for (int y = 0) Y < width; y = y + half_patch) {/ / if it is not a marked area, skip if (mask.at (x, y) = = 0) {continue;} else {/ / if it is a marked area, reassign the pixel int b = theRNG (). Uniform (0,255); int g = theRNG (). Uniform (0,255) Int r = theRNG (). Uniform (0,255); for (int dx = 0; dx < half_patch; dx++) {for (int dy = 0; dy < half_patch; dy++) {frame.at (x + dx, y + dy) [0] = (uchar) b; frame.at (x + dx, y + dy) [1] = (uchar) g Frame.at (x + dx, y + dy) [2] = (uchar) r;} return frame;}

The idea of this function is very simple, that is, to traverse the image pixels, the step size is a custom mosaic size, and then determine whether the pixel belongs to the marked (face) region, otherwise skip it, then take the change point as the starting point to traverse the left side of the change point and the lower side of a mosaic size region, and assign the pixels of this area randomly.

Thank you for your reading, the above is the content of "how python opencv detects human face". After the study of this article, I believe you have a deeper understanding of how python opencv detects human face, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report