//private void CountBlobs(WriteableBitmap writableBitmap) private void CountBlobs(WriteableBitmap writeableBitmap) { Mat imgIR = writeableBitmap.ToMat();// CV_16UC1 imgIR.ConvertTo(imgIR, MatType.CV_8UC1, 1.0 / 256.0); Mat imgIRbin = new Mat(imgIR.Rows, imgIR.Cols, MatType.CV_8UC1); Cv2.Threshold(imgIR, imgIRbin, 225, 255, ThresholdTypes.Binary); //imgIR.SaveImage("D:/imgIR.png"); CvBlobs blobs = new CvBlobs(imgIRbin); blobs.FilterByArea(30, 2000); //label_sample.Content = blobs.Count().ToString(); // Canvasに追加 canvas_blob.Children.Clear(); list_arr_index.Clear(); //label_sample.Content = blobs.Count.ToString(); if (blobs.Count > 0) { foreach (KeyValuePair <int, CvBlob> item in blobs) { int labelValue = item.Key; CvBlob blob = item.Value; Rectangle blob_rect = new Rectangle { Width = blob.Rect.Width, Height = blob.Rect.Height, Stroke = Brushes.Red, StrokeThickness = 2 }; canvas_blob.Children.Add(blob_rect); Canvas.SetLeft(blob_rect, blob.Rect.Left); Canvas.SetTop(blob_rect, blob.Rect.Top); } // blobsから各blobのindexを取り出す//////////////////// // blobsからLabelsに変換 LabelData labelBlobs = blobs.Labels; // Labelsを1dデータに変換 int[] label_blobs_vector = new int[labelBlobs.Rows * labelBlobs.Cols]; int ii = 0; //for (int i_col = 0; i_col< labelBlobs.Cols; i_col++) for (int i_row = 0; i_row < labelBlobs.Rows; i_row++) { //for (int i_row = 0; i_row<labelBlobs.Rows;i_row++) for (int i_col = 0; i_col < labelBlobs.Cols; i_col++) { label_blobs_vector[ii] = labelBlobs[i_row, i_col]; ii += 1; } } // // Labelsからblob.Valueに一致するindexの配列を作成 // list_arr_indexに格納する // int count_blobs = blobs.Count; //label_sample.Content = list_arr_index.Count().ToString(); foreach (KeyValuePair <int, CvBlob> item in blobs) { int count_blobs = blobs.Count(); int labelvalue = item.Key; // Labelsからlabelvalueに一致するindex配列を作成 int area_blob = item.Value.Area;// int[] arr_idx_label = new int[area_blob]; ii = 0; for (int i_lab = 0; i_lab < label_blobs_vector.Length; i_lab++) { if (label_blobs_vector[i_lab] == labelvalue) { arr_idx_label[ii] = i_lab; ii += 1; } } //int[] arr_idx_label = label_blobs_vector.FindIndex<int>(label => label == labelvalue); list_arr_index.Add(arr_idx_label); } //label_sample.Content = list_arr_index.Count().ToString(); Console.WriteLine("hoge");//ブレイクポイント用 } }