2014-09-26 2 views
0

사진 묶음 위에 오버레이해야하는 4 개의 채널이있는 이미지가 있습니다. 3 채널의 사진 위에 오버레이가 잘 작동하지만 알파 채널이있는 사진의 경우 사진의 배경이 검은 색으로 바뀝니다.4 채널 이미지 오버레이

원본 사진 : http://img.blog.csdn.net/20130610074054484

오버레이 사진 : 당신이 배경 이미지 1 - 불투명도를 사용하기 때문에

void overlayImage(const cv::Mat &background, const cv::Mat &foreground, 
       cv::Mat &output, cv::Point2i location) 
{ 
    background.copyTo(output); 

    for(int y = std::max(location.y , 0); y < background.rows; ++y) 
    { 
     int fY = y - location.y; 
     if(fY >= foreground.rows) 
      break; 

     for(int x = std::max(location.x, 0); x < background.cols; ++x) 
     { 
      int fX = x - location.x; 
      if(fX >= foreground.cols) 
       break; 

      double opacity = ((double)foreground.data[fY * foreground.step + fX * foreground.channels() + 3])/255.; 

      for(int c = 0; opacity > 0 && c < output.channels(); ++c) 
      { 
       unsigned char foregroundPx = foreground.data[fY * foreground.step + fX * foreground.channels() + c]; 
       unsigned char backgroundPx = background.data[y * background.step + x * background.channels() + c]; 
       output.data[y*output.step + output.channels()*x + c] = 
       backgroundPx * (1.-opacity) + foregroundPx * opacity; 
      } 
     } 
    } 
} 
+0

이미 (http://docs.opencv.org/doc/tutorials/core/adding_images/adding_images.html) [OpenCV의 사용 (혼합) 두 개의 이미지 추가]를 살펴했다? –

+0

예. 어쨌든 고마워. highgui의 imshow 구현은 투명 이미지를 올바르게 표시하지 못합니다. 만약 내가 imshow 최종 이미지 (overlaying 후) 전화, 배경은 검은 색입니다. 최종 이미지에서 imwrite를 호출하면 이미지가 좋아 보인다. –

답변

0

이것은 : http://imgur.com/mlVAN0A

이 오버레이를 수행하는 코드입니다. forground 이미지의 불투명도가 0이면 배경 픽셀의 불투명도는 0이 아닌 1이됩니다.

둘 모두에 대해 0이 될 수있는 두 가지 이미지의 결과 불투명도를 계산해야합니다.

클로스

+0

정직하게 코드를 바꾸어야하는지 이해할 수 없다. –

+0

문제의 일부 지점에서 이미지를 인코딩하고 디코딩하는 것은 디코드를 호출 할 때 두 번째 매개 변수가 CV_LOAD_IMAGE_ANYCOLOR이고 CV_LOAD_IMAGE_UNCHANGED가 아니라는 것이었다. 클로스와 도비 감사합니다. 대답은 지금 닫을 수 있습니다. –