C ++ |使用setTo更改cv :: mat中的颜色 [英] C++ | Change color in cv::mat with setTo
问题描述
我有一个cv :: Mat文件与vec3b值。这些值是图像的颜色。我想更改图像中的一些颜色。
I have a cv::Mat file with vec3b values. These values are colours from an image. I'd like to change some colours in that image.
我知道setTo()函数的正常矩阵操作,但如何使用它为我的Mat文件?
I know the setTo() function for normal matrix manipulation, but how do I use it for my Mat file?
我试过这样:
image = image.setto(Vec3b(0,0,0), image == Vec3b(255,0,0))
Thx!
推荐答案
给定一个图像 image
image
中等于 Scalar(255,0,0)
的所有像素,然后将这些像素设置为 Scalar(0,0,0)
。
Given an image image
, we want to find all the pixels in image
that are equal to Scalar(255,0,0)
and then set these pixels to Scalar(0,0,0)
.
-
首先,我们需要获取
mask
如果image中的相应位置等于
Scalar(255,0,...),则
,否则设置为0.这可以通过mask
0)inRange()
函数。
First we need to obtain
mask
, such that a location inmask
is set to 255 if the corresponding location inimage
equalsScalar(255,0,0)
, otherwise it is set to 0. This can be achieved withinRange()
function.
Mat mask;
inRange(image, Scalar(255,0,0), Scalar(255,0,0), mask);
<函数到 image
。
image.setTo(Scalar(0,0,0), mask);
这篇关于C ++ |使用setTo更改cv :: mat中的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!