title: OpenCV C++ code to Obj-C++
body: <p>I was suggested an algorithm to prepare an image for OCR, the code giving to me is great! However it is not compatible with the iOS build of OpenCV it seems, there are a few different naming conventions and I am having a hard time converting the code to Obj-C++.
  Could someone rewrite it for Obj-C++?</p>
  
  <p>Here is the original code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdarg.h&gt;
  #include "opencv2/opencv.hpp"
  #include "fstream"
  #include "iostream"
  using namespace std;
  using namespace cv;
  
  //-----------------------------------------------------------------------------------------------------
  // 
  //-----------------------------------------------------------------------------------------------------
  void CalcBlockMeanVariance(Mat&amp; Img,Mat&amp; Res,float blockSide=21) // blockSide - the parameter (set greater for larger font on image)
  {
      Mat I;
      Img.convertTo(I,CV_32FC1);
      Res=Mat::zeros(Img.rows/blockSide,Img.cols/blockSide,CV_32FC1);
      Mat inpaintmask;
      Mat patch;
      Mat smallImg;
      Scalar m,s;
  
      for(int i=0;i&lt;Img.rows-blockSide;i+=blockSide)
      {       
          for (int j=0;j&lt;Img.cols-blockSide;j+=blockSide)
          {
              patch=I(Range(i,i+blockSide+1),Range(j,j+blockSide+1));
              cv::meanStdDev(patch,m,s);
              if(s[0]&gt;0.01) // Thresholding parameter (set smaller for lower contrast image)
              {
                  Res.at&lt;float&gt;(i/blockSide,j/blockSide)=m[0];
              }else
              {
                  Res.at&lt;float&gt;(i/blockSide,j/blockSide)=0;
              }           
          }
      }
  
      cv::resize(I,smallImg,Res.size());
  
      cv::threshold(Res,inpaintmask,0.02,1.0,cv::THRESH_BINARY);
  
      Mat inpainted;
      smallImg.convertTo(smallImg,CV_8UC1,255);
  
      inpaintmask.convertTo(inpaintmask,CV_8UC1);
      inpaint(smallImg, inpaintmask, inpainted, 5, INPAINT_TELEA);
  
      cv::resize(inpainted,Res,Img.size());
      Res.convertTo(Res,CV_32FC1,1.0/255.0);
  
  }
  //-----------------------------------------------------------------------------------------------------
  // 
  //-----------------------------------------------------------------------------------------------------
  int main( int argc, char** argv )
  {
      namedWindow("Img");
      namedWindow("Edges");
      //Mat Img=imread("D:\\ImagesForTest\\BookPage.JPG",0);
      Mat Img=imread("Test2.JPG",0);
      Mat res;
      Img.convertTo(Img,CV_32FC1,1.0/255.0);
      CalcBlockMeanVariance(Img,res); 
      res=1.0-res;
      res=Img+res;
      imshow("Img",Img);
      cv::threshold(res,res,0.85,1,cv::THRESH_BINARY);
      cv::resize(res,res,cv::Size(res.cols/2,res.rows/2));
      imwrite("result.jpg",res*255);
      imshow("Edges",res);
      waitKey(0);
  
      return 0;
  }
  </code></pre>
  
  <p>My attempt: (gives back blue image with black spots)</p>
  
  <pre><code>void CalcBlockMeanVariance(cv::Mat&amp; Img,cv::Mat&amp; Res,float blockSide=21) // blockSide - the parameter (set greater for larger font on image)
  {
      cv::Mat I;
      Img.convertTo(I,CV_32FC1);
      Res=cv::Mat::zeros(Img.rows/blockSide,Img.cols/blockSide,CV_32FC1);
      cv::Mat inpaintmask;
      cv::Mat patch;
      cv::Mat smallImg;
      cv::Scalar m,s;
  
      for(int i=0;i&lt;Img.rows-blockSide;i+=blockSide)
      {
          for (int j=0;j&lt;Img.cols-blockSide;j+=blockSide)
          {
               patch=I(cv::Rect(j,i,blockSide,blockSide));
              cv::meanStdDev(patch,m,s);
              if(s[0]&gt;0.01) // Thresholding parameter (set smaller for lower contrast image)
              {
                  Res.at&lt;float&gt;(i/blockSide,j/blockSide)=m[0];
              }else
              {
                  Res.at&lt;float&gt;(i/blockSide,j/blockSide)=0;
              }
          }
      }
  
      cv::resize(I,smallImg,Res.size());
  
      cv::threshold(Res,inpaintmask,0.02,1.0,CV_THRESH_BINARY);
  
      cv::Mat inpainted;
      smallImg.convertTo(smallImg,CV_8UC1,255);
  
      inpaintmask.convertTo(inpaintmask,CV_8UC1);
      inpaint(smallImg, inpaintmask, inpainted, 5, CV_INPAINT_TELEA);
  
      cv::resize(inpainted,Res,Img.size());
      Res.convertTo(Res,CV_32FC1,1.0/255.0);
  
  }
  </code></pre>
  
  <p>Calling the method.</p>
  
  <pre><code>_img = [self cvMatFromUIImage:_endImage];
  cv::cvtColor(_img, _img, CV_RGB2GRAY);
  _img.convertTo(_img, CV_32FC1, 1.0/255.0);
  CalcBlockMeanVariance(_img, _res);
  _res = 1.0 - _res;
  _res = _img + _res;
  cv::threshold(_res,_res,0.85,1,cv::THRESH_BINARY);
  cv::resize(_res,_res,cv::Size(_res.cols/2,_res.rows/2));
  
  _endImage = [self UIImageFromMat:_res];
  </code></pre>