C#のOpenCVSharpを用いて画像を回転できるようにしたいのですが
今現在作成したものだとbutton4と5を押すことで回転はできるのですが
回転した際に画像が見切れてしまって180度回転するまでpictureboxに
黒いスペースができてしまいます。
どうしたら治せるでしょうか?
デザインは画像のような感じです
button1でカメラ映像取得、button2で静止画、button3で保存、button4と5で回転です。
デザイン画像
C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Text; 8using System.Threading.Tasks; 9using System.Windows.Forms; 10//以下、OpenCvsharpの使用とMatの変換用として追加 11using OpenCvSharp; 12using OpenCvSharp.Extensions; 13 14namespace ren524 15{ 16 public partial class Form1 : Form 17 { 18 private Mat _flame; 19 public Form1() 20 { 21 InitializeComponent(); 22 } 23 24 25 private void Form1_Load(object sender, EventArgs e) 26 { 27 //PictureBoxのサイズに合わせて表示 28 pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; 29 pictureBox2.SizeMode = PictureBoxSizeMode.Zoom; 30 } 31 32 33 private void button1_Click(object sender, EventArgs e) 34 { 35 //VideoCapture作成 36 using (var capture = new VideoCapture()) 37 { 38 //カメラの起動 39 capture.Open(0); 40 41 if (!capture.IsOpened()) 42 { 43 throw new Exception("capture initialization failed"); 44 } 45 46 //画像取得用のMatを作成 47 _flame = new Mat(); 48 49 while (true) 50 { 51 try 52 { 53 capture.Read(_flame); 54 if (_flame.Empty()) 55 { 56 break; 57 } 58 59 if (_flame.Size().Width > 0) 60 { 61 Process(_flame);//process用 62 63 //Process2(_flame);//process2用 64 65 //PictureBoxに表示 MatをBitMapに変換 66 pictureBox1.Image = BitmapConverter.ToBitmap(_flame); 67 } 68 69 int key = Cv2.WaitKey(); 70 71 if (this.IsDisposed) 72 { 73 break; 74 } 75 } 76 77 catch (Exception) 78 { 79 break; 80 } 81 } 82 83 } 84 } 85 86 private void button2_Click(object sender, EventArgs e) 87 { 88 if (_flame == null) 89 { 90 return; 91 } 92 //PictureBoxに表示 MatをBitMapに変換 93 pictureBox2.Image = BitmapConverter.ToBitmap(_flame); 94 } 95 96 97 /// [画像を保存]ボタンクリック時の処理 98 private void btnSaveImage_Click(object sender, EventArgs e) 99 { 100 pictureBox2.Image.Save(@"C:\Users\B4R04\Desktop\引継ぎ\aaa\test.png"); 101 } 102 103 private int angle = 0; 104 105 private void Process(Mat img) 106 { 107 using (Mat trans = Cv2.GetRotationMatrix2D(new Point2f(img.Width / 2, img.Height / 2), angle, 1)) 108 { 109 Cv2.WarpAffine(img, img, trans, img.Size()); 110 } 111 } 112 113 114 private void button4_Click(object sender, EventArgs e) 115 { 116 angle += 45; 117 } 118 private void button5_Click(object sender, EventArgs e) 119 { 120 angle -= 45; 121 } 122 123 } 124}
できるようにする
あなたの回答
tips
プレビュー