2013-07-23 30 views
5

我想混合兩個圖像。如果它們具有相同的尺寸,則很容易,但是如果其中一個圖像較小或較大,則cv :: addWeighted失敗。使用opencv複製/混合不同大小的圖像

圖片A(預期爲大) 圖片B(預期更小)

我試圖創建一個ROI - 試圖創建A的尺寸的第三圖像,並複製的內部B - I可以似乎沒有對的。請幫忙。

double alpha = 0.7; // something 
int min_x = (A.cols - B.cols)/2); 
int min_y = (A.rows - B.rows)/2); 
int width, height; 
if(min_x < 0) { 
    min_x = 0; width = (*input_images).at(0).cols - 1; 
} 
else   width = (*input_images).at(1).cols - 1; 
if(min_y < 0) { 
    min_y = 0; height = (*input_images).at(0).rows - 1; 
} 
else   height = (*input_images).at(1).rows - 1; 
cv::Rect roi = cv::Rect(min_x, min_y, width, height);    
cv::Mat larger_image(A); 
// not sure how to copy B into roi, or even if it is necessary... and keep the images the same size 
cv::addWeighted(larger_image, alpha, A, 1-alpha, 0.0, out_image, A.depth()); 

即使像cvSetImageROI - 可以工作,但我無法找到C++等同 - 可幫助 - 但我不知道如何使用它仍然保持靜止圖像內容,只有把內部投資回報率的另一個圖像...

+0

您是否希望'out_image'爲(1)與'B'混合的'A'的裁剪部分(即「B」的大小)或(2) 「A」的大小以及與「B」混合的選定部分(因此「A」的大小)? – Jacob

+0

A的大小 - 與選定部分混合B – Thalia

+0

爲什麼'width =(* input_images).at(0).cols - 1'?爲什麼不''(* input_images).at(0).cols'? – Jacob

回答

9
// min_x, min_y should be valid in A and [width height] = size(B) 
cv::Rect roi = cv::Rect(min_x, min_y, B.cols, B.rows); 

// "out_image" is the output ; i.e. A with a part of it blended with B 
cv::Mat out_image = A.clone(); 

// Set the ROIs for the selected sections of A and out_image (the same at the moment) 
cv::Mat A_roi= A(roi); 
cv::Mat out_image_roi = out_image(roi); 

// Blend the ROI of A with B into the ROI of out_image 
cv::addWeighted(A_roi,alpha,B,1-alpha,0.0,out_image_roi); 

需要注意的是,如果你想融入B直接進入A,你只需要roi

cv::addWeighted(A(roi),alpha,B,1-alpha,0.0,A(roi)); 
+1

我仍然得到相同的錯誤,因爲我總是得到:輸入參數的大小不匹配(該操作既不是'數組操作數組'(其中數組具有相同的大小和相同數量的通道),也不是'數組操作標量操作數組'在未知函數中,... \ arithm.cpp,行1277 – Thalia

+0

嘗試'cv :: Rect roi = cv :: Rect(min_x,min_y,B.cols,B.rows);' – Jacob

+0

它的種類我使用A(roi)作爲輸出 – Thalia

-1

可以使用addWeighted()功能容易混合兩個圖像

addWeighted(src1, alpha, src2, beta, 0.0, dst); 

聲明兩個圖像

src1 = imread("c://test//blend1.jpg"); 
src2 = imread("c://test//blend2.jpg"); 

聲明的alphabeta值,然後調用該函數。你完成了。您可以在鏈接中找到詳細信息:Blending of Images using Opencv

相關問題