diff --git a/Tongue extraction_cropresizemethod/Tongue extraction/Form1.cs b/Tongue extraction_cropresizemethod/Tongue extraction/Form1.cs index 55c64d5..c504285 100644 --- a/Tongue extraction_cropresizemethod/Tongue extraction/Form1.cs +++ b/Tongue extraction_cropresizemethod/Tongue extraction/Form1.cs @@ -580,220 +580,222 @@ continue; } - - mat_drawBox.SaveImage(basepath + "\\detection" + imageFile); - bitmap_bitch = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat_drawBox); - Invoke((MethodInvoker)delegate + // 舌が検出された場合 + else { - pictureBox_detection.Image = bitmap_bitch; - pictureBox_detection.Refresh(); - }); - label1.BackColor = Color.White; - label2.BackColor = Color.Red; - // 検出された場合の処理の続き - for (int i = P1.Y; i < P2.Y; i++) - { - for (int j = P1.X; j < P2.X; j++) + mat_drawBox.SaveImage(basepath + "\\detection" + imageFile); + bitmap_bitch = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat_drawBox); + Invoke((MethodInvoker)delegate { - Vec3b pix = mat_input.At(i, j); - mat_cropped.Set(i, j, pix); - } - } - mat_cropped.SaveImage(basepath + "\\cropped" + imageFile); - OpenCvSharp.Size size_roi = new OpenCvSharp.Size(); - size_roi.Height = rectangle.Height; - size_roi.Width = rectangle.Width; - roi = new Rect(P1, size_roi); - mat_roisize = mat_input.Clone(roi); - Cv2.Resize(mat_roisize, mat_roi, mat_roi256.Size()); - mat_roi.SaveImage(basepath + "\\cropresized" + imageFile); - bitmap_bitch = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat_roi); - Invoke((MethodInvoker)delegate - { - pictureBox_cropResized.Image = bitmap_bitch; - pictureBox_cropResized.Refresh(); - }); - - label2.BackColor = Color.White; - label3.BackColor = Color.Red; - byte_inputSegmentation = Bitmap2Byte(bitmap_bitch); - Thread.Sleep(1000); - modelFile = DownloadDefaultModel(basepath); - using (var graph = new TFGraph()) - { - var model = File.ReadAllBytes(modelFile); - graph.Import(model, ""); - - using (var session = new TFSession(graph)) + pictureBox_detection.Image = bitmap_bitch; + pictureBox_detection.Refresh(); + }); + label1.BackColor = Color.White; + label2.BackColor = Color.Red; + // 検出された場合の処理の続き + for (int i = P1.Y; i < P2.Y; i++) { - var tensor = ImageUtil2.CreateTensorFromImageFile(byte_inputSegmentation); - var runner = session.GetRunner(); - runner - .AddInput(graph["generator/input_image"][0], tensor) - .Fetch(graph["generator/prediction"][0]); - - //.AddInput(graph["input_image"][0], tensor) - //.Fetch(graph["generator1/decoder_1/Tanh"][0]); - - var output = runner.Run(); - float[,,,] resultfloat = (float[,,,])output[0].GetValue(jagged: false); - - for (int p = 0; p < 256; p++) + for (int j = P1.X; j < P2.X; j++) { - for (int q = 0; q < 256; q++) + Vec3b pix = mat_input.At(i, j); + mat_cropped.Set(i, j, pix); + } + } + mat_cropped.SaveImage(basepath + "\\cropped" + imageFile); + OpenCvSharp.Size size_roi = new OpenCvSharp.Size(); + size_roi.Height = rectangle.Height; + size_roi.Width = rectangle.Width; + roi = new Rect(P1, size_roi); + mat_roisize = mat_input.Clone(roi); + Cv2.Resize(mat_roisize, mat_roi, mat_roi256.Size()); + mat_roi.SaveImage(basepath + "\\cropresized" + imageFile); + bitmap_bitch = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat_roi); + Invoke((MethodInvoker)delegate + { + pictureBox_cropResized.Image = bitmap_bitch; + pictureBox_cropResized.Refresh(); + }); + + label2.BackColor = Color.White; + label3.BackColor = Color.Red; + byte_inputSegmentation = Bitmap2Byte(bitmap_bitch); + Thread.Sleep(1000); + modelFile = DownloadDefaultModel(basepath); + using (var graph = new TFGraph()) + { + var model = File.ReadAllBytes(modelFile); + graph.Import(model, ""); + + using (var session = new TFSession(graph)) + { + var tensor = ImageUtil2.CreateTensorFromImageFile(byte_inputSegmentation); + var runner = session.GetRunner(); + runner + .AddInput(graph["generator/input_image"][0], tensor) + .Fetch(graph["generator/prediction"][0]); + + //.AddInput(graph["input_image"][0], tensor) + //.Fetch(graph["generator1/decoder_1/Tanh"][0]); + + var output = runner.Run(); + float[,,,] resultfloat = (float[,,,])output[0].GetValue(jagged: false); + + for (int p = 0; p < 256; p++) { - float check = resultfloat[0, p, q, 0]; - if (check < 0) + for (int q = 0; q < 256; q++) { - mask[ii] = 0; + float check = resultfloat[0, p, q, 0]; + if (check < 0) + { + mask[ii] = 0; + } + else + { + mask[ii] = 255; + } + ii++; } - else - { - mask[ii] = 255; - } - ii++; } } } - } - Thread.Sleep(1000); - bitmap_bitch = ToGrayBitmap(mask, 256, 256); - mat_output = OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap_bitch); - bitmap_bitch = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat_output); - Invoke((MethodInvoker)delegate - { - pictureBox_output.Image = bitmap_bitch; - pictureBox_output.Refresh(); - }); - - label3.BackColor = Color.White; - label4.BackColor = Color.Red; - mat_output.SaveImage(basepath + "\\output256" + imageFile); - try - { - RemoveSmallRegion(basepath + "\\output256" + imageFile, basepath + "\\output_changed1" + imageFile, 500, 1, 1); - RemoveSmallRegion(basepath + "\\output_changed1" + imageFile, basepath + "\\output_changed2" + imageFile, 500, 0, 0); - } - catch - { - MessageBox.Show("Error: Unable to reprocess! Please check is there [RemoveSmallRegionDLL.dll] file in floder?", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - break; - } - mat_outputSRG = new Mat(basepath + "\\output_changed2" + imageFile, ImreadModes.GrayScale); - bitmap_bitch = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat_outputSRG); - Invoke((MethodInvoker)delegate - { - pictureBox_outputSRG.Image = bitmap_bitch; - pictureBox_outputSRG.Refresh(); - }); - - label4.BackColor = Color.White; - label5.BackColor = Color.Red; - Cv2.Resize(mat_outputSRG, mat_outputChanged, mat_roisize.Size()); - mat_outputChanged.SaveImage(basepath + "\\output_resized" + imageFile); - mat_mask = new Mat(mat_input.Size(), MatType.CV_8UC1, 1); - for (int i = P1.Y; i < P2.Y; i++) - { - for (int j = P1.X; j < P2.X; j++) + Thread.Sleep(1000); + bitmap_bitch = ToGrayBitmap(mask, 256, 256); + mat_output = OpenCvSharp.Extensions.BitmapConverter.ToMat(bitmap_bitch); + bitmap_bitch = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat_output); + Invoke((MethodInvoker)delegate { - int pix = mat_outputChanged.At(mmp, pmm); - mat_mask.Set(i, j, pix); - pmm++; - } - mmp++; - pmm = 0; - } - mmp = 0; - Cv2.Resize(mat_mask, mat_mask, mat_input.Size()); - mat_mask.SaveImage(basepath + "\\mask" + imageFile); - try - { - RemoveSmallRegion(basepath + "\\mask" + imageFile, basepath + "\\mask_changed1" + imageFile, 500, 1, 1); - RemoveSmallRegion(basepath + "\\mask_changed1" + imageFile, basepath + "\\mask_changed2" + imageFile, 500, 0, 0); - } - catch - { - MessageBox.Show("Error: Unable to reprocess! Please check is there [RemoveSmallRegionDLL.dll] file in floder?", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); - break; - } + pictureBox_output.Image = bitmap_bitch; + pictureBox_output.Refresh(); + }); - mat_maskSRG = new Mat(basepath + "\\mask_changed2" + imageFile, ImreadModes.GrayScale); - Cv2.Threshold(mat_maskSRG, mat_maskSRG, 128, 255, ThresholdTypes.Binary); - bitmap_bitch = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat_maskSRG); - if(pictureBox_maskSRG.InvokeRequired) - { - pictureBox_maskSRG.Invoke((MethodInvoker)delegate + label3.BackColor = Color.White; + label4.BackColor = Color.Red; + mat_output.SaveImage(basepath + "\\output256" + imageFile); + try + { + RemoveSmallRegion(basepath + "\\output256" + imageFile, basepath + "\\output_changed1" + imageFile, 500, 1, 1); + RemoveSmallRegion(basepath + "\\output_changed1" + imageFile, basepath + "\\output_changed2" + imageFile, 500, 0, 0); + } + catch + { + MessageBox.Show("Error: Unable to reprocess! Please check is there [RemoveSmallRegionDLL.dll] file in floder?", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + break; + } + mat_outputSRG = new Mat(basepath + "\\output_changed2" + imageFile, ImreadModes.GrayScale); + bitmap_bitch = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat_outputSRG); + Invoke((MethodInvoker)delegate + { + pictureBox_outputSRG.Image = bitmap_bitch; + pictureBox_outputSRG.Refresh(); + }); + + label4.BackColor = Color.White; + label5.BackColor = Color.Red; + Cv2.Resize(mat_outputSRG, mat_outputChanged, mat_roisize.Size()); + mat_outputChanged.SaveImage(basepath + "\\output_resized" + imageFile); + mat_mask = new Mat(mat_input.Size(), MatType.CV_8UC1, 1); + for (int i = P1.Y; i < P2.Y; i++) + { + for (int j = P1.X; j < P2.X; j++) + { + int pix = mat_outputChanged.At(mmp, pmm); + mat_mask.Set(i, j, pix); + pmm++; + } + mmp++; + pmm = 0; + } + mmp = 0; + Cv2.Resize(mat_mask, mat_mask, mat_input.Size()); + mat_mask.SaveImage(basepath + "\\mask" + imageFile); + try + { + RemoveSmallRegion(basepath + "\\mask" + imageFile, basepath + "\\mask_changed1" + imageFile, 500, 1, 1); + RemoveSmallRegion(basepath + "\\mask_changed1" + imageFile, basepath + "\\mask_changed2" + imageFile, 500, 0, 0); + } + catch + { + MessageBox.Show("Error: Unable to reprocess! Please check is there [RemoveSmallRegionDLL.dll] file in floder?", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + break; + } + + mat_maskSRG = new Mat(basepath + "\\mask_changed2" + imageFile, ImreadModes.GrayScale); + Cv2.Threshold(mat_maskSRG, mat_maskSRG, 128, 255, ThresholdTypes.Binary); + bitmap_bitch = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat_maskSRG); + if (pictureBox_maskSRG.InvokeRequired) + { + pictureBox_maskSRG.Invoke((MethodInvoker)delegate + { + pictureBox_maskSRG.Image = bitmap_bitch; + pictureBox_maskSRG.Refresh(); + }); + } + else { pictureBox_maskSRG.Image = bitmap_bitch; pictureBox_maskSRG.Refresh(); - }); - } - else - { - pictureBox_maskSRG.Image = bitmap_bitch; - pictureBox_maskSRG.Refresh(); - } - + } - label5.BackColor = Color.White; - label6.BackColor = Color.Red; - mat_extraction = mat_input.Clone(); - int areaCount = 0; - for (int i = 0; i < mat_input.Height; i++) - { - for (int j = 0; j < mat_input.Width; j++) + label5.BackColor = Color.White; + label6.BackColor = Color.Red; + + mat_extraction = mat_input.Clone(); + int areaCount = 0; + for (int i = 0; i < mat_input.Height; i++) { - Vec3b pix = mat_extraction.At(i, j); - if (mat_maskSRG.At(i, j) == 0) + for (int j = 0; j < mat_input.Width; j++) { - pix[0] = (byte)(255); - pix[1] = (byte)(255); - pix[2] = (byte)(255); - mat_extraction.Set(i, j, pix); - } - else - { - pix[0] = (byte)(mat_extraction.At(i, j).Item0); - pix[1] = (byte)(mat_extraction.At(i, j).Item1); - pix[2] = (byte)(mat_extraction.At(i, j).Item2); - mat_extraction.Set(i, j, pix); - areaCount++; + Vec3b pix = mat_extraction.At(i, j); + if (mat_maskSRG.At(i, j) == 0) + { + pix[0] = (byte)(255); + pix[1] = (byte)(255); + pix[2] = (byte)(255); + mat_extraction.Set(i, j, pix); + } + else + { + pix[0] = (byte)(mat_extraction.At(i, j).Item0); + pix[1] = (byte)(mat_extraction.At(i, j).Item1); + pix[2] = (byte)(mat_extraction.At(i, j).Item2); + mat_extraction.Set(i, j, pix); + areaCount++; + } } } + mat_extraction.SaveImage(basepath + "\\extraction" + imageFile); + bitmap_bitch = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat_extraction); + Invoke((MethodInvoker)delegate + { + pictureBox_extraction.Image = bitmap_bitch; + pictureBox_extraction.Refresh(); + }); + + label6.BackColor = Color.White; + label7.BackColor = Color.Red; + bitmap_bitch = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat_input); + Invoke((MethodInvoker)delegate + { + pictureBox_contract.Image = bitmap_bitch; + pictureBox_contract.Refresh(); + }); + + label7.BackColor = Color.White; + label8.BackColor = Color.Red; + time = DateTime.Now.ToLocalTime().ToString(); + File.AppendAllText("Log.txt ", time + " " + imageFile + " Done!\n"); + + + //csvにboundingbox情報を保存 + sw.WriteLine( + imageFile.Substring(1) + "," + + P1.X.ToString() + "," + P1.Y.ToString() + "," + + P2.X.ToString() + "," + P2.Y.ToString() + "," + + Math.Abs(P1.X - P2.X).ToString() + "," + Math.Abs(P1.Y - P2.Y).ToString() + "," + + areaCount.ToString() + ); } - mat_extraction.SaveImage(basepath + "\\extraction" + imageFile); - bitmap_bitch = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat_extraction); - Invoke((MethodInvoker)delegate - { - pictureBox_extraction.Image = bitmap_bitch; - pictureBox_extraction.Refresh(); - }); - - label6.BackColor = Color.White; - label7.BackColor = Color.Red; - bitmap_bitch = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(mat_input); - Invoke((MethodInvoker)delegate - { - pictureBox_contract.Image = bitmap_bitch; - pictureBox_contract.Refresh(); - }); - - label7.BackColor = Color.White; - label8.BackColor = Color.Red; - time = DateTime.Now.ToLocalTime().ToString(); - File.AppendAllText("Log.txt ", time + " " + imageFile + " Done!\n"); - - - //csvにboundingbox情報を保存 - sw.WriteLine( - imageFile.Substring(1) + "," - + P1.X.ToString() + "," + P1.Y.ToString() + "," - + P2.X.ToString() + "," + P2.Y.ToString() + "," - + Math.Abs(P1.X - P2.X).ToString() + "," + Math.Abs(P1.Y - P2.Y).ToString() + "," - + areaCount.ToString() - ); - } MessageBox.Show("Finished!");