wangdaren 2020-06-03
<span># Classes and methods whitelist</span><br />core = {‘‘: [‘absdiff‘, ‘add‘, ‘addWeighted‘, ‘bitwise_and‘, ‘bitwise_not‘, ‘bitwise_or‘, ‘bitwise_xor‘, ‘cartToPolar‘,\<br /> ‘compare‘, ‘convertScaleAbs‘, ‘copyMakeBorder‘, ‘countNonZero‘, ‘determinant‘, ‘dft‘, ‘divide‘, ‘eigen‘, \<br /> ‘exp‘, ‘flip‘, ‘getOptimalDFTSize‘,‘gemm‘, ‘hconcat‘, ‘inRange‘, ‘invert‘, ‘kmeans‘, ‘log‘, ‘magnitude‘, \<br /> ‘max‘, ‘mean‘, ‘meanStdDev‘, ‘merge‘, ‘min‘, ‘minMaxLoc‘, ‘mixChannels‘, ‘multiply‘, ‘norm‘, ‘normalize‘, \<br /> ‘perspectiveTransform‘, ‘polarToCart‘, ‘pow‘, ‘randn‘, ‘randu‘, ‘reduce‘, ‘repeat‘, ‘rotate‘, ‘setIdentity‘, ‘setRNGSeed‘, \<br /> ‘solve‘, ‘solvePoly‘, ‘split‘, ‘sqrt‘, ‘subtract‘, ‘trace‘, ‘transform‘, ‘transpose‘, ‘vconcat‘],<br /> ‘Algorithm‘: []}<br /><br />imgproc = {‘‘: [‘Canny‘, ‘GaussianBlur‘, ‘Laplacian‘, ‘HoughLines‘, ‘HoughLinesP‘, ‘HoughCircles‘, ‘Scharr‘,‘Sobel‘, \<br /> ‘adaptiveThreshold‘,‘approxPolyDP‘,‘arcLength‘,‘bilateralFilter‘,‘blur‘,‘boundingRect‘,‘boxFilter‘,\<br /> ‘calcBackProject‘,‘calcHist‘,‘circle‘,‘compareHist‘,‘connectedComponents‘,‘connectedComponentsWithStats‘, \<br /> ‘contourArea‘, ‘convexHull‘, ‘convexityDefects‘, ‘cornerHarris‘,‘cornerMinEigenVal‘,‘createCLAHE‘, \<br /> ‘createLineSegmentDetector‘,‘cvtColor‘,‘demosaicing‘,‘dilate‘, ‘distanceTransform‘,‘distanceTransformWithLabels‘, \<br /> ‘drawContours‘,‘ellipse‘,‘ellipse2Poly‘,‘equalizeHist‘,‘erode‘, ‘filter2D‘, ‘findContours‘,‘fitEllipse‘, \<br /> ‘fitLine‘, ‘floodFill‘,‘getAffineTransform‘, ‘getPerspectiveTransform‘, ‘getRotationMatrix2D‘, ‘getStructuringElement‘, \<br /> ‘goodFeaturesToTrack‘,‘grabCut‘,‘initUndistortRectifyMap‘, ‘integral‘,‘integral2‘, ‘isContourConvex‘, ‘line‘, \<br /> ‘matchShapes‘, ‘matchTemplate‘,‘medianBlur‘, ‘minAreaRect‘, ‘minEnclosingCircle‘, ‘moments‘, ‘morphologyEx‘, \<br /> ‘pointPolygonTest‘, ‘putText‘,‘pyrDown‘,‘pyrUp‘,‘rectangle‘,‘remap‘, ‘resize‘,‘sepFilter2D‘,‘threshold‘, \<br /> ‘undistort‘,‘warpAffine‘,‘warpPerspective‘,‘warpPolar‘,‘watershed‘, \<br /> ‘fillPoly‘, ‘fillConvexPoly‘],<br /> ‘CLAHE‘: [‘apply‘, ‘collectGarbage‘, ‘getClipLimit‘, ‘getTilesGridSize‘, ‘setClipLimit‘, ‘setTilesGridSize‘]}<br /><br />objdetect = {‘‘: [‘groupRectangles‘],<br /> ‘HOGDescriptor‘: [‘load‘, ‘HOGDescriptor‘, ‘getDefaultPeopleDetector‘, ‘getDaimlerPeopleDetector‘, ‘setSVMDetector‘, ‘detectMultiScale‘],<br /> ‘CascadeClassifier‘: [‘load‘, ‘detectMultiScale2‘, ‘CascadeClassifier‘, ‘detectMultiScale3‘, ‘empty‘, ‘detectMultiScale‘]}<br /><br />video = {‘‘: [‘CamShift‘, ‘calcOpticalFlowFarneback‘, ‘calcOpticalFlowPyrLK‘, ‘createBackgroundSubtractorMOG2‘, \<br /> ‘findTransformECC‘, ‘meanShift‘],<br /> ‘BackgroundSubtractorMOG2‘: [‘BackgroundSubtractorMOG2‘, ‘apply‘],<br /> ‘BackgroundSubtractor‘: [‘apply‘, ‘getBackgroundImage‘]}<br /><br />dnn = {‘dnn_Net‘: [‘setInput‘, ‘forward‘],<br /> ‘‘: [‘readNetFromCaffe‘, ‘readNetFromTensorflow‘, ‘readNetFromTorch‘, ‘readNetFromDarknet‘,<br /> ‘readNetFromONNX‘, ‘readNet‘, ‘blobFromImage‘]}<br /><br />features2d = {‘Feature2D‘: [‘detect‘, ‘compute‘, ‘detectAndCompute‘, ‘descriptorSize‘, ‘descriptorType‘, ‘defaultNorm‘, ‘empty‘, ‘getDefaultName‘],<br /> ‘BRISK‘: [‘create‘, ‘getDefaultName‘],<br /> ‘ORB‘: [‘create‘, ‘setMaxFeatures‘, ‘setScaleFactor‘, ‘setNLevels‘, ‘setEdgeThreshold‘, ‘setFirstLevel‘, ‘setWTA_K‘, ‘setScoreType‘, ‘setPatchSize‘, ‘getFastThreshold‘, ‘getDefaultName‘],<br /> ‘MSER‘: [‘create‘, ‘detectRegions‘, ‘setDelta‘, ‘getDelta‘, ‘setMinArea‘, ‘getMinArea‘, ‘setMaxArea‘, ‘getMaxArea‘, ‘setPass2Only‘, ‘getPass2Only‘, ‘getDefaultName‘],<br /> ‘FastFeatureDetector‘: [‘create‘, ‘setThreshold‘, ‘getThreshold‘, ‘setNonmaxSuppression‘, ‘getNonmaxSuppression‘, ‘setType‘, ‘getType‘, ‘getDefaultName‘],<br /> ‘AgastFeatureDetector‘: [‘create‘, ‘setThreshold‘, ‘getThreshold‘, ‘setNonmaxSuppression‘, ‘getNonmaxSuppression‘, ‘setType‘, ‘getType‘, ‘getDefaultName‘],<br /> ‘GFTTDetector‘: [‘create‘, ‘setMaxFeatures‘, ‘getMaxFeatures‘, ‘setQualityLevel‘, ‘getQualityLevel‘, ‘setMinDistance‘, ‘getMinDistance‘, ‘setBlockSize‘, ‘getBlockSize‘, ‘setHarrisDetector‘, ‘getHarrisDetector‘, ‘setK‘, ‘getK‘, ‘getDefaultName‘],<br /> # ‘SimpleBlobDetector‘: [‘create‘],<br /> ‘KAZE‘: [‘create‘, ‘setExtended‘, ‘getExtended‘, ‘setUpright‘, ‘getUpright‘, ‘setThreshold‘, ‘getThreshold‘, ‘setNOctaves‘, ‘getNOctaves‘, ‘setNOctaveLayers‘, ‘getNOctaveLayers‘, ‘setDiffusivity‘, ‘getDiffusivity‘, ‘getDefaultName‘],<br /> ‘AKAZE‘: [‘create‘, ‘setDescriptorType‘, ‘getDescriptorType‘, ‘setDescriptorSize‘, ‘getDescriptorSize‘, ‘setDescriptorChannels‘, ‘getDescriptorChannels‘, ‘setThreshold‘, ‘getThreshold‘, ‘setNOctaves‘, ‘getNOctaves‘, ‘setNOctaveLayers‘, ‘getNOctaveLayers‘, ‘setDiffusivity‘, ‘getDiffusivity‘, ‘getDefaultName‘],<br /> ‘DescriptorMatcher‘: [‘add‘, ‘clear‘, ‘empty‘, ‘isMaskSupported‘, ‘train‘, ‘match‘, ‘knnMatch‘, ‘radiusMatch‘, ‘clone‘, ‘create‘],<br /> ‘BFMatcher‘: [‘isMaskSupported‘, ‘create‘],<br /> ‘‘: [‘drawKeypoints‘, ‘drawMatches‘, ‘drawMatchesKnn‘]}<br /><br />photo = {‘‘: [‘createAlignMTB‘, ‘createCalibrateDebevec‘, ‘createCalibrateRobertson‘, \<br /> ‘createMergeDebevec‘, ‘createMergeMertens‘, ‘createMergeRobertson‘, \<br /> ‘createTonemapDrago‘, ‘createTonemapMantiuk‘, ‘createTonemapReinhard‘, ‘inpaint‘],<br /> ‘CalibrateCRF‘: [‘process‘],<br /> ‘AlignMTB‘ : [‘calculateShift‘, ‘shiftMat‘, ‘computeBitmaps‘, ‘getMaxBits‘, ‘setMaxBits‘, \<br /> ‘getExcludeRange‘, ‘setExcludeRange‘, ‘getCut‘, ‘setCut‘],<br /> ‘CalibrateDebevec‘ : [‘getLambda‘, ‘setLambda‘, ‘getSamples‘, ‘setSamples‘, ‘getRandom‘, ‘setRandom‘],<br /> ‘CalibrateRobertson‘ : [‘getMaxIter‘, ‘setMaxIter‘, ‘getThreshold‘, ‘setThreshold‘, ‘getRadiance‘],<br /> ‘MergeExposures‘ : [‘process‘],<br /> ‘MergeDebevec‘ : [‘process‘],<br /> ‘MergeMertens‘ : [‘process‘, ‘getContrastWeight‘, ‘setContrastWeight‘, ‘getSaturationWeight‘, \<br /> ‘setSaturationWeight‘, ‘getExposureWeight‘, ‘setExposureWeight‘],<br /> ‘MergeRobertson‘ : [‘process‘],<br /> ‘Tonemap‘ : [‘process‘ , ‘getGamma‘, ‘setGamma‘],<br /> ‘TonemapDrago‘ : [‘getSaturation‘, ‘setSaturation‘, ‘getBias‘, ‘setBias‘, \<br /> ‘getSigmaColor‘, ‘setSigmaColor‘, ‘getSigmaSpace‘,‘setSigmaSpace‘],<br /> ‘TonemapMantiuk‘ : [‘getScale‘, ‘setScale‘, ‘getSaturation‘, ‘setSaturation‘],<br /> ‘TonemapReinhard‘ : [‘getIntensity‘, ‘setIntensity‘, ‘getLightAdaptation‘, ‘setLightAdaptation‘, \<br /> ‘getColorAdaptation‘, ‘setColorAdaptation‘]<br /> }<br /><br />aruco = {‘‘: [‘detectMarkers‘, ‘drawDetectedMarkers‘, ‘drawAxis‘, ‘estimatePoseSingleMarkers‘, ‘estimatePoseBoard‘, ‘estimatePoseCharucoBoard‘, ‘interpolateCornersCharuco‘, ‘drawDetectedCornersCharuco‘],<br /> ‘aruco_Dictionary‘: [‘get‘, ‘drawMarker‘],<br /> ‘aruco_Board‘: [‘create‘],<br /> ‘aruco_GridBoard‘: [‘create‘, ‘draw‘],<br /> ‘aruco_CharucoBoard‘: [‘create‘, ‘draw‘],<br /> }<br /><br />calib3d = {‘‘: [‘findHomography‘, ‘calibrateCameraExtended‘, ‘drawFrameAxes‘, ‘estimateAffine2D‘, ‘getDefaultNewCameraMatrix‘, ‘initUndistortRectifyMap‘, ‘Rodrigues‘]}<br /><br /><br />white_list = makeWhiteList([core, imgproc, objdetect, video, dnn, features2d, photo, aruco, calib3d])
<span>#</span><span>include</span><span> </span><span>"opencv2/highgui/highgui.hpp"</span><br /><span>#</span><span>include</span><span> </span><span>"opencv2/imgproc/imgproc.hpp"</span><br /><span>#</span><span>include</span><span> </span><span><</span><span>iostream</span><span>></span><br /><span>#</span><span>include</span><span> </span><span><</span><span>stdio.h</span><span>></span><br /><span>#</span><span>include</span><span> </span><span><</span><span>stdlib.h</span><span>></span><br /><span>#</span><span>include</span><span> </span><span><</span><span>math.h</span><span>></span><br /><span>using</span><span> </span><span>namespace</span><span> cv;</span><br /><span>using</span><span> </span><span>namespace</span><span> std;</span><br /><span>//找到所提取轮廓的中心点</span><br /><span>//在提取的中心小正方形的边界上每隔周长个像素提取一个点的坐标,求所提取四个点的平均坐标(即为小正方形的大致中心)</span><br /><span>Point Center_cal(vector</span><span><</span><span>vector</span><span><</span><span>Point</span><span>></span><span> </span><span>></span><span> contours,</span><span>int</span><span> i)</span><br /><span>{</span><br /><span> </span><span>int</span><span> centerx</span><span>=</span><span>0</span><span>,centery</span><span>=</span><span>0</span><span>,n</span><span>=</span><span>contours[i].size();</span><br /><span> centerx </span><span>=</span><span> (contours[i][n</span><span>/</span><span>4</span><span>].x </span><span>+</span><span> contours[i][n</span><span>*</span><span>2</span><span>/</span><span>4</span><span>].x </span><span>+</span><span> contours[i][</span><span>3</span><span>*</span><span>n</span><span>/</span><span>4</span><span>].x </span><span>+</span><span> contours[i][n</span><span>-</span><span>1</span><span>].x)</span><span>/</span><span>4</span><span>;</span><br /><span> centery </span><span>=</span><span> (contours[i][n</span><span>/</span><span>4</span><span>].y </span><span>+</span><span> contours[i][n</span><span>*</span><span>2</span><span>/</span><span>4</span><span>].y </span><span>+</span><span> contours[i][</span><span>3</span><span>*</span><span>n</span><span>/</span><span>4</span><span>].y </span><span>+</span><span> contours[i][n</span><span>-</span><span>1</span><span>].y)</span><span>/</span><span>4</span><span>;</span><br /><span> Point point1</span><span>=</span><span>Point(centerx,centery);</span><br /><span> </span><span>return</span><span> point1;</span><br /><span>}</span><br /><span>int</span><span> main( </span><span>int</span><span> argc, </span><span>char</span><span>*</span><span>*</span><span> argv[] )</span><br /><span>{</span><br /><span> Mat src </span><span>=</span><span> imread( </span><span>"e:/sandbox/qrcode.jpg"</span><span>, </span><span>1</span><span> );</span><br /><span> resize(src,src,Size(</span><span>800</span><span>,</span><span>600</span><span>));</span><span>//标准大小</span><br /><span> Mat src_gray;</span><br /><span> Mat src_all</span><span>=</span><span>src.clone();</span><br /><span> Mat threshold_output;</span><br /><span> vector</span><span><</span><span>vector</span><span><</span><span>Point</span><span>></span><span> </span><span>></span><span> contours,contours2;</span><br /><span> vector</span><span><</span><span>Vec4i</span><span>></span><span> hierarchy;</span><br /><span> </span><span>//预处理</span><br /><span> cvtColor( src, src_gray, CV_BGR2GRAY );</span><br /><span> blur( src_gray, src_gray, Size(</span><span>3</span><span>,</span><span>3</span><span>) ); </span><span>//模糊,去除毛刺</span><br /><span> threshold( src_gray, threshold_output, </span><span>100</span><span>, </span><span>255</span><span>, THRESH_OTSU );</span><br /><span> </span><span>//寻找轮廓 </span><br /><span> </span><span>//第一个参数是输入图像 2值化的</span><br /><span> </span><span>//第二个参数是内存存储器,FindContours找到的轮廓放到内存里面。</span><br /><span> </span><span>//第三个参数是层级,**[Next, Previous, First_Child, Parent]** 的vector</span><br /><span> </span><span>//第四个参数是类型,采用树结构</span><br /><span> </span><span>//第五个参数是节点拟合模式,这里是全部寻找</span><br /><span> findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CHAIN_APPROX_NONE, Point(</span><span>0</span><span>, </span><span>0</span><span>) );</span><br /><span> </span><span>//轮廓筛选</span><br /><span> </span><span>int</span><span> c</span><span>=</span><span>0</span><span>,ic</span><span>=</span><span>0</span><span>,area</span><span>=</span><span>0</span><span>;</span><br /><span> </span><span>int</span><span> parentIdx</span><span>=</span><span>-</span><span>1</span><span>;</span><br /><span> </span><span>for</span><span>( </span><span>int</span><span> i </span><span>=</span><span> </span><span>0</span><span>; i</span><span><</span><span> contours.size(); i</span><span>++</span><span> )</span><br /><span> {</span><br /><span> </span><span>//hierarchy[i][2] != -1 表示不是最外面的轮廓</span><br /><span> </span><span>if</span><span> (hierarchy[i][</span><span>2</span><span>] </span><span>!=</span><span> </span><span>-</span><span>1</span><span> </span><span>&&</span><span> ic</span><span>==</span><span>0</span><span>)</span><br /><span> {</span><br /><span> parentIdx </span><span>=</span><span> i; </span><br /><span> ic</span><span>++</span><span>;</span><br /><span> }</span><br /><span> </span><span>else</span><span> </span><span>if</span><span> (hierarchy[i][</span><span>2</span><span>] </span><span>!=</span><span> </span><span>-</span><span>1</span><span>)</span><br /><span> {</span><br /><span> ic</span><span>++</span><span>;</span><br /><span> }</span><br /><span> </span><span>//最外面的清0</span><br /><span> </span><span>else</span><span> </span><span>if</span><span>(hierarchy[i][</span><span>2</span><span>] </span><span>==</span><span> </span><span>-</span><span>1</span><span>)</span><br /><span> {</span><br /><span> ic </span><span>=</span><span> </span><span>0</span><span>;</span><br /><span> parentIdx </span><span>=</span><span> </span><span>-</span><span>1</span><span>;</span><br /><span> }</span><br /><span> </span><span>//找到定位点信息</span><br /><span> </span><span>if</span><span> ( ic </span><span>></span><span>=</span><span> </span><span>2</span><span>)</span><br /><span> {</span><br /><span> contours2.push_back(contours[parentIdx]);</span><br /><span> ic </span><span>=</span><span> </span><span>0</span><span>;</span><br /><span> parentIdx </span><span>=</span><span> </span><span>-</span><span>1</span><span>;</span><br /><span> }</span><br /><span> }</span><br /><span> </span><span>//填充定位点</span><br /><span> </span><span>for</span><span>(</span><span>int</span><span> i</span><span>=</span><span>0</span><span>; i</span><span><</span><span>contours2.size(); i</span><span>++</span><span>)</span><br /><span> drawContours( src_all, contours2, i, CV_RGB(</span><span>0</span><span>,</span><span>255</span><span>,</span><span>0</span><span>) , </span><span>-</span><span>1</span><span> );</span><br /><span> </span><span>//连接定位点</span><br /><span> Point point[</span><span>3</span><span>];</span><br /><span> </span><span>for</span><span>(</span><span>int</span><span> i</span><span>=</span><span>0</span><span>; i</span><span><</span><span>contours2.size(); i</span><span>++</span><span>)</span><br /><span> {</span><br /><span> point[i] </span><span>=</span><span> Center_cal( contours2, i );</span><br /><span> }</span><br /><span> </span><br /><span> line(src_all,point[</span><span>0</span><span>],point[</span><span>1</span><span>],Scalar(</span><span>0</span><span>,</span><span>0</span><span>,</span><span>255</span><span>),</span><span>2</span><span>);</span><br /><span> line(src_all,point[</span><span>1</span><span>],point[</span><span>2</span><span>],Scalar(</span><span>0</span><span>,</span><span>0</span><span>,</span><span>255</span><span>),</span><span>2</span><span>);</span><br /><span> line(src_all,point[</span><span>0</span><span>],point[</span><span>2</span><span>],Scalar(</span><span>0</span><span>,</span><span>0</span><span>,</span><span>255</span><span>),</span><span>2</span><span>);</span><br /><span> </span><br /><span> imshow( </span><span>"结果"</span><span>, src_all );</span><br /><span> waitKey(</span><span>0</span><span>);</span><br /><span> </span><span>return</span><span>(</span><span>0</span><span>);</span><br /><span>}</span>