質問編集履歴
1
解決方法を追記した
test
CHANGED
File without changes
|
test
CHANGED
@@ -283,3 +283,227 @@
|
|
283
283
|
|
284
284
|
|
285
285
|
お手数ですが、教えていただければ幸いです。
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
#追記・以下のとおり解決しました。
|
290
|
+
|
291
|
+
まず、コメントで教えていただいたとおり、OpenCvSharp4.Windows をインストールしました。
|
292
|
+
|
293
|
+
![OpenCvSharp4.Windows](3095019b5ef4f24bc2c2dd61602d808d.png)
|
294
|
+
|
295
|
+
次に、コードを次のように変更しました。
|
296
|
+
|
297
|
+
```C#
|
298
|
+
|
299
|
+
using OpenCvSharp;
|
300
|
+
|
301
|
+
using OpenCvSharp.Extensions;
|
302
|
+
|
303
|
+
using System.Drawing;
|
304
|
+
|
305
|
+
using System.Drawing.Drawing2D;
|
306
|
+
|
307
|
+
using System.Windows.Forms;
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
namespace test21
|
312
|
+
|
313
|
+
{
|
314
|
+
|
315
|
+
public partial class Form1 : Form
|
316
|
+
|
317
|
+
{
|
318
|
+
|
319
|
+
public Form1()
|
320
|
+
|
321
|
+
{
|
322
|
+
|
323
|
+
InitializeComponent();
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
this.Size = new System.Drawing.Size(900, 750);
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
System.Drawing.Point px1 = new System.Drawing.Point(95, 95);
|
332
|
+
|
333
|
+
System.Drawing.Point px2 = new System.Drawing.Point(360, 32);
|
334
|
+
|
335
|
+
System.Drawing.Point px3 = new System.Drawing.Point(300, 240);
|
336
|
+
|
337
|
+
System.Drawing.Point px4 = new System.Drawing.Point(50, 200);
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
Image img = global::test21.Properties.Resources._400x300;
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
// 元の画像を表示
|
346
|
+
|
347
|
+
PictureBox p0 = new PictureBox();
|
348
|
+
|
349
|
+
p0.Image = img;
|
350
|
+
|
351
|
+
p0.Location = new System.Drawing.Point(30, 30);
|
352
|
+
|
353
|
+
p0.Size = new System.Drawing.Size(400, 300);
|
354
|
+
|
355
|
+
this.Controls.Add(p0);
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
// 元の画像に四角形を表示
|
360
|
+
|
361
|
+
PictureBox p1 = new PictureBox();
|
362
|
+
|
363
|
+
Bitmap bt1 = new Bitmap(400, 300);
|
364
|
+
|
365
|
+
Graphics gp1 = Graphics.FromImage(bt1);
|
366
|
+
|
367
|
+
gp1.DrawImage(img, 0, 0, 400, 300);
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
Pen skyBluePen = new Pen(Brushes.DeepSkyBlue);
|
372
|
+
|
373
|
+
skyBluePen.Width = 4.0F;
|
374
|
+
|
375
|
+
|
376
|
+
|
377
|
+
gp1.DrawLine(skyBluePen, px1, px2);
|
378
|
+
|
379
|
+
gp1.DrawLine(skyBluePen, px2, px3);
|
380
|
+
|
381
|
+
gp1.DrawLine(skyBluePen, px3, px4);
|
382
|
+
|
383
|
+
gp1.DrawLine(skyBluePen, px4, px1);
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
p1.Image = bt1;
|
388
|
+
|
389
|
+
p1.Location = new System.Drawing.Point(30, 360);
|
390
|
+
|
391
|
+
p1.Size = new System.Drawing.Size(400, 300);
|
392
|
+
|
393
|
+
this.Controls.Add(p1);
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
// 四角形で切り取って表示
|
398
|
+
|
399
|
+
System.Drawing.Point[] p2pt = { px1, px2, px3, px4 };
|
400
|
+
|
401
|
+
GraphicsPath p2path = new GraphicsPath();
|
402
|
+
|
403
|
+
p2path.AddPolygon(p2pt);
|
404
|
+
|
405
|
+
Region p2region = new Region(p2path);
|
406
|
+
|
407
|
+
Bitmap p2btm = new Bitmap(400, 300);
|
408
|
+
|
409
|
+
Graphics gp2 = Graphics.FromImage(p2btm);
|
410
|
+
|
411
|
+
gp2.Clip = p2region;
|
412
|
+
|
413
|
+
gp2.DrawImage(img, gp1.VisibleClipBounds);
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
PictureBox p2 = new PictureBox();
|
418
|
+
|
419
|
+
p2.Image = p2btm;
|
420
|
+
|
421
|
+
p2.Location = new System.Drawing.Point(450, 30);
|
422
|
+
|
423
|
+
p2.Size = new System.Drawing.Size(400, 300);
|
424
|
+
|
425
|
+
this.Controls.Add(p2);
|
426
|
+
|
427
|
+
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
// p2の四角形を引き伸ばして表示する
|
432
|
+
|
433
|
+
Mat src_img = BitmapConverter.ToMat((Bitmap)img);
|
434
|
+
|
435
|
+
Mat dst_img = src_img;
|
436
|
+
|
437
|
+
|
438
|
+
|
439
|
+
// 四角形の変換前と変換後の対応する頂点をそれぞれセットする
|
440
|
+
|
441
|
+
Point2f[] src_pt = new Point2f[4];
|
442
|
+
|
443
|
+
src_pt[0] = new Point2f(px1.X, px1.Y);
|
444
|
+
|
445
|
+
src_pt[1] = new Point2f(px2.X, px2.Y);
|
446
|
+
|
447
|
+
src_pt[2] = new Point2f(px3.X, px3.Y);
|
448
|
+
|
449
|
+
src_pt[3] = new Point2f(px4.X, px4.Y);
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
Point2f[] dst_pt = new Point2f[4];
|
454
|
+
|
455
|
+
dst_pt[0] = new Point2f(0, 0);
|
456
|
+
|
457
|
+
dst_pt[1] = new Point2f(399, 0);
|
458
|
+
|
459
|
+
dst_pt[2] = new Point2f(399, 299);
|
460
|
+
|
461
|
+
dst_pt[3] = new Point2f(0, 299);
|
462
|
+
|
463
|
+
|
464
|
+
|
465
|
+
Mat map_matrix = Cv2.GetPerspectiveTransform(src_pt, dst_pt);
|
466
|
+
|
467
|
+
|
468
|
+
|
469
|
+
// 指定された透視投影変換行列により,cvWarpPerspectiveを用いて画像を変換させる
|
470
|
+
|
471
|
+
OpenCvSharp.Size mysize = new OpenCvSharp.Size(400, 300);
|
472
|
+
|
473
|
+
InterpolationFlags OIFLiner = InterpolationFlags.Linear;
|
474
|
+
|
475
|
+
BorderTypes OBTDefault = BorderTypes.Default;
|
476
|
+
|
477
|
+
Cv2.WarpPerspective(src_img, dst_img, map_matrix, mysize, OIFLiner, OBTDefault);
|
478
|
+
|
479
|
+
|
480
|
+
|
481
|
+
// 結果を表示する
|
482
|
+
|
483
|
+
PictureBox p3 = new PictureBox();
|
484
|
+
|
485
|
+
p3.Image = dst_img.ToBitmap();
|
486
|
+
|
487
|
+
p3.Location = new System.Drawing.Point(450, 360);
|
488
|
+
|
489
|
+
p3.Size = new System.Drawing.Size(400, 300);
|
490
|
+
|
491
|
+
this.Controls.Add(p3);
|
492
|
+
|
493
|
+
}
|
494
|
+
|
495
|
+
}
|
496
|
+
|
497
|
+
}
|
498
|
+
|
499
|
+
```
|
500
|
+
|
501
|
+
|
502
|
+
|
503
|
+
これにより、正常に射影変換ができました。
|
504
|
+
|
505
|
+
![画像の一部を拡大](c551f21408334439da4b842b54358fa3.png)
|
506
|
+
|
507
|
+
|
508
|
+
|
509
|
+
ありがとうございました。
|