Lar*_*bar 10 c++ opencv edge-detection
我需要从太空中探测太阳.
这些是输入图像的示例:

我在Morphologic过滤后得到了这样的结果(open两次操作)

这是此处理的算法代码:
// 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);
Run Code Online (Sandbox Code Playgroud)
对于这么简单的任务,处理不是太重了吗?如何找到太阳的中心?如果我找到白点,我会发现大地球的白点(第一个示例图像上的左上角)
请告诉我,我的进一步行动是为了探测太阳.
更新1:
尝试centroid按公式获取算法:{x,y} = {M10/M00, M01/M00}
CvMoments moments;
cvMoments(dst, &moments, 1);
double m00, m10, m01;
m00 = cvGetSpatialMoment(&moments, 0,0);
m10 = cvGetSpatialMoment(&moments, 1,0);
m01 = cvGetSpatialMoment(&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);
Run Code Online (Sandbox Code Playgroud)
地球在照片中的地方,我得到了这样的结果:

因此,质心在地球上.:(
更新2:
尝试cvHoughCircles:
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* circles = cvHoughCircles(dst, storage, CV_HOUGH_GRADIENT, 12,
dst->width/2, 255, 100, 0, 35);
if ( circles->total > 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 );
}
Run Code Online (Sandbox Code Playgroud)

第一个例子:宾果,但第二个 - 没有;(
我尝试了不同的配置cvHoughCircles()- 找不到适合我每个示例照片的配置.
UPDATE3:
matchTemplate方法对我有效(回应mevatron).它适用于大量测试.
mev*_*ron 10
尝试一个简单的matchTemplate方法怎么样?我用过这个模板图片:

并且,它检测到我试过的3张太阳图像中的3张:
这应该是有效的,因为圆圈(在你的情况下是太阳)是旋转不变的,并且因为你离太阳太远,它应该是大致尺度不变的.因此,模板匹配在这里可以很好地工作.
最后,这是我用来执行此操作的代码:
#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;
}
Run Code Online (Sandbox Code Playgroud)
希望你觉得有帮助!
| 归档时间: |
|
| 查看次数: |
11445 次 |
| 最近记录: |