■ 입체 사진을 만드는 방법을 보여준다.
▶ MainForm.cs
1 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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
using System; using System.Drawing; using System.Windows.Forms; using Emgu.CV; using Emgu.CV.Structure; using OpenTK.Graphics; using OpenTK.Graphics.OpenGL; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 왼쪽 이미지 /// </summary> private Image<Bgr, Byte> leftImage; /// <summary> /// 오른쪽 이미지 /// </summary> private Image<Bgr, byte> rightImage; /// <summary> /// 3D 포인트 배열 /// </summary> private MCvPoint3D32f[] point3DArray; /// <summary> /// GL 로드 여부 /// </summary> private bool giLoaded = false; /// <summary> /// 텍스처 배열 /// </summary> private int[] textureArray; /// <summary> /// 텍스처 이미지 /// </summary> private Image<Bgr, Byte> textureImage; /// <summary> /// 각도 /// </summary> private double angle = 0.0; /// <summary> /// 각도 증가치 /// </summary> private double angleIncrement = 0.2; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.leftImage = new Image<Bgr, byte>("imageLeft.png" ); this.rightImage = new Image<Bgr, byte>("imageRight.png"); Image<Gray, short> disparityMapImage; Computer3DPointArray ( leftImage.Convert<Gray, Byte>(), rightImage.Convert<Gray, Byte>(), out disparityMapImage, out this.point3DArray ); this.disparityMapImageBox.Image = disparityMapImage; FormClosed += Form_FormClosed; this.resultGlControl.Load += resultGlControl_Load; this.resultGlControl.Paint += resultGlControl_Paint; this.resultGlControl.Resize += resultGlControl_Resize; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 폼 닫은 경우 처리하기 - Form_FormClosed(sender, e) /// <summary> /// 폼 닫은 경우 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_FormClosed(object sender, FormClosedEventArgs e) { this.resultGlControl.Dispose(); } #endregion #region 결과 GL 컨트롤 로드시 처리하기 - resultGlControl_Load(sender, e) /// <summary> /// 결과 GL 컨트롤 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void resultGlControl_Load(object sender, EventArgs e) { this.giLoaded = true; GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f); SetupViewport(); #region 3D 포인트 클라아두를 위한 텍스처를 생성한다. int repeat = (int)All.Repeat; int linear = (int)All.Linear; GL.Enable(EnableCap.CullFace); GL.Enable(EnableCap.Texture2D); this.textureArray = new int[1]; GL.GenTextures(1, this.textureArray); GL.BindTexture(TextureTarget.Texture2D, this.textureArray[0]); GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureWrapS , ref repeat); GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureWrapT , ref repeat); GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, ref linear); GL.TexParameterI(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, ref linear); GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (int)All.Decal); GL.ShadeModel(ShadingModel.Smooth); GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest); GL.BindTexture(TextureTarget.Texture2D, 1); Size imageSize = leftImage.Size; int maximumDimension = Math.Max(imageSize.Width, imageSize.Height); using(Image<Bgr, Byte> squareImage = new Image<Bgr, byte>(maximumDimension, maximumDimension)) { Rectangle roiRectangle = new Rectangle ( maximumDimension / 2 - imageSize.Width / 2, maximumDimension / 2 - imageSize.Height / 2, imageSize.Width, imageSize.Height ); squareImage.ROI = roiRectangle; CvInvoke.cvCopy(leftImage, squareImage, IntPtr.Zero); squareImage.ROI = Rectangle.Empty; this.textureImage = squareImage.Resize(512, 512, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC, true); this.textureImage._Flip(Emgu.CV.CvEnum.FLIP.VERTICAL); GL.TexImage2D ( TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb8, this.textureImage.Width, this.textureImage.Height, 0, PixelFormat.Bgr, PixelType.UnsignedByte, this.textureImage.MIplImage.imageData ); } #endregion } #endregion #region 결과 GL 컨트롤 크기 변경시 처리하기 - resultGlControl_Resize(sender, e) /// <summary> /// 결과 GL 컨트롤 크기 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void resultGlControl_Resize(object sender, EventArgs e) { if(!this.giLoaded) { return; } SetupViewport(); } #endregion #region 결과 GL 컨트롤 PAINT 처리하기 - resultGlControl_Paint(sender, e) /// <summary> /// 결과 GL 컨트롤 PAINT 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void resultGlControl_Paint(object sender, PaintEventArgs e) { if(!this.giLoaded) { return; } GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Enable(EnableCap.Texture2D); GL.PushMatrix(); GL.Rotate(10.0, 0.0, 1.0, 0.0); GL.Rotate(this.angle, 0.0, 1.0, 0.0); GL.Begin(BeginMode.Points); GL.Color4(1.0, 1.0, 1.0, 1.0); foreach(MCvPoint3D32f point3D in this.point3DArray) { GL.TexCoord2(point3D.x + 0.5f, point3D.y + 0.5f); GL.Vertex3(point3D.x, point3D.y, point3D.z); } GL.End(); GL.PopMatrix(); if(this.angle >= 30.0 || this.angle <= -30.0) { this.angleIncrement = -this.angleIncrement; } this.angle += this.angleIncrement; GraphicsContext.CurrentContext.SwapBuffers(); this.resultGlControl.Invalidate(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 3D 포인트 배열 계산하기 - Computer3DPointArray(leftImage, rightImage, disparityMapImage, point3DArray) /// <summary> /// 3D 포인트 배열 계산하기 /// </summary> /// <param name="leftImage">왼쪽 이미지</param> /// <param name="rightImage">오른쪽 이미지</param> /// <param name="disparityMapImage">차이 맵 이미지</param> /// <param name="point3DArray">3D 포인트 배열</param> private static void Computer3DPointArray(Image<Gray, Byte> leftImage, Image<Gray, Byte> rightImage, out Image<Gray, short> disparityMapImage, out MCvPoint3D32f[] point3DArray) { Size imageSize = leftImage.Size; disparityMapImage = new Image<Gray, short>(imageSize); //using(StereoSGBM stereoSolver = new StereoSGBM(5, 64, 0, 0, 0, 0, 0, 0, 0, 0, StereoSGBM.Mode.SGBM)) using(StereoBM stereoSolver = new StereoBM(Emgu.CV.CvEnum.STEREO_BM_TYPE.BASIC, 0)) { stereoSolver.FindStereoCorrespondence(leftImage, rightImage, disparityMapImage); float scale = Math.Max(imageSize.Width, imageSize.Height); using ( Matrix<double> qMartix = new Matrix<double> ( new double[,] { { 1.0, 0.0, 0.0, -imageSize.Width / 2 }, { 0.0, -1.0, 0.0, imageSize.Height / 2 }, { 0.0, 0.0, -1.0, 0.0 }, { 0.0, 0.0, 0.0, scale } } ) ) { point3DArray = PointCollection.ReprojectImageTo3D(disparityMapImage, qMartix); } } } #endregion #region 뷰포트 셋업하기 - SetupViewport() /// <summary> /// 뷰포트 셋업하기 /// </summary> private void SetupViewport() { int width = this.resultGlControl.Width; int height = this.resultGlControl.Height; GL.MatrixMode(MatrixMode.Projection); GL.LoadIdentity(); GL.Ortho(-1.5, 1.5, -1.5, 1.5, -1.5, 1.5); GL.Viewport(0, 0, width, height); } #endregion } } |