2013-05-28 88 views
1

我是OpenCV和C++的初學者,但現在我必須爲這個問題找到解決方案: 我有一個藍色背景的人的圖像,現在我必須減去背景從圖像,然後將其替換爲另一個圖像。 現在我認爲有2種方法來解決這個問題,但我不知道哪個好:從OpenCV C++減去藍色背景圖像

解決方案1 ​​

  1. 圖像轉換到B &W¯¯
  2. 使用它作爲減去背景的面具。

解決方案2

  1. 使用coutour找到後臺,
  2. ,然後減去它。

我已經作爲解決方案1實施,但結果並非如我所料。 你知道還有另一個更好的解決方案,或者有人已經實現它作爲源代碼? 我會感謝您的幫助。

我在這裏更新我的源代碼,請給我一些意見

//Get the image with person 
cv::Mat imgRBG = imread("test.jpg"); 
//Convert this image to grayscale 
cv::Mat imgGray = imread("test.jpg",CV_LOAD_IMAGE_GRAYSCALE); 
//Get the background from image 
cv::Mat background = imread("paris.jpg"); 

cv::Mat imgB, imgW; 
//Image with black background but inside have some area black 
threshold(imgGray, imgB, 200, 255, CV_THRESH_BINARY_INV); 

cv::Mat imgTemp; 
cv::Mat maskB, maskW; 
cv::Mat imgDisplayB, imgDisplayW; 
cv::Mat imgDisplay1, imgDisplay2, imgResult;  

//Copy image with black background, overide the original image 
//Now imgTemp has black background wrap the human image, and inside the person, if there're some white area, they will be replace by black area 
imgRBG.copyTo(imgTemp, imgB); 

//Now replace the black background with white color 
cv::floodFill(imgTemp, cv::Point(imgTemp.cols -10 ,10), cv::Scalar(255.0, 255.0, 255.0)); 
cv::floodFill(imgTemp, cv::Point(10,10), cv::Scalar(255.0, 255.0, 255.0)); 
cv::floodFill(imgTemp, cv::Point(10,imgTemp.rows -10), cv::Scalar(255.0, 255.0, 255.0)); 
cv::floodFill(imgTemp, cv::Point(imgTemp.cols -10,imgTemp.rows -10), cv::Scalar(255.0, 255.0, 255.0)); 

//Convert to grayscale 
cvtColor(imgTemp,imgGray,CV_RGB2GRAY); 

//Convert to B&W image, now background is black, other is white 
threshold(imgGray, maskB, 200, 255, CV_THRESH_BINARY_INV); 

//Convert to B&W image, now background is white, other is black 
threshold(imgGray, maskW, 200, 255, CV_THRESH_BINARY); 

//Replace background of image by the black mask 
imgRBG.copyTo(imgDisplayB, maskB); 

//Clone the background image 
cv::Mat overlay = background.clone(); 

//Create ROI 
cv::Mat overlayROI = overlay(cv::Rect(0,0,imgDisplayB.cols,imgDisplayB.rows)); 

//Replace the area which will be human image by white color 
overlayROI.copyTo(imgResult, maskW); 

//Add the person image 
cv::addWeighted(imgResult,1,imgDisplayB,1,0.0,imgResult); 

imshow("Image Result", imgResult); 


waitKey(); 

return 0; 
+0

嘗試分割。使用cv :: grabCut以及非常接近邊界的前景蒙版(例如,將圖像的4個角點像素設置爲前景蒙版)。 – William

+0

親愛的威廉。你能描述更多細節嗎?如何在這種情況下使用grabCut?謝謝 – user2411800

+0

閱讀[本](http://www.packtpub.com/article/opencv-segmenting-images)並嘗試[this](http://stackoverflow.com/questions/14111716/how-to-set-a -mask圖像換grabcut合的OpenCV)。 – William

回答

0

如果你知道背景是藍色的,你是通過圖像轉換到B/W失去有價值的信息。

如果人沒有穿藍色(至少不是非常接近背景顏色的人),則不必使用輪廓。只需將藍色像素替換爲其他圖像的像素即可。您可以使用cvScalar數據類型,cvGet2D和cvSet2D函數來實現此目的。

編輯: 您的代碼看起來比您陳述的原始問題複雜得多。具有藍色背景(也稱爲「藍屏」和「色度鍵」)是電視頻道用於改變新聞閱讀器背景的常用方法。選擇藍色的原因是人類皮膚在藍色成分中占主導地位。

假設這個人沒有穿藍色,下面的代碼應該工作。讓我知道你是否需要不同的東西。

//Read the image with person 
IplImage* imgPerson = cvLoadImage("person.jpg"); 
//Read the image with background 
IplImage* imgBackground = cvLoadImage("paris.jpg"); 

// assume that the blue background is quite even 
// here is a possible range of pixel values 
// note that I did not use all of them :-) 
unsigned char backgroundRedMin = 0; 
unsigned char backgroundRedMax = 10; 
unsigned char backgroundGreenMin = 0; 
unsigned char backgroundGreenMax = 10; 
unsigned char backgroundBlueMin = 245; 
unsigned char backgroundBlueMax = 255; 

// for simplicity, I assume that both images are of the same resolution 
// run a loop to replace pixels 
for (int i=0; i<imgPerson->width; i++) 
{ 
    for (int j=0; j< imgPerson->height; j++) 
    { 
     CvScalar currentPixel = cvGet2D(imgPerson, j, i); 
     // compare the RGB values of the pixel, with the range 
     if (curEdgePixel.val[0] > backgroundBlueMin && curEdgePixel.val[1] < 
      backgroundGreenMax && curEdgePixel.val[2] < backgroundRedMax) 
      { 
      // copy the corresponding pixel from background 
      CvScalar currentBackgroundPixel = cvGet2D(imgBackground, j, i); 
      cvSet2D(imgPerson, j, i, currentBackgroundPixel); 
      } 
     } 
    } 


imshow("Image Result", imgPerson); 

waitKey(); 

return 0; 
+0

感謝您的幫助,我已經更新了我的源代碼,您能否給我一些評論? – user2411800

+0

完成。如果我沒有回答你的問題,請告訴我。 – Totoro

+1

如果您的背景不是藍色或不均勻,GrabCut是最佳選擇。 – Totoro

0

檢查這個項目

https://sourceforge.net/projects/cvchromakey

void chromakey(const Mat under, const Mat over, Mat *dst, const Scalar& color) { 

// Create the destination matrix 
*dst = Mat(under.rows,under.cols,CV_8UC3); 

for(int y=0; y<under.rows; y++) { 
    for(int x=0; x<under.cols; x++) { 

    if (over.at<Vec3b>(y,x)[0] >= red_l && over.at<Vec3b>(y,x)[0] <= red_h && over.at<Vec3b>(y,x)[1] >= green_l && over.at<Vec3b>(y,x)[1] <= green_h && over.at<Vec3b>(y,x)[2] >= blue_l && over.at<Vec3b>(y,x)[2] <= blue_h) 
     { 
       dst->at<Vec3b>(y,x)[0]= under.at<Vec3b>(y,x)[0]; 
       dst->at<Vec3b>(y,x)[1]= under.at<Vec3b>(y,x)[1]; 
       dst->at<Vec3b>(y,x)[2]= under.at<Vec3b>(y,x)[2];} 
      else{ 
       dst->at<Vec3b>(y,x)[0]= over.at<Vec3b>(y,x)[0]; 
       dst->at<Vec3b>(y,x)[1]= over.at<Vec3b>(y,x)[1]; 
       dst->at<Vec3b>(y,x)[2]= over.at<Vec3b>(y,x)[2];} 

    } 
} 

}