我不确定那个错误是什么,但是我刚刚为你测试了一个可能的解决方案。虽然这是用C++写的,但我认为你可以很容易地将它转换成Python。
std::string test_image_path = "Galicia.png";
cv::namedWindow("Input image", cv::WINDOW_NORMAL);
cv::namedWindow("Input image R", cv::WINDOW_NORMAL);
cv::namedWindow("Input image G", cv::WINDOW_NORMAL);
cv::namedWindow("Input image B", cv::WINDOW_NORMAL);
cv::namedWindow("Input image A", cv::WINDOW_NORMAL);
cv::namedWindow("Output image", cv::WINDOW_NORMAL);
cv::Mat test_image = cv::imread(test_image_path, cv::IMREAD_UNCHANGED);
std::cout << "Image type: " << test_image.type() << std::endl;
std::vector<cv::Mat> pngChannels(4);
cv::split(test_image, pngChannels);
cv::imshow("Input image", test_image);
cv::imshow("Input image R", pngChannels[0]);
cv::imshow("Input image G", pngChannels[1]);
cv::imshow("Input image B", pngChannels[2]);
cv::imshow("Input image A", pngChannels[3]);
pngChannels[0].setTo(cv::Scalar(255), pngChannels[3]==0);
pngChannels[1].setTo(cv::Scalar(255), pngChannels[3]==0);
pngChannels[2].setTo(cv::Scalar(255), pngChannels[3]==0);
cv::Mat test_image_output;
cv::merge(pngChannels, test_image_output);
cv::imshow("Output image", test_image_output);
cv::cvtColor(test_image_output, test_image_output, cv::COLOR_RGBA2RGB);
cv::imwrite("Galicia_mod.png", test_image_output);
我通过这张截图来补充我的代码,它可能会帮助你更好地理解我的解决方案:
![enter image description here](https://istack.dev59.com/JSZXH.webp)
祝一切顺利,
Arritmic