// HSV から RGB へ変換する public static Color HSV2RGB(ColorHSV hsv) { Color result = Color.FromArgb(0, 0, 0); if (hsv == null) { return(result); } float h = hsv.H; float s = hsv.S; float v = hsv.V; float r = v; float g = v; float b = v; if (s > 0f) { h *= 6f; int i = (int)h; float f = h - (float)i; switch (i) { default: case 0: g *= 1f - s * (1f - f); b *= 1f - s; break; case 1: r *= 1f - s * f; b *= 1f - s; break; case 2: r *= 1f - s; b *= 1f - s * (1f - f); break; case 3: r *= 1f - s; g *= 1f - s * f; break; case 4: r *= 1f - s * (1f - f); g *= 1f - s; break; case 5: g *= 1f - s; b *= 1f - s * f; break; } } r *= 255f; g *= 255f; b *= 255f; result = Color.FromArgb((int)r, (int)g, (int)b); return(result); }
public static bool CheckColor(NAME name) { State.SetCamProp(); General.cam.ResetFlag();//カメラのフラグを初期化 リトライ時にフラグが初期化できてないとだめ //例 NGリトライ時は、General.cam.FlagFrame = trueになっていてNGフレーム表示の無限ループにいる int X = 0; int Y = 0; int HueMax = 0; int HueMin = 0; var ListH = new List <int>(); //var ListS = new List<int>(); //var ListV = new List<int>(); int side = (int)Math.Sqrt(TEST_FRAME);//検査枠の1辺の長さ try { switch (name) { case NAME.LED1: X = Int32.Parse(State.camProp.LED1.Split('/').ToArray()[0]); Y = Int32.Parse(State.camProp.LED1.Split('/').ToArray()[1]); HueMax = State.TestSpec.OrangeHueMax; HueMin = State.TestSpec.OrangeHueMin; break; case NAME.LED2: X = Int32.Parse(State.camProp.LED2.Split('/').ToArray()[0]); Y = Int32.Parse(State.camProp.LED2.Split('/').ToArray()[1]); HueMax = State.TestSpec.GreenHueMax; HueMin = State.TestSpec.GreenHueMin; break; case NAME.LED3: X = Int32.Parse(State.camProp.LED3.Split('/').ToArray()[0]); Y = Int32.Parse(State.camProp.LED3.Split('/').ToArray()[1]); HueMax = State.TestSpec.RedHueMax; HueMin = State.TestSpec.RedHueMin; break; } //cam0の画像を取得する処理 General.cam.FlagTestPic = true; while (General.cam.FlagTestPic) { } source = General.cam.imageForTest; //デバッグ用コード(下記コメントを外すと画像を保存します) //source.SaveImage(@"C:\OS303\ColorPic.bmp"); using (IplImage hsv = new IplImage(640, 360, BitDepth.U8, 3)) // グレースケール画像格納用の変数 { //RGBからHSVに変換 Cv.CvtColor(source, hsv, ColorConversion.BgrToHsv); OpenCvSharp.CPlusPlus.Mat mat = new OpenCvSharp.CPlusPlus.Mat(hsv, true); foreach (var i in Enumerable.Range(0, side)) { foreach (var j in Enumerable.Range(0, side)) { var re = mat.At <OpenCvSharp.CPlusPlus.Vec3b>((int)Y - (side / 2) + i, (int)X - (side / 2) + j); if (re[1] == 255 && re[2] == 255) { ListH.Add(re[0]); } } } switch (name) { case NAME.LED1: //黄色 H_Ave = ListH.Min(); break; case NAME.LED2: //緑色 H_Ave = ListH.Max(); break; case NAME.LED3: //赤色 H_Ave = ListH.Min(); break; } return(H_Ave >= HueMin && H_Ave <= HueMax); } } finally { string hsvValue = H_Ave.ToString("F0"); ColorHSV hsv = new ColorHSV((float)Test_Led.H_Ave / 180, 1, 1); var rgb = ColorConv.HSV2RGB(hsv); var color = new SolidColorBrush(Color.FromRgb(rgb.R, rgb.G, rgb.B)); color.Freeze();//これ重要!!! switch (name) { case NAME.LED1: State.VmTestResults.HueLED1 = hsvValue; State.VmTestResults.ColorLED1 = color; break; case NAME.LED2: State.VmTestResults.HueLED2 = hsvValue; State.VmTestResults.ColorLED2 = color; break; case NAME.LED3: State.VmTestResults.HueLED3 = hsvValue; State.VmTestResults.ColorLED3 = color; break; } } }