teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

別のやり方を試した際の問題点

2019/11/27 06:47

投稿

hsgru3
hsgru3

スコア22

title CHANGED
File without changes
body CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  ### 該当のソースコード
8
8
 
9
- ソースコードの一部となりますがボタンをクリックするとファイル選択ダイアログが開かれ選択した画像をpicturebox1に表示し、画像処理したものをpicturebox2に表示させようとしています。
9
+ 下記のコードボタンをクリックするとファイル選択ダイアログが開かれ選択した画像をpicturebox1に表示し、画像処理したものをpicturebox2に表示させようとしています。
10
10
 
11
11
  ```C++
12
12
  private: System::Void button_Click(System::Object^ sender, System::EventArgs^ e) {
@@ -37,4 +37,55 @@
37
37
  ### 補足情報(FW/ツールのバージョンなど)
38
38
 
39
39
  Visual studio community 2015
40
- OpenCV 3.4.0
40
+ OpenCV 3.4.0
41
+
42
+ ###追記
43
+
44
+ 下記のコードはBitmapをMatに変換するのではなくファイル選択で選んだパス(dlg->FileName)をSystem::String ^型からstd::stringに変換してimreadで読み込ませています。そのあとimreadで読み込んだ画像を画像処理してからpicureBox2に表示させようとしています。
45
+ ここで問題なのですがimshowで表示させた時点で上下左右反転しdrawImage関数でpictureBox2に表示させると画像が崩れてしまいます。
46
+ 申し訳ございませんがヒントや参考になりそうなサイト等でも教えていただけると幸いです。
47
+
48
+ ```C++
49
+ private: System::Void button_Click(System::Object^ sender, System::EventArgs^ e) {
50
+ //ファイルを開くダイアログの作成
51
+ OpenFileDialog^ dlg = gcnew OpenFileDialog;
52
+ //ファイルフィルタ
53
+ dlg->Filter = "画像ファイル(*.bmp,*.jpg,*.png,*.tif,*.ico)|*.bmp;*.jpg;*.png;*.tif;*.ico";
54
+ //ダイアログの表示 (Cancelボタンがクリックされた場合は何もしない)
55
+ if (dlg->ShowDialog() == System::Windows::Forms::DialogResult::Cancel) return;
56
+ //ビットマップファイルから、元画像Bitmapを作成
57
+ Bitmap^ bmp1 = gcnew Bitmap(dlg->FileName);
58
+ //picturebox1に表示
59
+ pictureBox1->Image = bmp1;
60
+
61
+     //ファイル選択ダイアログで選択したファイルのパス(dlg->FileName(System::String ^型))をstd::stringに変換
62
+ string imgfl = msclr::interop::marshal_as<std::string>(dlg->FileName);
63
+ //imreadでよみこむ
64
+      Mat img = imread(imgfl);
65
+      --------------------------
66
+       OpenCVを用いて画像処理
67
+ --------------------------
68
+ //picturebox2に画像処理したものを表示
69
+ drawImage(pictureBox2, img);
70
+ }
71
+ //描画
72
+ private: System::Void drawImage(PictureBox^ pic, Mat mat) {
73
+
74
+ if ((pic->Image == nullptr) || (pic->Width != mat.cols)
75
+ || (pic->Height != mat.rows)) {
76
+ pic->Width = mat.cols;
77
+ pic->Height = mat.rows;
78
+ Bitmap^ bmpPicBox = gcnew Bitmap(pic->Width, pic->Height);
79
+ pic->Image = bmpPicBox;
80
+ }
81
+ Graphics^g = Graphics::FromImage(pic->Image);
82
+ Bitmap^ bmp = gcnew Bitmap(mat.cols, mat.rows, mat.step,
83
+ System::Drawing::Imaging::PixelFormat::Format32bppRgb, IntPtr(mat.data));
84
+ g->DrawImage(bmp, 0, 0, mat.cols, mat.rows);
85
+ pic->Refresh();
86
+ delete g;
87
+ }
88
+ ```
89
+ 左からBitmapで読み込んでpictureBox1に出力したもの、Matで読み込んでpictureBox2に表示させたもの、Matで読み込んでimshowで表示させたものです。
90
+ まずimreadで読み込まれた際になぜか上下左右が反転してしまい、pictureBox2に出力しようとすると画像が崩れてしまいます。
91
+ ![![イメージ説明](817b1fab7166295c18dd813cf3cd7685.png)](f71d220f54b286edb664607c017fb12a.png)

1

2019/11/27 06:47

投稿

hsgru3
hsgru3

スコア22

title CHANGED
File without changes
body CHANGED
File without changes