从二进制图像中屏蔽 blob [英] Masking a blob from a binary image

查看:34
本文介绍了从二进制图像中屏蔽 blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 openCV 和 C++ 对行走进行运动识别,我想创建一个蒙版或复制图像,以实现所提供图片中的效果..以下是图片的解释可以看到由此产生的人类行走的斑点.然后,创建原始帧的蒙版图像或复制图像,现在对二进制人体斑点进行蒙版,并将未蒙版像素设置为零.结果是提取的具有黑色背景的人体.下图显示了人类斑点是如何被提取然后被屏蔽的.这是针对视频序列的每 5 帧进行的.到目前为止,我的代码包括获取每 5 帧,对其进行灰度化,找到所有 blob 的区域,并应用阈值来获得二进制图像,其中或多或少,只有人类 blob 是白色的,图像的其余部分是黑色的.现在,我正在尝试提取人体,但我不知道如何进行.请帮帮我.

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. .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;
}

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

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.

推荐答案

最简单的方法是在图像中寻找最大的斑点(cvfind contours 可以是您需要的功能),然后设置为 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

这篇关于从二进制图像中屏蔽 blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆