本文介绍了使用透明层opencv读取和显示PNG时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在做一个简单的阅读并显示PNG图像.我正在读取具有透明背景的png图像.我正在转换灰度图像,然后显示它.但是转换后的图像看起来像这样:
I am doing a simple reading and displaying PNG image. I am reading a png image with background as transparent. I am converting the image in greyscale and then displaying it. But the converted image is looking something like this:
原始图片:
灰度图像:
这是代码.我在做什么错了?:
Here is the code. What am I doing wrong?:
Mat image = imread("3X3_a11.png",IMREAD_GRAYSCALE);
Mat output(image.size(),image.type());
// connectedComponents(image, output);
imshow("Output", image);
waitKey(0);
destroyAllWindows();
推荐答案
您的RGBA图像格式错误,或者至少非常不清晰.加载时,我看到警告:
Your RGBA image is malformed, or at least is very wierd. On loading I see the warning:
libpng warning: iCCP: known incorrect sRGB profile
不过,您可以通过以下简单处理来获取cv::connectedComponents
的二进制版本:
You can however get the binary version need for cv::connectedComponents
with simple processing like:
#include <opencv2\opencv.hpp>
int main()
{
cv::Mat4b img4b = cv::imread("path_to_image", cv::IMREAD_UNCHANGED);
// Convert to grayscale getting rid of alpha channel
cv::Mat1b img(img4b.rows, img4b.cols, uchar(0));
for (int r = 0; r < img4b.rows; ++r) {
for (int c = 0; c < img4b.cols; ++c) {
if (img4b(r, c) == cv::Vec4b(255,255,255,255)) {
img(r, c) = uchar(255);
}
if (img4b(r, c)[3] == 0) {
img(r, c) = uchar(255);
}
}
}
cv::imshow("img", img);
cv::waitKey();
}
结果:
这篇关于使用透明层opencv读取和显示PNG时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!