質問編集履歴

1

ソースコードの掲示

2018/10/16 14:20

投稿

soramama
soramama

スコア10

test CHANGED
File without changes
test CHANGED
@@ -32,7 +32,693 @@
32
32
 
33
33
  ### 該当のソースコード
34
34
 
35
-
35
+ ```ここに言語を入力
36
+
37
+ コード
38
+
39
+ #include "stdafx.h"
40
+
41
+ #include <Windows.h>
42
+
43
+ #include <opencv2/opencv.hpp>
44
+
45
+ #include <opencv2/core.hpp>
46
+
47
+ #include <opencv2/imgproc.hpp>
48
+
49
+ #include <opencv2/highgui.hpp>
50
+
51
+ #include <stdlib.h>
52
+
53
+ #include <iostream>
54
+
55
+ #include "BmpMem.h"
56
+
57
+
58
+
59
+
60
+
61
+
62
+
63
+ #define _CRTBG_MAP_ALLOC
64
+
65
+ #define BMP_SHARE_MEM _T ("BMP_SHARED_MEMORY")
66
+
67
+ #define MEMORY_SIZE 3000 * 2000 * 4
68
+
69
+
70
+
71
+ using namespace std;
72
+
73
+ using namespace cv;
74
+
75
+
76
+
77
+
78
+
79
+ BmpMem::BmpMem()
80
+
81
+ {
82
+
83
+ }
84
+
85
+ BmpMem::~BmpMem()
86
+
87
+ {
88
+
89
+ }
90
+
91
+
92
+
93
+
94
+
95
+ int BmpMem::CreateMapObject(void) {
96
+
97
+ try {
98
+
99
+ // マッピングオブジェクト作成
100
+
101
+ m_hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, MEMORY_SIZE, BMP_SHARE_MEM);
102
+
103
+ if (m_hMapping == NULL) {
104
+
105
+ cout << "マップドファイルオブジェクトの作成に失敗しました" << endl;
106
+
107
+ getchar();
108
+
109
+ return -1;
110
+
111
+ }
112
+
113
+
114
+
115
+ // 共有メモリをマッピング
116
+
117
+ m_lpBuff = (LPSTR)::MapViewOfFile(m_hMapping, FILE_MAP_WRITE, 0, 0, 0);
118
+
119
+ if (m_lpBuff == NULL) {
120
+
121
+ cout << "MapViewOfFileエラー" << endl;
122
+
123
+ getchar();
124
+
125
+ return -1;
126
+
127
+ }
128
+
129
+
130
+
131
+ ::ZeroMemory(m_lpBuff, MEMORY_SIZE);
132
+
133
+ }
134
+
135
+ catch (...) {
136
+
137
+ cout << endl << "CreateMapObject Exception !" << endl;
138
+
139
+ getchar();
140
+
141
+ }
142
+
143
+
144
+
145
+ return 0;
146
+
147
+ }
148
+
149
+
150
+
151
+
152
+
153
+ void BmpMem::OutMem(unsigned long* bmp_img) {
154
+
155
+ try {
156
+
157
+ memcpy(m_lpBuff, bmp_img, (16 + (m_ImageWidth * m_ImageHeight)) * 4);
158
+
159
+ cout << "出力が完了しました" << endl;
160
+
161
+ }
162
+
163
+ catch (...) {
164
+
165
+ cout << endl << "OutMem Exception !" << endl;
166
+
167
+ getchar();
168
+
169
+ }
170
+
171
+ }
172
+
173
+
174
+
175
+
176
+
177
+
178
+
179
+ void BmpMem::OutBmpData(cv::Mat* src) {
180
+
181
+
182
+
183
+ m_ImageWidth = src->rows;
184
+
185
+ m_ImageHeight = src->cols;
186
+
187
+ IplImage IplImg;
188
+
189
+ BITMAPINFO info;
190
+
191
+ BITMAPFILEHEADER bmpHeader;
192
+
193
+
194
+
195
+ try {
196
+
197
+ IplImg = *src;
198
+
199
+
200
+
201
+ HANDLE hHandle;
202
+
203
+ unsigned long* bmpData;
204
+
205
+ hHandle = GetProcessHeap();
206
+
207
+ bmpData = (LPDWORD)HeapAlloc(hHandle, HEAP_ZERO_MEMORY, (16 + (m_ImageWidth * m_ImageHeight)) * 4);
208
+
209
+ cout << "共有メモリに出力します" << endl;
210
+
211
+ iplTobmp(&IplImg, info, bmpHeader, bmpData);
212
+
213
+
214
+
215
+ OutMem(bmpData);
216
+
217
+
218
+
219
+ HeapFree(hHandle, 0, bmpData);
220
+
221
+ cout << "出力が完了しました" << endl;
222
+
223
+ }
224
+
225
+ catch (...) {
226
+
227
+ cout << endl << "OutBmpData Exception !" << endl;
228
+
229
+ getchar();
230
+
231
+ }
232
+
233
+ }
234
+
235
+
236
+
237
+
238
+
239
+ Mat BmpMem::InBmpData() {
240
+
241
+ HANDLE mh_lpBuff = (LPSTR)::OpenFileMapping(FILE_MAP_ALL_ACCESS, false, BMP_SHARE_MEM);
242
+
243
+ // 共有メモリをマッピング
244
+
245
+ LPSTR mp_lpBuff = (LPSTR)::MapViewOfFile(mh_lpBuff, FILE_MAP_WRITE, 0, 0, 0);
246
+
247
+ if (mp_lpBuff == NULL) {
248
+
249
+ cout << "MapViewOfFileエラー" << endl;
250
+
251
+ getchar();
252
+
253
+ }
254
+
255
+ unsigned long bmpInfo[16] = {};
256
+
257
+ memcpy(bmpInfo, mp_lpBuff, 16 * 4);
258
+
259
+ cout << "インフォ部を読み込みました" << endl;
260
+
261
+ int width = bmpInfo[6];
262
+
263
+ int height = bmpInfo[7];
264
+
265
+ cv::Mat image;
266
+
267
+ cv::Mat bmpImage(cvSize(bmpInfo[6], bmpInfo[7]), CV_8UC3, cv::Scalar(0));
268
+
269
+ cout << "Mat宣言完了" << endl;
270
+
271
+ try {
272
+
273
+ unsigned char b, g, r;
274
+
275
+ unsigned long pixel;
276
+
277
+ cout << "変数宣言完了しました" << endl;
278
+
279
+
280
+
281
+ for (int countHeight = 0; countHeight < bmpInfo[7]; countHeight++) {
282
+
283
+ //cout << countHeight << "行目を読み込みます" << endl;
284
+
285
+ for (int countWidth = 0; countWidth < bmpInfo[6]; countWidth++) {
286
+
287
+ CopyMemory(&pixel, (mp_lpBuff + ((16 + countWidth + ((bmpInfo[7] - countHeight - 1) * bmpInfo[6])) * 4)), sizeof(unsigned long));
288
+
289
+ b = pixel;
290
+
291
+ g = pixel >> 8;
292
+
293
+ r = pixel >> 16;
294
+
295
+ bmpImage.at<cv::Vec3b>(countHeight, countWidth)[0] = b;
296
+
297
+ bmpImage.at<cv::Vec3b>(countHeight, countWidth)[1] = g;
298
+
299
+ bmpImage.at<cv::Vec3b>(countHeight, countWidth)[2] = r;
300
+
301
+ }
302
+
303
+ //cout << "読み込み完了しました" << endl;
304
+
305
+ }
306
+
307
+ cout << "読み込み完了しました" << endl;
308
+
309
+ }
310
+
311
+ catch (...) {
312
+
313
+ cout << endl << "CloseMapObject Exception !" << endl;
314
+
315
+ getchar();
316
+
317
+ }
318
+
319
+ return bmpImage;
320
+
321
+ }
322
+
323
+
324
+
325
+
326
+
327
+
328
+
329
+
330
+
331
+
332
+
333
+
334
+
335
+ void BmpMem::CloseMapObject(void)
336
+
337
+ {
338
+
339
+ try {
340
+
341
+ UnmapViewOfFile(m_lpBuff);
342
+
343
+ CloseHandle(m_hMapping);
344
+
345
+ }
346
+
347
+ catch (...) {
348
+
349
+ cout << endl << "OutBmpData Exception !" << endl;
350
+
351
+ getchar();
352
+
353
+ }
354
+
355
+ }
356
+
357
+
358
+
359
+ void BmpMem::iplTobmp(const IplImage* srcIplImage, BITMAPINFO& bmpInfo, BITMAPFILEHEADER& bmpHeader, unsigned long* bmpData) {
360
+
361
+
362
+
363
+ int width = srcIplImage->width;
364
+
365
+ int height = srcIplImage->height;
366
+
367
+
368
+
369
+ // ビットマップのヘッダーを0で初期化
370
+
371
+ ZeroMemory(&bmpHeader, sizeof(bmpHeader));
372
+
373
+ bmpHeader.bfType = 'BM'; // ファイルタイプ
374
+
375
+ bmpHeader.bfSize = (16 + width * height) * 4; // ファイル全体のサイズ
376
+
377
+ bmpHeader.bfReserved1 = 0; // 予約領域 常に0
378
+
379
+ bmpHeader.bfReserved2 = 0; // 予約領域 常に0
380
+
381
+ bmpHeader.bfOffBits = 16; // ファイル先頭から画像データまでのオフセット値
382
+
383
+ // bmpData[0-4]にヘッダ情報を格納
384
+
385
+ bmpData[0] = bmpHeader.bfType;
386
+
387
+ bmpData[1] = bmpHeader.bfSize;
388
+
389
+ bmpData[2] = bmpHeader.bfReserved1;
390
+
391
+ bmpData[3] = bmpHeader.bfReserved2;
392
+
393
+ bmpData[4] = bmpHeader.bfOffBits;
394
+
395
+
396
+
397
+ // ビットマップのインフォを0で初期化
398
+
399
+ ZeroMemory(&bmpInfo, sizeof(bmpInfo));
400
+
401
+ bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); // Info構造体のサイズ 40
402
+
403
+ bmpInfo.bmiHeader.biWidth = width; // 画像の幅[pix]
404
+
405
+ bmpInfo.bmiHeader.biHeight = height; // 画像の高さ[pix]
406
+
407
+ bmpInfo.bmiHeader.biPlanes = 1; // プレーン数 常に1
408
+
409
+ bmpInfo.bmiHeader.biBitCount = 32; // 1画素あたりのビット数
410
+
411
+ bmpInfo.bmiHeader.biCompression = BI_RGB; // 圧縮形式
412
+
413
+ bmpInfo.bmiHeader.biSizeImage = 0; // 画像データのサイズ(byte) BI_RGBは0でも可
414
+
415
+ bmpInfo.bmiHeader.biXPelsPerMeter = 0; // 水平方向1[m]あたりの画素数 0でも可
416
+
417
+ bmpInfo.bmiHeader.biYPelsPerMeter = 0; // 垂直方向1[m]あたりの画素数 0でも可
418
+
419
+ bmpInfo.bmiHeader.biClrUsed = 0; // カラーテーブルの色数 0でも可
420
+
421
+ bmpInfo.bmiHeader.biClrImportant = 0; // 表示に必要なカラーテーブルの色数 0でも可
422
+
423
+ // bmpData[5-15]にインフォ情報を格納
424
+
425
+ bmpData[5] = bmpInfo.bmiHeader.biSize;
426
+
427
+ bmpData[6] = bmpInfo.bmiHeader.biWidth;
428
+
429
+ bmpData[7] = bmpInfo.bmiHeader.biHeight;
430
+
431
+ bmpData[8] = bmpInfo.bmiHeader.biPlanes;
432
+
433
+ bmpData[9] = bmpInfo.bmiHeader.biBitCount;
434
+
435
+ bmpData[10] = bmpInfo.bmiHeader.biCompression;
436
+
437
+ bmpData[11] = bmpInfo.bmiHeader.biSizeImage;
438
+
439
+ bmpData[12] = bmpInfo.bmiHeader.biXPelsPerMeter;
440
+
441
+ bmpData[13] = bmpInfo.bmiHeader.biYPelsPerMeter;
442
+
443
+ bmpData[14] = bmpInfo.bmiHeader.biClrUsed;
444
+
445
+ bmpData[15] = bmpInfo.bmiHeader.biClrImportant;
446
+
447
+ cout << "情報を出力しました" << endl;
448
+
449
+
450
+
451
+ for (int count = 0; count < 16; count++) {
452
+
453
+ cout << count << " : " << bmpData[count] << endl;
454
+
455
+ }
456
+
457
+ unsigned long pixel;
458
+
459
+ unsigned char r, g, b;
460
+
461
+ for (int countHeight = 0; countHeight < height; countHeight++) {
462
+
463
+ for (int countWidth = 0; countWidth < width; countWidth++) {
464
+
465
+ // Brue,Green,Redの画素値を取得する
466
+
467
+ b = srcIplImage->imageData[srcIplImage->widthStep * countHeight + countWidth * 3];
468
+
469
+ g = srcIplImage->imageData[srcIplImage->widthStep * countHeight + countWidth * 3 + 1];
470
+
471
+ r = srcIplImage->imageData[srcIplImage->widthStep * countHeight + countWidth * 3 + 2];
472
+
473
+ //imageBuffer->push_back(b);
474
+
475
+ //imageBuffer->push_back(g);
476
+
477
+ //imageBuffer->push_back(r);
478
+
479
+ // pixelを初期化する
480
+
481
+ pixel = 0x00000000;
482
+
483
+ // rは16bitシフト,gは8bitシフト,bはそのままでpixelに格納
484
+
485
+ pixel = (r << 16) + (g << 8) + b;
486
+
487
+ // pixelをbmpDataに格納
488
+
489
+ bmpData[16 + countWidth + (height - countHeight - 1) * width] = pixel;
490
+
491
+ }
492
+
493
+ //cout << countHeight << "行目の画素データを格納します" << endl;
494
+
495
+ }
496
+
497
+ cout << "画像を出力しました" << endl;
498
+
499
+ }
500
+
501
+
502
+
503
+
504
+
505
+
506
+
507
+
508
+
509
+
510
+
511
+ BmpMem.h
512
+
513
+ #pragma once
514
+
515
+ #include <Windows.h>
516
+
517
+ #include <vector>
518
+
519
+ #include <opencv2/core.hpp>
520
+
521
+ #include <opencv2/imgproc.hpp>
522
+
523
+ #include <opencv2/highgui.hpp>
524
+
525
+
526
+
527
+ class BmpMem
528
+
529
+ {
530
+
531
+ public:
532
+
533
+ BmpMem();
534
+
535
+ ~BmpMem();
536
+
537
+ private:
538
+
539
+ HANDLE m_hMapping;
540
+
541
+ LPSTR m_lpBuff;
542
+
543
+ int m_memSize;
544
+
545
+ int m_ImageWidth;
546
+
547
+ int m_ImageHeight;
548
+
549
+
550
+
551
+
552
+
553
+ // 共有メモリにデータ書き込み
554
+
555
+ void OutMem(unsigned long*);
556
+
557
+ // IplImageからBMP(DIB)へ変換する
558
+
559
+ void iplTobmp(const IplImage* srcIplImage, BITMAPINFO& bmpInfo, BITMAPFILEHEADER& bmpHeader, unsigned long* bmpData);
560
+
561
+
562
+
563
+ public:
564
+
565
+ // 共有メモリの作成
566
+
567
+ int CreateMapObject(void);
568
+
569
+ // ビットマップデータの書き込み
570
+
571
+ void OutBmpData(cv::Mat *src);
572
+
573
+ // ビットマップデータの読み込み
574
+
575
+ //void InBmpData();
576
+
577
+ cv::Mat InBmpData();
578
+
579
+ // 共有メモリオブジェクトの破棄
580
+
581
+ void CloseMapObject(void);
582
+
583
+
584
+
585
+
586
+
587
+
588
+
589
+ };
590
+
591
+
592
+
593
+
594
+
595
+ 読み込み部分
596
+
597
+ #include "stdafx.h"
598
+
599
+ #include <conio.h>
600
+
601
+ #include <opencv2/core.hpp>
602
+
603
+ #include <opencv2/opencv.hpp>
604
+
605
+ #include <opencv2/imgproc.hpp>
606
+
607
+ #include <opencv2/highgui.hpp>
608
+
609
+ #include <iostream>
610
+
611
+ #include "BmpMem.h"
612
+
613
+
614
+
615
+ using namespace std;
616
+
617
+ using namespace cv;
618
+
619
+
620
+
621
+
622
+
623
+ int _tmain(int argc, _TCHAR* argv[])
624
+
625
+ {
626
+
627
+ int key;
628
+
629
+ Mat img;
630
+
631
+ Mat returnImage;
632
+
633
+
634
+
635
+
636
+
637
+ cout << "共有メモリのオープンに成功しました" << endl;
638
+
639
+ img = imread("D:\デスクトップ\Project1\Project1\954704.png",1);
640
+
641
+
642
+
643
+ if (img.empty()) {
644
+
645
+ cout << "画像は読み込まれませんでした\nプログラムを終了します" << endl;
646
+
647
+ return -1;
648
+
649
+ }
650
+
651
+ cout << "画像のオープンに成功しました" << endl;
652
+
653
+ BmpMem *pBmp = new BmpMem;
654
+
655
+ if (pBmp->CreateMapObject() == -1) {
656
+
657
+ cout << "共有メモリ(画像)のオープンに失敗しました。" << endl;
658
+
659
+ getchar();
660
+
661
+ return -1;
662
+
663
+ }
664
+
665
+
666
+
667
+
668
+
669
+ pBmp->OutBmpData(&img);
670
+
671
+ cout << "読み込みを開始します" << endl;
672
+
673
+
674
+
675
+ returnImage = pBmp->InBmpData();
676
+
677
+ cout << "読み込みが完了しました" << endl;
678
+
679
+
680
+
681
+ while (true) {
682
+
683
+ cv::imshow("書き込み元の画像", img);
684
+
685
+ cv::imshow("共有メモリ内の画像", returnImage);
686
+
687
+ waitKey();
688
+
689
+
690
+
691
+ Sleep(100);
692
+
693
+
694
+
695
+ if (_kbhit() == TRUE) {
696
+
697
+ break;
698
+
699
+ }
700
+
701
+ }
702
+
703
+
704
+
705
+ pBmp->CloseMapObject();
706
+
707
+ delete pBmp;
708
+
709
+
710
+
711
+ _CrtDumpMemoryLeaks();
712
+
713
+
714
+
715
+ return 0;
716
+
717
+ }
718
+
719
+
720
+
721
+ ```
36
722
 
37
723
  ```ここに言語名を入力
38
724