OpenCVSharp对图像进行颜色分割
作者:互联网
使用OpencvSharp的InRange函数对图像进行RGB颜色的分割。
1 using System; 2 using OpenCvSharp; 3 using OpenCvSharp.Extensions; 4 using OpenCvSharp.XFeatures2D; 5 using static OpenCvSharp.Cv2; 6 namespace Mycv 7 { 8 public class cvGO 9 { 10 /// <summary> 11 /// 分割颜色得到面积占比的大小 12 /// </summary> 13 /// <param name="srcbmp">原图</param> 14 /// <param name="dstbmp">结果图</param> 15 /// <param name="value_Low">最低值RGB(0-255) ([0]:R,[1]:G,[2]:B)这里用一个长度为3的数组存放最低RGB值</param> 16 /// <param name="value_High">最高值RGB(0-255)([0]:R,[1]:G,[2]:B)这里用一个长度为3的数组存放最高RGB值 </param> 17 public float Color_Inrange(Bitmap srcbmp, out Bitmap dstbmp, int[] value_Low, int[] value_High) 18 { 19 //bitmap转mat 20 using (Mat src = BitmapConverter.ToMat(srcbmp)) 21 using (Mat dst = new Mat()) 22 { 23 //根据传进来的2个数组新建两个Scalar 24 Scalar cvL = new Scalar(value_Low[0], value_Low[1], value_Low[2]); 25 Scalar cvH = new Scalar(value_High[0], value_High[1], value_High[2]); 26 27 //这一步就是颜色分割 28 InRange(src, cvL, cvH, dst); 29 //Inrange的时候已经给dst赋值了,这里是mat转bitmap 30 dstbmp = dst.ToBitmap(); 31 //用CountNonZero来得到面积,然后再计算占比并返回这个值 32 return (CountNonZero(dst) / (src.Width * src.Height * 1.0f) * 100); 33 } 34 } 35 } 36 }
标签:src,分割,dst,RGB,value,Scalar,OpenCVSharp,图像,using 来源: https://www.cnblogs.com/ybqjymy/p/12170836.html