如何在Java中使用OpenCV将透明像素设置为白色?

huangapple go评论169阅读模式
英文:

how to set transparent pixel to white in java using opencv?

问题

//生成透明位的掩码
Mat trans_mask = new Mat();
Core.compare(image.col(3), new Scalar(0), trans_mask, Core.CMP_EQ);

//用白色和非透明颜色替换透明区域
Mat mask_values = new Mat(4, 1, CvType.CV_8U);
mask_values.setTo(new Scalar(255));
image.setTo(mask_values, trans_mask);
//我不明白,为什么会有人给这个贴踩。这个“简单”的解决方案如此清晰,你只需在循环中为每个像素赋值,但这并不像这种方式高效。我搜索了一下也没有找到线索。为什么这些人一开始就不理解这个问题并尝试讨论它。如果你们不知道,就算了吧。

//如果你尝试在Java中高效处理图像,这个问题对你很有用。
英文:

what's the equivalent code in java using opencv ?

#make mask of where the transparent bits are
trans_mask = image[:,:,3] == 0

#replace areas of transparency with white and not transparent
image[trans_mask] = [255, 255, 255, 255]

updated:

I don't get it, why people downvote this. the "easy" solution is so clear that you assign each pixel in a loop, but that would not as efficient as this way. I searched for it and got no clues. why these people don't understand this question in the first place and try to discuss it. if you guys don't know it just leave it.

if you try to process images in java efficiently, this question is useful to you.

答案1

得分: 0

我找到了这个,感谢新的 Bing。

Mat img = Imgcodecs.imread(imgPath, Imgcodecs.IMREAD_UNCHANGED);
Mat dst = new Mat(img.size(), CvType.CV_8UC4);
Core.inRange(img, new Scalar(0, 0, 0, 0), new Scalar(255, 255, 255, 0), dst);
Imgproc.cvtColor(dst, dst, Imgproc.COLOR_GRAY2BGRA);
Core.add(img, dst, dst);
英文:

found this, thanks for new bing

Mat img = Imgcodecs.imread(imgPath, Imgcodecs.IMREAD_UNCHANGED);
Mat dst = new Mat(img.size(), CvType.CV_8UC4);
Core.inRange(img, new Scalar(0, 0, 0, 0), new Scalar(255, 255, 255, 0), dst);
Imgproc.cvtColor(dst, dst, Imgproc.COLOR_GRAY2BGRA);
Core.add(img, dst, dst);

huangapple
  • 本文由 发表于 2023年7月18日 11:19:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709338.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定