使用python和opencv检测图像中

使用python和opencv检测图像中

本文介绍了使用python和opencv检测图像中的局部圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我拍摄了约220张日全食的图像,并计划将这次活动的延时动画放在一起.不出所料,部分黯淡的Sun的图像略有跳动,在制作动画之前,我需要记录镜头.

I took about 220 images of the partial solar eclipse today and plan to put together a timelapse animation of the event. As expected the image of the partially eclipsed Sun jumps around a bit and I need to register the shots before making the animation.

以下是示例照片:

http://www.trivalleystargazers.org/gert/sofi_141023/sofi.htm

我想将图像放在太阳上居中,这显然是日食期间的一个圆弧段.我猜想月亮会干扰算法(我不想集中在月亮上).我对Python有一些了解,而对opencv没有任何了解.

I would like to center the images on the Sun which is obviously a segment of a circle during the eclipse. I guess the Moon would be a distraction for the algorithm (I don't want to center on the Moon). I have some knowledge on Python and none on opencv.

是否有一种简单的方法可以在图像中找到太阳并将其居中到大约. 1像素的精度? opencv + python完全是正确的方法吗?是否有一些特殊的技巧可以解决,以获得最佳结果?

Is there an easy way to find the Sun in the images and center it to approx. 1pixel accuracy? Is opencv + python the proper approach at all? Are there particular tricks to work out to get to the best result?

感谢&晴朗的天空,格特

Thanks & Clear Skies,Gert

推荐答案

您可以尝试以下方法:

  • 阈值图像
  • 获得最大轮廓
  • 找到包围该轮廓的最小面积圆

找到中心和半径后,注册起来会更容易.如果快照之间的半径不同,则必须在注册阶段将所有圆的大小调整为预定义的大小,并相应地调整中心.

Once you find the center and radius, it'll be easier to register. If the radii are different between snapshots, you'll have to resize all circles to a predefined size and adjust the center accordingly in the registration phase.

我在OpenCV和C ++中尝试过.

I tried this in OpenCV and C++.

    Mat im = imread(INPUT_FOLDER_PATH + string("SoFi_400_20141023_163450.jpg"));

    Mat gray;
    cvtColor(im, gray, CV_BGR2GRAY);

    Mat bw;
    threshold(gray, bw, 0, 255, CV_THRESH_BINARY|CV_THRESH_OTSU);

    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;
    findContours(bw, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
    /* in actual implementation you'll have to find the largest contour.
    here i'm just assuming i get one and it's the largest*/
    for(int idx = 0; idx >= 0; idx = hierarchy[idx][0])
    {
        Point2f center;
        float radius;
        minEnclosingCircle(contours[idx], center, radius);
        cout << idx << " (" << center.x << ", " << center.y << ") : " << radius << endl;

        circle(im, Point(center.x, center.y), radius, Scalar(0, 255, 255), 2);
    }

    imshow("", im);
    waitKey();

一些结果:

这篇关于使用python和opencv检测图像中的局部圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 08:12