简体   繁体   中英

How to access the face pixel in a detected face from an image?

I have use the source code in nashruddin.com to draw a rectangle on the detected face from live video stream from my webcam.

I want to change the face, or the whole rectangle as black colour. I have tried using solutions from this page: http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html#SECTION00053000000000000000

however i cant get the face or the rectangle to become black.

What i actually tried, one of the examples is like this:

//do the capture frame all those

 for( int i = 0 ; i < ( faces ? faces->total : 0 ) ; i++ ) {

 CvRect *r = ( CvRect* )cvGetSeqElem( faces, i );        
 cvRectangle( frame,
              cvPoint( r->x, r->y ),
              cvPoint( r->x + r->width, r->y + r->height ),
              CV_RGB( 255, 0, 0 ), 1, 8, 0 );

 int x=r->x;

 int y=r->y;

 int width=r->width;

 int height=r->height;

 for(int i=x; i<=height; i++){

 for(int j=y; j<=width; j++){

 //data[i*step+j*channels+0] = 0;//3=yellow; 2=blue; 1=purple;

((unsigned char *)(frame->imageData + i*frame->widthStep))[j*frame->nChannels + 0]=0;// B 

((unsigned char *)(frame->imageData + i*frame->widthStep))[j*frame->nChannels + 1]=0; // G 

((unsigned char *)(frame->imageData + i*frame->widthStep))[j*frame->nChannels + 2]=0; // R }}

What i want to do is, i declare the width and height, by using the points which also are using in the cvRectangle, which is the point r.

However, the output, are never same in size as how the rectangle look like. The output is usually a rectangle or square, black in color but with different different and smaller size as the rectangle, which it suppose to be, and they are also smaller and out of allignment.

I don't know why.

Please help. The aim is to cover the face with black, to hide the face with black pixels.

Please help me. Thank you.

Well first of all, you're re-using the i variable which is a big problem. Change that topmost for loop, that's just confusing.

Also, your innermost two for loops are wrong. They should be:

for(int i = y; i < y + height; i++){
    for(int j = x; j < x + width; j++){

Keep in mind that you're looping over absolute coordinates, and width and height are relative terms. Also generally you want the y variable to be on the outer loop, because it makes memory access quicker (at least with OpenCV).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM