| 1 | /*M/////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // |
| 3 | // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. |
| 4 | // |
| 5 | // By downloading, copying, installing or using the software you agree to this license. |
| 6 | // If you do not agree to this license, do not download, install, |
| 7 | // copy or use the software. |
| 8 | // |
| 9 | // |
| 10 | // License Agreement |
| 11 | // For Open Source Computer Vision Library |
| 12 | // |
| 13 | // Copyright (C) 2000, Intel Corporation, all rights reserved. |
| 14 | // Copyright (C) 2013, OpenCV Foundation, all rights reserved. |
| 15 | // Third party copyrights are property of their respective owners. |
| 16 | // |
| 17 | // Redistribution and use in source and binary forms, with or without modification, |
| 18 | // are permitted provided that the following conditions are met: |
| 19 | // |
| 20 | // * Redistribution's of source code must retain the above copyright notice, |
| 21 | // this list of conditions and the following disclaimer. |
| 22 | // |
| 23 | // * Redistribution's in binary form must reproduce the above copyright notice, |
| 24 | // this list of conditions and the following disclaimer in the documentation |
| 25 | // and/or other materials provided with the distribution. |
| 26 | // |
| 27 | // * The name of the copyright holders may not be used to endorse or promote products |
| 28 | // derived from this software without specific prior written permission. |
| 29 | // |
| 30 | // This software is provided by the copyright holders and contributors "as is" and |
| 31 | // any express or implied warranties, including, but not limited to, the implied |
| 32 | // warranties of merchantability and fitness for a particular purpose are disclaimed. |
| 33 | // In no event shall the Intel Corporation or contributors be liable for any direct, |
| 34 | // indirect, incidental, special, exemplary, or consequential damages |
| 35 | // (including, but not limited to, procurement of substitute goods or services; |
| 36 | // loss of use, data, or profits; or business interruption) however caused |
| 37 | // and on any theory of liability, whether in contract, strict liability, |
| 38 | // or tort (including negligence or otherwise) arising in any way out of |
| 39 | // the use of this software, even if advised of the possibility of such damage. |
| 40 | // |
| 41 | //M*/ |
| 42 | #include "precomp.hpp" |
| 43 | |
| 44 | void cv::cornerSubPix( InputArray _image, InputOutputArray _corners, |
| 45 | Size win, Size zeroZone, TermCriteria criteria ) |
| 46 | { |
| 47 | CV_INSTRUMENT_REGION(); |
| 48 | |
| 49 | const int MAX_ITERS = 100; |
| 50 | int win_w = win.width * 2 + 1, win_h = win.height * 2 + 1; |
| 51 | int i, j, k; |
| 52 | int max_iters = (criteria.type & TermCriteria::MAX_ITER) ? MIN(MAX(criteria.maxCount, 1), MAX_ITERS) : MAX_ITERS; |
| 53 | double eps = (criteria.type & TermCriteria::EPS) ? MAX(criteria.epsilon, 0.) : 0; |
| 54 | eps *= eps; // use square of error in comparison operations |
| 55 | |
| 56 | cv::Mat src = _image.getMat(), cornersmat = _corners.getMat(); |
| 57 | int count = cornersmat.checkVector(elemChannels: 2, CV_32F); |
| 58 | CV_Assert( count >= 0 ); |
| 59 | Point2f* corners = cornersmat.ptr<Point2f>(); |
| 60 | |
| 61 | if( count == 0 ) |
| 62 | return; |
| 63 | |
| 64 | CV_Assert( win.width > 0 && win.height > 0 ); |
| 65 | CV_Assert( src.cols >= win.width*2 + 5 && src.rows >= win.height*2 + 5 ); |
| 66 | CV_Assert( src.channels() == 1 ); |
| 67 | |
| 68 | Mat maskm(win_h, win_w, CV_32F), subpix_buf(win_h+2, win_w+2, CV_32F); |
| 69 | float* mask = maskm.ptr<float>(); |
| 70 | |
| 71 | for( i = 0; i < win_h; i++ ) |
| 72 | { |
| 73 | float y = (float)(i - win.height)/win.height; |
| 74 | float vy = std::exp(x: -y*y); |
| 75 | for( j = 0; j < win_w; j++ ) |
| 76 | { |
| 77 | float x = (float)(j - win.width)/win.width; |
| 78 | mask[i * win_w + j] = (float)(vy*std::exp(x: -x*x)); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // make zero_zone |
| 83 | if( zeroZone.width >= 0 && zeroZone.height >= 0 && |
| 84 | zeroZone.width * 2 + 1 < win_w && zeroZone.height * 2 + 1 < win_h ) |
| 85 | { |
| 86 | for( i = win.height - zeroZone.height; i <= win.height + zeroZone.height; i++ ) |
| 87 | { |
| 88 | for( j = win.width - zeroZone.width; j <= win.width + zeroZone.width; j++ ) |
| 89 | { |
| 90 | mask[i * win_w + j] = 0; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // do optimization loop for all the points |
| 96 | for( int pt_i = 0; pt_i < count; pt_i++ ) |
| 97 | { |
| 98 | Point2f cT = corners[pt_i], cI = cT; |
| 99 | CV_Assert( Rect(0, 0, src.cols, src.rows).contains(cT) ); |
| 100 | int iter = 0; |
| 101 | double err = 0; |
| 102 | |
| 103 | do |
| 104 | { |
| 105 | Point2f cI2; |
| 106 | double a = 0, b = 0, c = 0, bb1 = 0, bb2 = 0; |
| 107 | |
| 108 | getRectSubPix(image: src, patchSize: Size(win_w+2, win_h+2), center: cI, patch: subpix_buf, patchType: subpix_buf.type()); |
| 109 | const float* subpix = &subpix_buf.at<float>(i0: 1,i1: 1); |
| 110 | |
| 111 | // process gradient |
| 112 | for( i = 0, k = 0; i < win_h; i++, subpix += win_w + 2 ) |
| 113 | { |
| 114 | double py = i - win.height; |
| 115 | |
| 116 | for( j = 0; j < win_w; j++, k++ ) |
| 117 | { |
| 118 | double m = mask[k]; |
| 119 | double tgx = subpix[j+1] - subpix[j-1]; |
| 120 | double tgy = subpix[j+win_w+2] - subpix[j-win_w-2]; |
| 121 | double gxx = tgx * tgx * m; |
| 122 | double gxy = tgx * tgy * m; |
| 123 | double gyy = tgy * tgy * m; |
| 124 | double px = j - win.width; |
| 125 | |
| 126 | a += gxx; |
| 127 | b += gxy; |
| 128 | c += gyy; |
| 129 | |
| 130 | bb1 += gxx * px + gxy * py; |
| 131 | bb2 += gxy * px + gyy * py; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | double det=a*c-b*b; |
| 136 | if( fabs( x: det ) <= DBL_EPSILON*DBL_EPSILON ) |
| 137 | break; |
| 138 | |
| 139 | // 2x2 matrix inversion |
| 140 | double scale=1.0/det; |
| 141 | cI2.x = (float)(cI.x + c*scale*bb1 - b*scale*bb2); |
| 142 | cI2.y = (float)(cI.y - b*scale*bb1 + a*scale*bb2); |
| 143 | err = (cI2.x - cI.x) * (cI2.x - cI.x) + (cI2.y - cI.y) * (cI2.y - cI.y); |
| 144 | // if new point is out of image, leave previous point as the result |
| 145 | if( !Rect(0, 0, src.cols, src.rows).contains(pt: cI2) ) |
| 146 | break; |
| 147 | cI = cI2; |
| 148 | } |
| 149 | while( ++iter < max_iters && err > eps ); |
| 150 | |
| 151 | // if new point is too far from initial, it means poor convergence. |
| 152 | // leave initial point as the result |
| 153 | if( fabs( x: cI.x - cT.x ) > win.width || fabs( x: cI.y - cT.y ) > win.height ) |
| 154 | cI = cT; |
| 155 | |
| 156 | corners[pt_i] = cI; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | |
| 161 | CV_IMPL void |
| 162 | cvFindCornerSubPix( const void* srcarr, CvPoint2D32f* _corners, |
| 163 | int count, CvSize win, CvSize zeroZone, |
| 164 | CvTermCriteria criteria ) |
| 165 | { |
| 166 | if(!_corners || count <= 0) |
| 167 | return; |
| 168 | |
| 169 | cv::Mat src = cv::cvarrToMat(arr: srcarr), corners(count, 1, CV_32FC2, _corners); |
| 170 | cv::cornerSubPix(image: src, corners: corners, win, zeroZone, criteria); |
| 171 | } |
| 172 | |
| 173 | /* End of file. */ |
| 174 | |