前言
东莞,晴,29至27度。忙了一天,最终能够写写东西了。今天继续昨天的话题,我们在昨天的例了基础上完好,通过匹配关键点求出映射从而找到场景中的已知对象。
目标
本文你将学习
- 採用nodeitk的findHomography和perspectiveTransform进行对象识别。
- 此外,样例基本包括nodeitk的一些基本数据结构的使用:NodeOpenCVMat, NodeOpenCVKeyPoint, NodeOpenCVPoint
- 上述主要的数据结构在nodeitk版本号稳定后将会在使用手冊中说明
var node_itk = require('./node-itk'); var img_object = node_itk.cv.imread( "./images/box.png", node_itk.cv.CV_LOAD_IMAGE_GRAYSCALE ); var img_scene = node_itk.cv.imread( "./images/box_in_scene.png", node_itk.cv.CV_LOAD_IMAGE_GRAYSCALE ); minHessian = 400 detector = new node_itk.cv.NodeOpenCVFeatureDetector("SURF") detector.Set("hessianThreshold", minHessian) keypoints_object = detector.Detect( img_object ); keypoints_scene = detector.Detect( img_scene ); extractor = new node_itk.cv.NodeOpenCVDescriptorExtractor("SURF"); descriptors_object = extractor.Compute(img_object, keypoints_object) descriptors_scene = extractor.Compute(img_scene, keypoints_scene) matcher = new node_itk.cv.NodeOpenCVDescriptorMatcher("FlannBased"); matches = matcher.Match(descriptors_object, descriptors_scene); max_dist=0 min_dist=100 for (var i = 0; i < descriptors_object.Rows(); i++ ) { dist = matches[i].GetDistance(); if (dist < min_dist) min_dist = dist; if (dist > max_dist) max_dist = dist; }; console.log("-- Max dist : " + max_dist + " ") console.log("-- Min dist : " + min_dist + " ") var good_matches = []; for( var i = 0; i < descriptors_object.Rows(); i++ ){ if( matches[i].GetDistance() <= 3*min_dist ) { good_matches.push( matches[i] ); } } img_matches = node_itk.cv.DrawMatches(img_object, keypoints_object, img_scene, keypoints_scene, good_matches); var obj=[], scene=[]; for (var i = 0; i < good_matches.length; i++) { obj.push( keypoints_object[good_matches[i].GetQueryIdx()].PT() ) scene.push( keypoints_scene[good_matches[i].GetTrainIdx()].PT() ) }; H = node_itk.cv.FindHomography( obj, scene, node_itk.cv.CV_RANSAC ); obj_corners = [] obj_corners[0] = new node_itk.cv.NodeOpenCVPoint("Point2d", [0,0]) obj_corners[1] = new node_itk.cv.NodeOpenCVPoint("Point2d", [img_object.Cols(),0]) obj_corners[2] = new node_itk.cv.NodeOpenCVPoint("Point2d", [img_object.Cols(),img_object.Rows()]) obj_corners[3] = new node_itk.cv.NodeOpenCVPoint("Point2d", [0,img_object.Rows()]) tmp = new node_itk.cv.NodeOpenCVPoint("Point2d", [img_object.Cols(),0]); color = new node_itk.cv.NodeOpenCVScalar("Scalar", [0,255,0]); scene_corners = node_itk.cv.PerspectiveTransform(obj_corners, H.res); node_itk.cv.Line(img_matches, scene_corners[0].Add(tmp), scene_corners[1].Add(tmp), color, 2) node_itk.cv.Line(img_matches, scene_corners[1].Add(tmp), scene_corners[2].Add(tmp), color, 2) node_itk.cv.Line(img_matches, scene_corners[2].Add(tmp), scene_corners[3].Add(tmp), color, 2) node_itk.cv.Line(img_matches, scene_corners[3].Add(tmp), scene_corners[0].Add(tmp), color, 2) node_itk.cv.NamedWindow( "Good Matches & Object detection", node_itk.cv.CV_WINDOW_AUTOSIZE ); node_itk.cv.imshow( "Good Matches & Object detection", img_matches ); node_itk.cv.WaitKey ( 0 );
结果
小结
本文是昨天话题的深化,代码依旧比較简洁。这是nodeitk遵循的原则:以简单的方式高速实现图像处理应用。喜欢的朋友就点踩,想说点东西的就评论吧!^_^ 待续