-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
预览窗口不支持透明图片 #3
Comments
原因: |
解决方案: |
读取一张图片,并生成一个白色背景。(Mat type 须使用 Mat imageMain = imread(imageName, IMREAD_UNCHANGED);
Mat bg(imageMain.rows, imageMain.cols, CV_8UC3, Scalar(255, 255, 255));
imageOverlay(imageMain, bg, 0, 0); 以下函数由 @TansuoTro 提供,感谢。 void imageOverlay(const Mat &pic, Mat &bg, int x, int y)
{
int channelNum = 3;
int alpha = 0;
for (int i = 0; i < pic.rows; i++)
{
for(int j = 0; j < pic.cols * 3; j += 3)
{
alpha = pic.ptr<uchar>(i)[j / 3*4 + 3];
if(alpha != 0)
{
for (int k = 0; k < 3; k++)
{
if( (i+y < bg.rows) && (i+y>=0) &&
((j+x*3) / 3*3 + k < bg.cols*3) && ((j+x*3) / 3*3 + k >= 0) &&
(i/channelNum*4 + k < bg.cols*4) && (j/channelNum*4 + k >=0) )
{
bg.ptr<uchar>(i+y)[(j+x*channelNum) / channelNum*channelNum + k] =
pic.ptr<uchar>(i)[(j) / channelNum*4 + k];
}
}
}
}
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
简而言之,OpenCV直接用
imshow
或imwrite 生成jpg
的方式,都无法正确显示 PNG 中的透明部分。在此感谢 @TansuoTro ,这位大佬提供了一个函数用于解决此问题。
下列内容改编自他的回复。
The text was updated successfully, but these errors were encountered: