python opencv-finding circle(Sun),图片中心的圆心坐标 [英] python opencv-finding circle (Sun) , coordinates of center the circle from picture

查看:1848
本文介绍了python opencv-finding circle(Sun),图片中心的圆心坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的,还有一些新手编程。

I am new here and a little bit newbie in programming.

我有一个问题。我在bmp文件和16位有太阳的图片。图片看起来像黑色背景的白色圆圈。

I have one question. I have picture of Sun in bmp file and 16 bit. The picture look as white circle with black backround.

我想找到一个圆圈并在x,y坐标中标识它的中心。

I want to find a circle and identify its center in x,y coordinates.

我有这个脚本

import cv
import numpy as np




orig = cv.LoadImage('sun0016.bmp')

grey_scale = cv.CreateImage(cv.GetSize(orig), 8, 1)
processed = cv.CreateImage(cv.GetSize(orig), 8, 1)

cv.Smooth(orig, orig, cv.CV_GAUSSIAN, 5, 5)
cv.CvtColor(orig, grey_scale, cv.CV_RGB2GRAY)
cv.Erode(grey_scale, processed, None, 10)
cv.Dilate(processed, processed, None, 10)
cv.Canny(processed, processed, 5, 70, 3)
cv.Smooth(processed, processed, cv.CV_GAUSSIAN, 15, 15)

storage = cv.CreateMat(orig.width, 1, cv.CV_32FC3)


cv.HoughCircles(processed, storage, cv.CV_HOUGH_GRADIENT, 1, 16.0, 10, 140)

for i in range(0, len(np.asarray(storage))):
    print "circle #%d" %i
    Radius = int(np.asarray(storage)[i][0][2])
    x = int(np.asarray(storage)[i][0][0])
    y = int(np.asarray(storage)[i][0][1])
    center = (x, y)
    print x,y

    cv.Circle(orig, center, 1, cv.CV_RGB(0, 255, 0), 1, 8, 0)
    cv.Circle(orig, center, Radius, cv.CV_RGB(255, 0, 0), 1, 8, 0)

    cv.Circle(processed, center, 1, cv.CV_RGB(0, 0, 0), -1, 8, 0)
    cv.Circle(processed, center, Radius, cv.CV_RGB(255, 0, 0), 3, 8, 0)

cv.ShowImage("sun0016", orig)
cv.ShowImage("processed", processed)
cv_key = cv.WaitKey(0)

当我跑步时,我发现太阳的边缘是圆心,但是非常不准确。
请知道您为精确搜索圈设置参数HoughCircles模块。
谢谢

And when I run this I find edge of Sun which is circle with center but very inaccurately. Pls know you setting of parameters HoughCircles module for precise search circles. Thanks

推荐答案

这里是我的问题的解决方案

here is solution of my problem

import numpy as np
import cv2

im = cv2.imread('sun0016.bmp')
height, width, depth = im.shape
print height, width, depth
thresh = 132
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(imgray,(5,5),0)
edges = cv2.Canny(blur,thresh,thresh*2)
contours, hierarchy = cv2.findContours(edges,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
cv2.drawContours(im,contours,-1,(0,255,0),-1)

#centroid_x = M10/M00 and centroid_y = M01/M00
M = cv2.moments(cnt)
x = int(M['m10']/M['m00'])
y = int(M['m01']/M['m00'])
print x,y
print width/2.0,height/2.0
print width/2-x,height/2-y


cv2.circle(im,(x,y),1,(0,0,255),2)
cv2.putText(im,"center of Sun contour", (x,y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255))
cv2.circle(im,(width/2,height/2),1,(255,0,0),2)
cv2.putText(im,"center of image", (width/2,height/2), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0))
cv2.imshow('contour',im)
cv2.waitKey(0)

这篇关于python opencv-finding circle(Sun),图片中心的圆心坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆