讲真的,我就不明白为什么这种需求一行代码都找不到,我的搜索词是【EmguCV Replace Color】,有什么错吗???
其实硬要说也不是没有,但是,这种代码算什么???
写这个的你知道循环3000*4000次替换颜色是什么样的体验吗???????
实际上,这只要使用矩阵去做不就可以了吗???
public static Image<Bgr, byte> BackgroundToGreen(Image<Bgr, byte> rgbimage)
{
Image<Bgr, byte> ret = rgbimage;
var image = rgbimage.InRange(new Bgr(190, 190, 190), new Bgr(255, 255, 255));
var mat = rgbimage.Mat;
mat.SetTo(new MCvScalar(200, 237, 204), image);
mat.CopyTo(ret);
return ret;
}
首先,建立一个原图像的副本,然后从原图像找到需要替换的颜色。
InRange干啥用的?http://www.emgu.com/wiki/files/2.0.0.0/html/07eff70b-f81f-6313-98a9-02d508f7c7e0.htm
Checks that image elements lie between two scalars
Return Value
res[i,j] = 255 if inrange, 0 otherwise
此时图像被二值化,需要的颜色被标为黑色,不需要的被标为白色,返回的类型是Image<Gray, byte>
接着我们用原图引出一个矩阵。为什么要用矩阵做呢?还是看文档:(http://www.emgu.com/wiki/index.php/Working_with_Images#Accessing_the_pixels_from_Mat ):
Unlike the Image<,> class, where memory are pre-allocated and fixed, the memory of Mat can be automatically re-allocated by Open CV function calls. We cannot pre-allocate managed memory and assume the same memory are used through the life time of the Mat object. As a result, Mat class do not contains a Data property like the Image<,> class, where the pixels can be access through a managed array. To access the data of the Mat, there are a few possible choices.
OK,我们怎么设置?用SetTo:
Copies scalar value to every selected element of the destination GpuMat: GpuMat(I)=value if mask(I)!=0
上面那个InRange把颜色在区间的改成了255,SetTo自然就把这部分替换成了新的颜色。
返回,完成。
所以为什么没有人写呢????????????????????