■ SURF 특징점을 찾는 방법을 보여준다.
▶ MatchHelper.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 |
using System; using System.Diagnostics; using System.Drawing; using Emgu.CV; using Emgu.CV.CvEnum; using Emgu.CV.Features2D; using Emgu.CV.Structure; using Emgu.CV.Util; #if !IOS using Emgu.CV.GPU; #endif namespace TestProject { /// <summary> /// 매칭 헬퍼 /// </summary> public static class MatchHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 매칭 찾기 - FindMatch(modelImage, sourceImage, matchTime, modelVectorOfKeyPoint, sourceVectorOfKeyPoint, indexMatrix, maskMatrix, homographyMatrix) /// <summary> /// 매칭 찾기 /// </summary> /// <param name="modelImage">모델 이미지</param> /// <param name="sourceImage">소스 이미지</param> /// <param name="matchTime">매칭 시간</param> /// <param name="modelVectorOfKeyPoint">모델 키 포인트 벡터</param> /// <param name="sourceVectorOfKeyPoint">소스 키 포인트 벡터</param> /// <param name="indexMatrix">인덱스 매트릭스</param> /// <param name="maskMatrix">마스크 매트릭스</param> /// <param name="homographyMatrix">호모 그래피 매트릭스</param> public static void FindMatch(Image<Gray, Byte> modelImage, Image<Gray, byte> sourceImage, out long matchTime, out VectorOfKeyPoint modelVectorOfKeyPoint, out VectorOfKeyPoint sourceVectorOfKeyPoint, out Matrix<int> indexMatrix, out Matrix<byte> maskMatrix, out HomographyMatrix homographyMatrix) { int k = 2; double uniquenessThreshold = 0.8; SURFDetector surfDetector = new SURFDetector(500, false); Stopwatch stopwatch; homographyMatrix = null; #if !IOS if(GpuInvoke.HasCuda) { GpuSURFDetector gpuSURFDetector = new GpuSURFDetector(surfDetector.SURFParams, 0.01f); using(GpuImage<Gray, Byte> modelGpuImage = new GpuImage<Gray, byte>(modelImage)) { using(GpuMat<float> modelKeyPointGpuMat = gpuSURFDetector.DetectKeyPointsRaw(modelGpuImage, null)) { using(GpuMat<float> modelDescriptorGpuMat = gpuSURFDetector.ComputeDescriptorsRaw(modelGpuImage, null, modelKeyPointGpuMat)) { using(GpuBruteForceMatcher<float> matcher = new GpuBruteForceMatcher<float>(DistanceType.L2)) { modelVectorOfKeyPoint = new VectorOfKeyPoint(); gpuSURFDetector.DownloadKeypoints(modelKeyPointGpuMat, modelVectorOfKeyPoint); stopwatch = Stopwatch.StartNew(); using(GpuImage<Gray, Byte> sourceGpuImage = new GpuImage<Gray, byte>(sourceImage)) { using(GpuMat<float> sourceKeyPointGpuMat = gpuSURFDetector.DetectKeyPointsRaw(sourceGpuImage, null)) { using(GpuMat<float> sourceDescriptorGpuMat = gpuSURFDetector.ComputeDescriptorsRaw(sourceGpuImage, null, sourceKeyPointGpuMat)) { using(GpuMat<int> matchIndexGpuMat = new GpuMat<int>(sourceDescriptorGpuMat.Size.Height, k, 1, true)) { using(GpuMat<float> matchDistainceGpuMat = new GpuMat<float>(sourceDescriptorGpuMat.Size.Height, k, 1, true)) { using(GpuMat<Byte> maskGpuMat = new GpuMat<byte>(matchIndexGpuMat.Size.Height, 1, 1)) { using(Stream stream = new Stream()) { matcher.KnnMatchSingle ( sourceDescriptorGpuMat, modelDescriptorGpuMat, matchIndexGpuMat, matchDistainceGpuMat, k, null, stream ); indexMatrix = new Matrix<int>(matchIndexGpuMat.Size); maskMatrix = new Matrix<byte>(maskGpuMat.Size); using(GpuMat<float> col0 = matchDistainceGpuMat.Col(0)) { using(GpuMat<float> col1 = matchDistainceGpuMat.Col(1)) { GpuInvoke.Multiply(col1, new MCvScalar(uniquenessThreshold), col1, stream); GpuInvoke.Compare(col0, col1, maskGpuMat, CMP_TYPE.CV_CMP_LE, stream); } } sourceVectorOfKeyPoint = new VectorOfKeyPoint(); gpuSURFDetector.DownloadKeypoints(sourceKeyPointGpuMat, sourceVectorOfKeyPoint); stream.WaitForCompletion(); maskGpuMat.Download(maskMatrix); matchIndexGpuMat.Download(indexMatrix); if(GpuInvoke.CountNonZero(maskGpuMat) >= 4) { int nonZeroCount = Features2DToolbox.VoteForSizeAndOrientation ( modelVectorOfKeyPoint, sourceVectorOfKeyPoint, indexMatrix, maskMatrix, 1.5, 20 ); if(nonZeroCount >= 4) { homographyMatrix = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures ( modelVectorOfKeyPoint, sourceVectorOfKeyPoint, indexMatrix, maskMatrix, 2 ); } } stopwatch.Stop(); } } } } } } } } } } } } else #endif { modelVectorOfKeyPoint = new VectorOfKeyPoint(); Matrix<float> modelDescriptorMatrix = surfDetector.DetectAndCompute(modelImage, null, modelVectorOfKeyPoint); stopwatch = Stopwatch.StartNew(); sourceVectorOfKeyPoint = new VectorOfKeyPoint(); Matrix<float> sourceDescriptorMatrix = surfDetector.DetectAndCompute(sourceImage, null, sourceVectorOfKeyPoint); BruteForceMatcher<float> matcher = new BruteForceMatcher<float>(DistanceType.L2); matcher.Add(modelDescriptorMatrix); indexMatrix = new Matrix<int>(sourceDescriptorMatrix.Rows, k); using(Matrix<float> distanceMatrix = new Matrix<float>(sourceDescriptorMatrix.Rows, k)) { matcher.KnnMatch(sourceDescriptorMatrix, indexMatrix, distanceMatrix, k, null); maskMatrix = new Matrix<byte>(distanceMatrix.Rows, 1); maskMatrix.SetValue(255); Features2DToolbox.VoteForUniqueness(distanceMatrix, uniquenessThreshold, maskMatrix); } int nonZeroCount = CvInvoke.cvCountNonZero(maskMatrix); if(nonZeroCount >= 4) { nonZeroCount = Features2DToolbox.VoteForSizeAndOrientation ( modelVectorOfKeyPoint, sourceVectorOfKeyPoint, indexMatrix, maskMatrix, 1.5, 20 ); if(nonZeroCount >= 4) { homographyMatrix = Features2DToolbox.GetHomographyMatrixFromMatchedFeatures ( modelVectorOfKeyPoint, sourceVectorOfKeyPoint, indexMatrix, maskMatrix, 2 ); } } stopwatch.Stop(); } matchTime = stopwatch.ElapsedMilliseconds; } #endregion #region 그리기 - Draw(modelImage, sourceImage, matchTime) /// <summary> /// 그리기 /// </summary> /// <param name="modelImage">모델 이미지</param> /// <param name="sourceImage">소스 이미지</param> /// <param name="matchTime">매칭 시간</param> /// <returns>이미지</returns> public static Image<Bgr, Byte> Draw(Image<Gray, Byte> modelImage, Image<Gray, byte> sourceImage, out long matchTime) { HomographyMatrix homographyMatrix; VectorOfKeyPoint modelVectorOfKeyPoint; VectorOfKeyPoint sourceVectorOfKeyPoint; Matrix<int> indexMatrix; Matrix<byte> maskMatrix; FindMatch ( modelImage, sourceImage, out matchTime, out modelVectorOfKeyPoint, out sourceVectorOfKeyPoint, out indexMatrix, out maskMatrix, out homographyMatrix ); Image<Bgr, Byte> resultImage = Features2DToolbox.DrawMatches ( modelImage, modelVectorOfKeyPoint, sourceImage, sourceVectorOfKeyPoint, indexMatrix, new Bgr(255, 255, 255), new Bgr(255, 255, 255), maskMatrix, Features2DToolbox.KeypointDrawType.DEFAULT ); if(homographyMatrix != null) { Rectangle rectangle = modelImage.ROI; PointF[] pointArray = new PointF[] { new PointF(rectangle.Left , rectangle.Bottom), new PointF(rectangle.Right, rectangle.Bottom), new PointF(rectangle.Right, rectangle.Top ), new PointF(rectangle.Left , rectangle.Top ) }; homographyMatrix.ProjectPoints(pointArray); resultImage.DrawPolyline ( Array.ConvertAll<PointF, Point>(pointArray, Point.Round), true, new Bgr(Color.Red), 5 ); } return resultImage; } #endregion } } |
▶ Program.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 |
using System; using System.Windows.Forms; using Emgu.CV; using Emgu.CV.GPU; using Emgu.CV.Structure; using Emgu.CV.UI; namespace TestProject { /// <summary> /// 프로그램 /// </summary> static class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> [STAThread] private static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); long matchTime; using(Image<Gray, Byte> modelImage = new Image<Gray, byte>("box.png")) { using(Image<Gray, Byte> sourceImage = new Image<Gray, byte>("box_in_scene.png")) { Image<Bgr, byte> resultImage = MatchHelper.Draw(modelImage, sourceImage, out matchTime); ImageViewer.Show ( resultImage, string.Format ( "{0}를 사용해 일치 여부 판단 : {1} 밀리초", GpuInvoke.HasCuda ? "GPU" : "CPU", matchTime ) ); } } } #endregion } } |