python - 使用 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等份。有谁知道该怎么做?
解决方案
我做了一个尝试,但它远非完美,也不是我想做的,但它仍然在这里
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()
推荐阅读
- excel - Excel 公式 - 有没有办法在更改为下一个连续查询单元格之前对第 n 个单元格的相同查询单元格执行 if 语句
- docker - 运行 apk add --update --no-cache py3-arrow not wokring in alpine docker image
- javascript - 我如何获得输入框的值
- javascript - 如何使用 react.js 获取格式 dd/MM/yyyy?
- string - 在 VB.NET 中将可为空的双精度变量转换为字符串
- python - 如何告诉 SymPy 变量是时间的函数?
- kotlin - 如何使用 kotlin 和 refelction 迭代嵌套数据类
- postgresql - 通过复合键删除超过限制的记录
- javascript - 如何有效地用js追加元素?
- c# - 如何检查用户是否在 c# 中以管理员身份运行应用程序?