从二值图像中屏蔽斑点

发布于 2025-01-07 17:18:03 字数 2107 浏览 2 评论 0原文

我正在使用 openCV 和 C++ 进行行走运动识别,我想创建一个掩模或复制图像,以达到所提供图片中看到的效果。 extracted human.以下是图像的解释 可以看到人类行走的结果。然后,创建原始帧的掩模图像或复制图像,现在对二进制人体斑点进行掩模,并且现在将非掩模像素设置为零。结果是提取出黑色背景的人体。下图显示了如何提取并屏蔽人体斑点。 视频序列的每第 5 帧都要执行此操作。到目前为止,我的代码包括获取每第 5 帧,对其进行灰度化,查找所有斑点的区域,然后应用阈值来获取二值图像,其中或多或少,只有人类斑点是白色的,图像的其余部分是白色的黑色的。现在,我正试图提取人体,但我不知道如何进行。请帮我。

#include "cv.h"
#include "highgui.h"
#include "iostream"

using namespace std;
int main( int argc, char* argv ) {

CvCapture *capture = NULL;
capture = cvCaptureFromAVI("C:\\walking\\lady walking.avi");
if(!capture){
    return -1;
}

IplImage* color_frame = NULL;
IplImage* gray_frame = NULL ;

int thresh_frame = 28;
CvMoments moments;

int frameCount=0;//Counts every 5 frames
cvNamedWindow( "walking", CV_WINDOW_AUTOSIZE );

while(1) {
    color_frame = cvQueryFrame( capture );//Grabs the frame from a file
    if( !color_frame ) break;
    gray_frame = cvCreateImage(cvSize(color_frame->width, color_frame->height), color_frame->depth, 1);
    if( !color_frame ) break;// If the frame does not exist, quit the loop


    frameCount++;
    if(frameCount==5)
    {
        cvCvtColor(color_frame, gray_frame, CV_BGR2GRAY);
        cvThreshold(gray_frame, gray_frame, thresh_frame, 255, CV_THRESH_BINARY);
        cvErode(gray_frame, gray_frame, NULL, 1);
        cvDilate(gray_frame, gray_frame, NULL, 1);

        cvMoments(gray_frame, &moments, 1);
        double m00;
        m00 = cvGetCentralMoment(&moments, 0,0);

        cvShowImage("walking", gray_frame);
        frameCount=0;
    }
    char c = cvWaitKey(33);
    if( c == 27 ) break;
}

double m00 = (double)cvGetCentralMoment(&moments, 0,0);
cout << "Area - : " << m00 << endl;
//area of lady walking = 39696. Therefore, using new threshold area as 30 for this video
//area of walking man = 67929

cvReleaseImage(&color_frame);
cvReleaseImage(&gray_frame);
cvReleaseCapture( &capture );
cvDestroyWindow( "walking" );

return 0;
}

我还想上传我在代码中使用的视频,但我不知道如何在这里上传它,所以如果有人也可以帮助我。我想为我的问题提供尽可能多的信息。

I am doing motion recognition of walking using openCV and C++ and I would like to create a mask or copied image in order to achieve the effect seen in the picture provided. extracted human.The following is an explanation of the images
The resulting blob of the human walking is seen. Then, a mask image or copied image of the original frame is created, the binary human blob is now masked and the non-masked pixels are now set to zero. The result is the extracted human body with a black background. The diagram below shows how the human blob is extracted and then masked.
This is to be done for every 5th frame of a video sequence. My code so far consists of getting every 5th frame, grayscaling it, finding the areas of all the blobs, and applying a threshold value to get a binary image where more or less, only the human blob is white and the rest of the image is black. Now, I am trying to extract the human body but I have no clue how to proceed. Please help me.

#include "cv.h"
#include "highgui.h"
#include "iostream"

using namespace std;
int main( int argc, char* argv ) {

CvCapture *capture = NULL;
capture = cvCaptureFromAVI("C:\\walking\\lady walking.avi");
if(!capture){
    return -1;
}

IplImage* color_frame = NULL;
IplImage* gray_frame = NULL ;

int thresh_frame = 28;
CvMoments moments;

int frameCount=0;//Counts every 5 frames
cvNamedWindow( "walking", CV_WINDOW_AUTOSIZE );

while(1) {
    color_frame = cvQueryFrame( capture );//Grabs the frame from a file
    if( !color_frame ) break;
    gray_frame = cvCreateImage(cvSize(color_frame->width, color_frame->height), color_frame->depth, 1);
    if( !color_frame ) break;// If the frame does not exist, quit the loop


    frameCount++;
    if(frameCount==5)
    {
        cvCvtColor(color_frame, gray_frame, CV_BGR2GRAY);
        cvThreshold(gray_frame, gray_frame, thresh_frame, 255, CV_THRESH_BINARY);
        cvErode(gray_frame, gray_frame, NULL, 1);
        cvDilate(gray_frame, gray_frame, NULL, 1);

        cvMoments(gray_frame, &moments, 1);
        double m00;
        m00 = cvGetCentralMoment(&moments, 0,0);

        cvShowImage("walking", gray_frame);
        frameCount=0;
    }
    char c = cvWaitKey(33);
    if( c == 27 ) break;
}

double m00 = (double)cvGetCentralMoment(&moments, 0,0);
cout << "Area - : " << m00 << endl;
//area of lady walking = 39696. Therefore, using new threshold area as 30 for this video
//area of walking man = 67929

cvReleaseImage(&color_frame);
cvReleaseImage(&gray_frame);
cvReleaseCapture( &capture );
cvDestroyWindow( "walking" );

return 0;
}

I would also like to upload the video that I am using in the code but I don't know how to upload it here, so if anyone can help me out with that too. I want to provide as much info as possible w.r.t. my question.

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

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

发布评论

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

评论(1

星光不落少年眉 2025-01-14 17:18:03

最简单的方法是寻找图像中最大的斑点(cvfind 轮廓可以是您需要的函数),然后将所有其他斑点设置为 blac(扫描所有轮廓并使用 cvfloadfill)。
最后,扫描整个二值图像,如果所考虑的像素是白色,则不执行任何操作,如果像素是黑色,则将第 5 帧的相应像素设置为黑色

the easiest way is to look for the biggest blob in the image (cvfind contours can be the function you need), then you set to blac all the other blobs (scannig all the contours and using cvfloadfill).
finally you scan the entire binary image if the considered pixel is white you do nothing, if the pixel is black you set to black the corresponding pixel of the 5th frame

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