我想將2個ROI融合在一起。一個是灰度圖像的ROI,另一個是我使用cvNot反轉的二值圖像的ROI,以便將對象變成白色,將背景變成黑色。我想要的輸出與我現在用我的代碼得到的結果是相反的。在圖片中,二進制圖像放在灰度圖像上,但是我希望它是相反的方式,以便當前圖像中的白色將是灰度體,灰度背景則是黑色。 使用openCV進行alpha混合
這是我的代碼。請有人通過它並告訴我需要改變什麼來獲得我上面描述的輸出?我真的很感激。
#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;
}
我也跟着你的建議,但我得到了一個錯誤說:在3020 project.exe在0x767db9bc未處理的異常:微軟C++異常:內存位置0x0039f98c品種::例外。另外,任何調用堆棧幀都不會加載符號。源代碼無法顯示。我的代碼太長,無法放入評論中,因此我將其作爲問題的答案發布。請看看它,看看這是你的意思。 – 2012-02-26 16:58:44