如何访问图像中检测到的人脸中的人脸像素?

发布于 2024-12-12 07:15:46 字数 1486 浏览 0 评论 0原文

我使用 nashruddin.com 中的源代码在网络摄像头实时视频流中检测到的脸部上绘制一个矩形。

我想将脸或整个矩形更改为黑色。 我尝试使用此页面中的解决方案: http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html#SECTION00053000000000000000

但是我看不到脸或矩形变为黑色。

我实际尝试过的,其中一个示例是这样的:

//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 }}

我想做的是,通过使用也在 cvRectangle 中使用的点(即点 r)来声明宽度和高度。

然而,输出的大小永远不会与矩形的外观相同。输出通常是一个矩形或正方形,颜色为黑色,但与矩形不同,尺寸更小,正如它所假设的那样,它们也更小并且不对齐。

我不知道为什么。

请帮忙。 目的是用黑色覆盖脸部,用黑色像素隐藏脸部。

请帮我。 谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

战皆罪 2024-12-19 07:15:47

首先,您正在重复使用 i 变量,这是一个大问题。更改最上面的 for 循环,这很令人困惑。

另外,你最里面的两个 for 循环是错误的。它们应该是:

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

请记住,您正在循环绝对坐标,而宽度和高度是相对术语。通常,您还希望 y 变量位于外循环中,因为它可以使内存访问更快(至少对于 OpenCV 而言)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文