How to detect the Sun from the space sky in OpenCv?

前端 未结 3 2005
庸人自扰
庸人自扰 2020-12-09 06:21

I need to detect the Sun from the space sky.

These are examples of the input images:

相关标签:
3条回答
  • 2020-12-09 06:29

    How about trying a simple matchTemplate approach. I used this template image:
    enter image description here

    And, it detected the 3 out of 3 of the sun images I tried: enter image description here enter image description here enter image description here

    This should work due to the fact that circles (in your case the sun) are rotationally invariant, and since you are so far away from the sun it should be roughly scale invariant as well. So, template matching will work quite nicely here.

    Finally, here is the code that I used to do this:

    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include <iostream>
    
    using namespace cv;
    using namespace std;
    
    int main(int argc, char* argv[])
    {
        /// Load image and template
        string inputName = "sun2.png";
        string outputName = "sun2_detect.png";
        Mat img   = imread( inputName, 1 );
        Mat templ = imread( "sun_templ.png", 1 );
    
        /// Create the result matrix
        int result_cols =  img.cols - templ.cols + 1;
        int result_rows = img.rows - templ.rows + 1;
    
        Mat result( result_cols, result_rows, CV_32FC1 );
    
        /// Do the Matching and Normalize
        matchTemplate(img, templ, result, CV_TM_CCOEFF);
        normalize(result, result, 0, 1, NORM_MINMAX, -1, Mat());
    
        Point maxLoc;
        minMaxLoc(result, NULL, NULL, NULL, &maxLoc);
    
        rectangle(img, maxLoc, Point( maxLoc.x + templ.cols , maxLoc.y + templ.rows ), Scalar(0, 255, 0), 2);
        rectangle(result, maxLoc, Point( maxLoc.x + templ.cols , maxLoc.y + templ.rows ), Scalar(0, 255, 0), 2);
    
        imshow("img", img);
        imshow("result", result);
    
        imwrite(outputName, img);
    
        waitKey(0);
    
        return 0;
    }
    

    Hope you find that helpful!

    0 讨论(0)
  • 2020-12-09 06:30

    Color Segmentation Approach

    Do a color segmentation on the images to identify objects on the black background. You may identify the sun according to its area (given this uniquely identifies it, resp. don't varies largely accross images). A more sophisticated approach could compute image moments, e.g. hu moments of the objects. See this page for these features.

    Use a classification algorithm of your choice to do the actual classification of the objects found. The most simple approach is to manually specify thresholds, resp. value ranges that turn out to work for all(most) of your object/image combinations.

    You may compute the actual position from the raw moments, as for the circular sun the position is equal to the center of mass

    Centroid: {x, y } = { M10/M00, M01/M00 }
    

    Edge Map Approach

    Another option would be a circle hough transformation of the edge map, this will hopefully return some candidate circles (by position and radius). You may select the sun-circle according to the radius you expect (if you are lucky there is at most one).

    0 讨论(0)
  • 2020-12-09 06:33

    A simple addition to your code is to filter out objects based on their size. If you always expect the earth to be much bigger than the sun, or the sun to have almost the same area in each picture, you can filter it by area.

    Try Blob detector for this task.

    And note that it may be good to apply a morphological opening/closing instead of simple erode or dilate, so your sun will have almost the same area before and after processing.

    0 讨论(0)
提交回复
热议问题