首页 > 解决方案 > 使用 OpenCV 和 Python 将一个圆分成 12 个相等的部分

问题描述

我有这张图片 目标

使用霍夫变换,我在目标上画圈,这是代码和结果

import cv2
import numpy as np
from matplotlib import pyplot as plt
import math

bgr_img = cv2.imread('16-Bit_ID-00001.jpg') # read as it is

if bgr_img.shape[-1] == 3:           # color image
    b,g,r = cv2.split(bgr_img)       # get b,g,r
    rgb_img = cv2.merge([r,g,b])     # switch it to rgb
    gray_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2GRAY)
else:
    gray_img = bgr_img

img = cv2.medianBlur(gray_img, 95)     # blur value acts as a filter
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,30,
                            param1=50,param2=50,minRadius=60,maxRadius=0)

circles = np.uint16(np.around(circles))

for i in circles[0,:]:
    # draw the outer circle
    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
    #sliceno = np.int32((math.pi + np.arctan2(Y, X)) * (N / (2 * math.pi)))

plt.subplot(121),plt.imshow(rgb_img)
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(cimg)
plt.title('Hough Transform'), plt.xticks([]), plt.yticks([])
plt.show()

我得到的结果是 霍夫变换的结果

现在我想把霍夫变换形成的圆分成12等份。有谁知道该怎么做?

标签: pythonopencvimage-processingcomputer-visionphotogrammetry

解决方案


我做了一个尝试,但它远非完美,也不是我想做的,但它仍然在这里

import cv2
import numpy as np
from matplotlib import pyplot as plt
import math

bgr_img = cv2.imread('16-Bit_ID-00001.jpg') # read as it is

if bgr_img.shape[-1] == 3:           # color image
    b,g,r = cv2.split(bgr_img)       # get b,g,r
    rgb_img = cv2.merge([r,g,b])     # switch it to rgb
    gray_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2GRAY)
else:
    gray_img = bgr_img

img = cv2.medianBlur(gray_img, 95)     # blur value acts as a filter
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,30,
                            param1=50,param2=50,minRadius=60,maxRadius=0)

circles = np.uint16(np.around(circles))
angle = 0
for i in circles[0,:]:
    # draw the outer circle
 cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
    # draw the center of the circle
 cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
    # dividing the circle into 12 equal parts
 (x, y), radius = (i[0],i[1]),i[2]

 radius = int(radius)
 angle = angle +30
 x_2 = int(round(x + radius * math.cos(angle * math.pi / 180.0)));
 y_2 = int(round(y + radius * math.sin(angle * math.pi / 180.0)));



 cv2.line(cimg, (i[0],i[1]),(x_2,y_2),(255,127,0),3,cv2.LINE_AA)
 angle = angle +30
 x_2 = int(round(x + radius * math.cos(angle * math.pi / 180.0)));
 y_2 = int(round(y + radius * math.sin(angle * math.pi / 180.0)));



 cv2.line(cimg, (i[0],i[1]),(x_2,y_2),(255,127,0),3,cv2.LINE_AA)

plt.subplot(121),plt.imshow(rgb_img)
plt.title('Input Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(cimg)
plt.title('Hough Transform'), plt.xticks([]), plt.yticks([])
plt.show()

这是结果 结果


推荐阅读