使用openCV进行alpha混合

时间:2012-02-26 08:37:42

标签: c++ image-processing opencv computer-vision alphablending

我正在努力将两个投资回报率融合在一起。一个是灰度图像的ROI,另一个是二进制图像的ROI,我使用cvNot反转,以使对象为白色,背景为黑色。我想要的输出与我现在使用我的代码得到的结果相反。在图片中,二进制图像被放在灰度图像上,但是我希望它是另一种方式,以便在图片中当前白色的位置是灰度级的身体,灰度级的背景将是黑色的。 alpha blended output

这是我的代码。请有人通过它并告诉我需要更改什么来获得我上面描述的输出?我真的非常感激。

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

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* new_frame = NULL ; 
    IplImage* res_frame = NULL;
int thresh = 17;    

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

while(1) {
    color_frame = cvQueryFrame( capture );//Grabs the frame from a file
    if( !color_frame ) break;

    new_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

    res_frame = cvCreateImage(cvSize(color_frame->width, color_frame->height), color_frame->depth, 1);

    frameCount++;
    if(frameCount==5)
    {
        cvCvtColor(color_frame, new_frame, CV_BGR2GRAY);
        cvThreshold(new_frame, res_frame, thresh, 255, CV_THRESH_BINARY);
        cvErode(res_frame, res_frame, NULL, 1);
        cvDilate(res_frame, res_frame, NULL, 1);
        cvNot (res_frame, res_frame);

        int x = 75;
        int y = 5;
        int width = 125;
        int height = 1500;

        double alpha = 1;
        double beta = 1;

        cvSetImageROI(res_frame, cvRect(x, y, width, height));
        cvSetImageROI(new_frame, cvRect(x, y, width, height));

        //cvResetImageROI(new_frame);
        cvAddWeighted(res_frame, alpha, new_frame, beta, 0.0, res_frame);
        cvShowImage("contours", res_frame);

         frameCount=0;
    }
    char c = cvWaitKey(33);
    if( c == 27 ) break;
}

cvReleaseImage(&color_frame);
cvReleaseImage(&new_frame);
cvReleaseImage(&res_frame);
cvReleaseCapture( &capture );
cvDestroyWindow( "contours" );

return 0;
}

2 个答案:

答案 0 :(得分:2)

根据您的阈值功能,我假设您要查看图像的黑色/深灰色部分。 您可以使用res_frame作为掩码来选择new_frame的相关部分,而不是使用cvAddWeighted:

cvCopy(new_frame,new2_frame,res_frame);

使用屏蔽副本,只在res_frame不为零的那些像素处在new2_frame中创建new_frame的副本。如果你仍然需要roi,new2_frame需要与new_frame相同或相同的roi大小。

答案 1 :(得分:1)

@rics,这是你的意思吗?

#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* new_frame = NULL ; 
IplImage* res_frame = NULL;
IplImage* new2_frame = NULL ;
int thresh = 17;    

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

while(1) {
    color_frame = cvQueryFrame( capture );//Grabs the frame from a file
    if( !color_frame ) break;

    new_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

    res_frame = cvCreateImage(cvSize(color_frame->width, color_frame->height), color_frame->depth, 1);
    new2_frame = cvCreateImage(cvSize(color_frame->width, color_frame->height), color_frame->depth, 1);


    frameCount++;
    if(frameCount==5)
    {
        cvCvtColor(color_frame, new_frame, CV_BGR2GRAY);
        cvThreshold(new_frame, res_frame, thresh, 255, CV_THRESH_BINARY);
        cvErode(res_frame, res_frame, NULL, 1);
        cvDilate(res_frame, res_frame, NULL, 1);
        cvNot (res_frame, res_frame);

        int x = 75;
        int y = 5;
        int width = 125;
        int height = 1500;

        //double alpha = 1;
        //double beta = 1;

        cvSetImageROI(res_frame, cvRect(x, y, width, height));
        cvSetImageROI(new_frame, cvRect(x, y, width, height));

        //cvResetImageROI(new_frame);
        //cvAddWeighted(res_frame, alpha, new_frame, beta, 0.0, res_frame);
        cvCopy(new_frame,new2_frame,res_frame);
        cvShowImage("contours", new2_frame);

         frameCount=0;
    }
    char c = cvWaitKey(33);
    if( c == 27 ) break;
}

cvReleaseImage(&color_frame);
cvReleaseImage(&new_frame);
cvReleaseImage(&res_frame);
cvReleaseImage(&new2_frame);
    cvReleaseCapture( &capture );
cvDestroyWindow( "contours" );

return 0;
}