title: How to detect the Sun from the space sky in OpenCv?
body: <p>I need to detect the Sun from the space sky.</p>
  
  <p>These are examples of the input images:</p>
  
  <p><img src="https://i.stack.imgur.com/b2gUH.png" width="300">
  <img src="https://i.stack.imgur.com/A6Mvf.png" width="300"></p>
  
  <p>I've got such results after Morphologic filtering ( <code>open</code> operation for twice )</p>
  
  <p><img src="https://i.stack.imgur.com/Nquaz.png" width="300">
  <img src="https://i.stack.imgur.com/35cFH.png" width="300"></p>
  
  <p>Here's the algorithm code of this processing:</p>
  
  <pre><code>// Color to Gray
  cvCvtColor(image, gray, CV_RGB2GRAY);
  
  // color threshold
  cvThreshold(gray,gray,150,255,CV_THRESH_BINARY);
  
  // Morphologic open for 2 times
  cvMorphologyEx( gray, dst, NULL, CV_SHAPE_RECT, CV_MOP_OPEN, 2);
  </code></pre>
  
  <p>Isn't it too heavy processing for such a simple task? And how to find the center of the Sun? If I find white points, than I'll find white points of big Earth ( left top corner on first example image )</p>
  
  <p>Please advise me please my further action to detect the Sun.</p>
  
  <p><strong>UPDATE 1:</strong></p>
  
  <p>Trying algorithm of getting <code>centroid</code> by formula : <code>{x,y} = {M10/M00, M01/M00}</code></p>
  
  <pre><code>CvMoments moments;
  cvMoments(dst, &amp;moments, 1);
  double m00, m10, m01;
  
  m00 = cvGetSpatialMoment(&amp;moments, 0,0);
  m10 = cvGetSpatialMoment(&amp;moments, 1,0);
  m01 = cvGetSpatialMoment(&amp;moments, 0,1);
  
  // calculating centroid
  float centroid_x = m10/m00;
  float centroid_y = m01/m00;
  
      cvCircle( image, 
                cvPoint(cvRound(centroid_x), cvRound(centroid_y)), 
                50, CV_RGB(125,125,0), 4, 8,0);
  </code></pre>
  
  <p>And where Earth is in the photo, I got such a result:
  <img src="https://i.stack.imgur.com/c2Soq.png" width="500"></p>
  
  <p>So, centroid is on the Earth. :(</p>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>Trying <code>cvHoughCircles</code>:</p>
  
  <pre><code>CvMemStorage* storage = cvCreateMemStorage(0);
  CvSeq* circles = cvHoughCircles(dst, storage, CV_HOUGH_GRADIENT, 12, 
                                  dst-&gt;width/2, 255, 100, 0, 35);
  
  if ( circles-&gt;total &gt; 0 ) {
      // getting first found circle
      float* circle = (float*)cvGetSeqElem( circles, 0 ); 
  
      // Drawing:
      // green center dot
      cvCircle( image, cvPoint(cvRound(circle[0]),cvRound(circle[1])), 
            3, CV_RGB(0,255,0), -1, 8, 0 ); 
      // wrapping red circle
      cvCircle( image, cvPoint(cvRound(circle[0]),cvRound(circle[1])), 
          cvRound(circle[2]), CV_RGB(255,0,0), 3, 8, 0 ); 
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/cd68M.png" width="310">
  <img src="https://i.stack.imgur.com/Y5zUJ.png" width="310"></p>
  
  <p>First example: bingo, but the second - no ;(</p>
  
  <p>I've tried different configuration of <code>cvHoughCircles()</code> - couldn't find configuration to fit every my example photo.</p>
  
  <p><strong>UPDATE3:</strong></p>
  
  <p><code>matchTemplate</code> approach worked for me ( response of <code>mevatron</code> ). It worked with big number of tests.</p>