[英]Dividing a circle into 12 equal parts using OpenCV and Python
Using Hough Transform, I am drawing circles on the target, here is the code and the result使用霍夫变换,我在目标上画圈,这是代码和结果
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()
Now I want to divide the circle made by the hough transform into 12 equal parts.现在我想把霍夫变换形成的圆分成12等份。 Anyone knows how to do it?
有谁知道该怎么做?
I did an attempt but it is far from perfect and not what I wanted to do but still here it is我做了一个尝试,但它远非完美,也不是我想做的,但它仍然在这里
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.