사진 묶음 위에 오버레이해야하는 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;
}
}
}
}
이미 (http://docs.opencv.org/doc/tutorials/core/adding_images/adding_images.html) [OpenCV의 사용 (혼합) 두 개의 이미지 추가]를 살펴했다? –
예. 어쨌든 고마워. highgui의 imshow 구현은 투명 이미지를 올바르게 표시하지 못합니다. 만약 내가 imshow 최종 이미지 (overlaying 후) 전화, 배경은 검은 색입니다. 최종 이미지에서 imwrite를 호출하면 이미지가 좋아 보인다. –