Mil转为opencv图像格式.
有同时使用MATROX_MC图像采集卡和openCV的同志们注意了:下面的这个例子能将MIL采集的图像格式转化为IplImage格式,方便使用openCV。
1
/**//* written by cutepig. hjs00@126.com*/
2
#include <cv.h>
3
#include <highgui.h>
4
#include <math.h>
5
#include <stdio.h>
6
#include <mil.h>
7
8
//lib
9
// cxcore.lib cv.lib highgui.lib mil.lib milcor.lib milgen.lib milmet2.lib milcor2.lib milmet2d.lib milmet2cl.lib milpul.lib milorion.lib mil1394.lib milvga.lib
10
#define CHECK_EXIT(x) {if(!(x)){printf("Error %s\n",#x);exit(0);}}
11
12
class CMIL_CV
13

{
14
MIL_ID MilApplication, /**//* Application identifier. */
15
MilSystem, /**//* System identifier. */
16
//MilDisplay, /* Display identifier. */
17
MilDigitizer, /**//* Digitizer identifier. */
18
MilImageDisp; /**//* Image buffer identifier. */
19
20
public:
21
enum
{
22
Mil_CH0=M_CH0,
23
Mil_CH1=M_CH1,
24
};
25
BOOL MilGetImage(int Channel,IplImage * &dst)
26
{
27
try
28
{
29
MbufClear(MilImageDisp, 0);
30
MdigChannel(MilDigitizer, Channel);
31
MdigGrab(MilDigitizer, MilImageDisp);
32
33
int w=MbufInquire(MilImageDisp,M_SIZE_X,M_NULL);;
34
int h=MbufInquire(MilImageDisp,M_SIZE_Y,M_NULL);;
35
int band=MbufInquire(MilImageDisp,M_SIZE_BAND,M_NULL);;
36
int pitch=MbufInquire(MilImageDisp,M_PITCH_BYTE,M_NULL);;
37
//IplImage *dst=cvCreateImage(cvSize(w,h),8,band);
38
if(!dst)
39
dst=cvCreateImage(cvSize(w,h),8,band);
40
41
//CHECK_EXIT(dst->width==w && dst->height==h && dst->nChannels=band);
42
char* p=dst->imageData;
43
MbufGet(MilImageDisp,p);
44
}
45
catch(
)
46
{
47
return FALSE;
48
}
49
50
return TRUE;
51
};
52
53
BOOL MilOpen(char *DCF_NAME)
54
{
55
try
56
{
57
MappAlloc(M_DEFAULT, &MilApplication);
58
MsysAlloc(M_SYSTEM_METEOR_II, M_DEF_SYSTEM_NUM, M_SETUP, &MilSystem);
59
//MdispAlloc(MilSystem, M_DEFAULT, M_DEF_DISPLAY_FORMAT, M_DEFAULT, &MilDisplay);
60
MdigAlloc(MilSystem, M_DEFAULT, DCF_NAME, M_DEFAULT, &MilDigitizer);
61
62
printf(" Allocate a display buffer, clear it and display it. \n");
63
MbufAllocColor(MilSystem,
64
MdigInquire(MilDigitizer, M_SIZE_BAND, M_NULL),
65
(long) (MdigInquire(MilDigitizer, M_SIZE_X, M_NULL)),
66
(long) (MdigInquire(MilDigitizer, M_SIZE_Y, M_NULL)),
67
8L+M_UNSIGNED,
68
M_IMAGE+M_GRAB+M_DISP,
69
&MilImageDisp);
70
MdigControl(MilDigitizer, M_GRAB_MODE, M_SYNCHRONOUS); //must!!
71
MdigControl(MilDigitizer, M_CAMERA_LOCK, M_ENABLE);//
72
MbufClear(MilImageDisp, 0);
73
}
74
catch(
)
75
{
76
return FALSE;
77
}
78
79
return TRUE;
80
}
81
82
BOOL MilClose()
83
{
84
try
{
85
MbufFree(MilImageDisp);
86
MdigFree(MilDigitizer);
87
MsysFree(MilSystem);
88
MappFree(MilApplication);
89
}catch (
)
{
90
return FALSE;
91
}
92
return TRUE;
93
}
94
95
void Test()
96
{
97
CHECK_EXIT(MilOpen("e:/DCF6.dcf"));
98
IplImage *img0=0; //必须初始化为0
99
IplImage *img1=0;
100
101
cvNamedWindow( "CH0", 1 );
102
cvNamedWindow( "CH1", 1 );
103
printf("\n");
104
do
105
{
106
//奇怪,抓到的图都是CH0的//--要设置同步
107
MilGetImage(M_CH0,img0);
108
CHECK_EXIT(img0);
109
printf("\rImageSize %d*%d*%d",img0->width,img0->height,img0->nChannels);
110
cvShowImage( "CH0", img0 );
111
112
113
MilGetImage(M_CH1,img1);
114
CHECK_EXIT(img1);
115
cvShowImage( "CH1", img1 );
116
}
117
while(cvWaitKey(100)==-1);
118
119
CHECK_EXIT(MilClose());
120
121
cvReleaseImage(&img0);
122
cvReleaseImage(&img1);
123
}
124
};
125
int main(int argc, char** argv)
126

{
127
CMIL_CV milcv;
128
milcv.Test();
129
return 0;
130
}
131
132


2

3

4

5

6

7

8

9

10

11

12

13



14


15


16

17


18


19

20

21



22

23

24

25

26



27

28



29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45


46



47

48

49

50

51

52

53

54



55

56



57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74


75



76

77

78

79

80

81

82

83



84



85

86

87

88

89




90

91

92

93

94

95

96



97

98

99

100

101

102

103

104

105



106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126



127

128

129

130

131

132

- 上一篇 Sad匹配产生致密的深度图.
- 下一篇 三维地形图重建代码.