我还没有完全测试过代码,但乍一看,它似乎可以工作。我会把它贴在这里,以防将来有人需要类似的东西。几乎不可能对所有可能的情况进行记录和评论,对此感到抱歉,请按原样使用代码:
def getAngle(x,y):
if x == 0:
if y>0:
return 90
else:
return 270
angle = np.arctan(y/x)*180/np.pi
if x>0:
if y<0:
angle+=360
else:
angle+=180
return angle
def getAngleRad(x,y):
if x == 0:
if y>0:
return 0.5*np.pi
else:
return 1.5*np.pi
angle = np.arctan(y/x)
if x>0:
if y<0:
angle+=2*np.pi
else:
angle+=np.pi
return angle
def circularMask(size=3, r=1, x_offset=0, y_offset=0):
x0 = y0 = size /2.0
x0 += x_offset
y0 -= y_offset
img = np.zeros((size,size))
for j in range(img.shape[0]):
y = y0-j
for i in range(img.shape[1]):
x = i-x0
d1 = np.sqrt(x**2 + y**2)
d2 = np.sqrt((x+1)**2 + y**2)
d3 = np.sqrt(x**2 + (y-1)**2)
d4 = np.sqrt((x+1)**2 + (y-1)**2)
corners_inside = 0
for d in [d1,d2,d3,d4]:
if d<r:
corners_inside+=1
if corners_inside==4:
img[j,i]=1
if corners_inside==3 or corners_inside==1:
if x>0 and y>0:
if corners_inside==3:
xp = x+1
yq = y
else:
xp = x
yq = y-1
xq = np.sqrt(r**2-yq**2)
yp = np.sqrt(r**2-xp**2)
traingle_area = 0.5*(xp-xq)*(yq-yp)
theta_p = getAngleRad(xp,yp)
theta_q = getAngleRad(xq,yq)
delta_theta = np.abs(theta_p - theta_q)
circle_segment_area = 0.5*(delta_theta-np.sin(delta_theta))*(r**2)
if corners_inside==3:
img[j,i]=1-traingle_area+circle_segment_area
else:
img[j,i]=traingle_area+circle_segment_area
if x<0 and y>0:
if corners_inside==3:
xp = x
yq = y
else:
xp = x+1
yq = y-1
xq = -np.sqrt(r**2-yq**2)
yp = np.sqrt(r**2-xp**2)
traingle_area = 0.5*(xq-xp)*(yq-yp)
theta_p = getAngleRad(xp,yp)
theta_q = getAngleRad(xq,yq)
delta_theta = np.abs(theta_p - theta_q)
circle_segment_area = 0.5*(delta_theta-np.sin(delta_theta))*(r**2)
if corners_inside==3:
img[j,i]=1-traingle_area+circle_segment_area
else:
img[j,i]=traingle_area+circle_segment_area
if x<0 and y<0:
if corners_inside==3:
xp = x
yq = y-1
else:
xp = x+1
yq = y
xq = -np.sqrt(r**2-yq**2)
yp = -np.sqrt(r**2-xp**2)
traingle_area = 0.5*(xq-xp)*(yp-yq)
theta_p = getAngleRad(xp,yp)
theta_q = getAngleRad(xq,yq)
delta_theta = np.abs(theta_p - theta_q)
circle_segment_area = 0.5*(delta_theta-np.sin(delta_theta))*(r**2)
if corners_inside==3:
img[j,i]=1-traingle_area+circle_segment_area
else:
img[j,i]=traingle_area+circle_segment_area
if x>0 and y<0:
if corners_inside==3:
xp = x+1
yq = y-1
else:
xp = x
yq = y
xq = np.sqrt(r**2-yq**2)
yp = -np.sqrt(r**2-xp**2)
traingle_area = 0.5*(xp-xq)*(yp-yq)
theta_p = getAngleRad(xp,yp)
theta_q = getAngleRad(xq,yq)
delta_theta = np.abs(theta_p - theta_q)
circle_segment_area = 0.5*(delta_theta-np.sin(delta_theta))*(r**2)
if corners_inside==3:
img[j,i]=1-traingle_area+circle_segment_area
else:
img[j,i]=traingle_area+circle_segment_area
if corners_inside==2:
angle = getAngle(x+0.5,y-0.5)
if angle<22.5 or angle>337.5:
yp = y
yq = y-1
xp = np.sqrt(r**2-yp**2)
xq = np.sqrt(r**2-yq**2)
trapezoid_area = (0.5*(xp+xq))-x
theta_p = np.arctan(yp/xp)
theta_q = np.arctan(yq/xq)
delta_theta = np.abs(theta_p - theta_q)
circle_segment_area = 0.5*(delta_theta-np.sin(delta_theta))*(r**2)
img[j,i] = trapezoid_area+circle_segment_area
if 22.5<angle<67.5:
if d1<r:
yp = y
yq = y-1
xp = np.sqrt(r**2-yp**2)
xq = np.sqrt(r**2-yq**2)
trapezoid_area = (0.5*(xp+xq))-x
else:
xp = x
xq = x+1
yp = np.sqrt(r**2-xp**2)
yq = np.sqrt(r**2-xq**2)
trapezoid_area = (0.5*(yp+yq))-y+1
theta_p = getAngleRad(xp,yp)
theta_q = getAngleRad(xq,yq)
delta_theta = np.abs(theta_p - theta_q)
circle_segment_area = 0.5*(delta_theta-np.sin(delta_theta))*(r**2)
img[j,i] = trapezoid_area+circle_segment_area
if 67.5<angle<112.5:
xp = x
xq = x+1
yp = np.sqrt(r**2-xp**2)
yq = np.sqrt(r**2-xq**2)
trapezoid_area = (0.5*(yp+yq))-y+1
theta_p = getAngleRad(xp,yp)
theta_q = getAngleRad(xq,yq)
delta_theta = np.abs(theta_p - theta_q)
circle_segment_area = 0.5*(delta_theta-np.sin(delta_theta))*(r**2)
img[j,i] = trapezoid_area+circle_segment_area
if 112.5<angle<157.5:
if d3< r:
xp = x+1
xq = x
yp = np.sqrt(r**2-xp**2)
yq = np.sqrt(r**2-xq**2)
trapezoid_area = (0.5*(yp+yq))-y+1
else:
yp = y
yq = y-1
xp = -np.sqrt(r**2-yp**2)
xq = -np.sqrt(r**2-yq**2)
trapezoid_area = x+1-(0.5*(xp+xq))
theta_p = getAngleRad(xp,yp)
theta_q = getAngleRad(xq,yq)
delta_theta = np.abs(theta_p - theta_q)
circle_segment_area = 0.5*(delta_theta-np.sin(delta_theta))*(r**2)
img[j,i] = trapezoid_area+circle_segment_area
if 157.5<angle<202.5:
yp = y
yq = y-1
xp = -np.sqrt(r**2-yp**2)
xq = -np.sqrt(r**2-yq**2)
trapezoid_area = x+1-(0.5*(xp+xq))
theta_p = getAngleRad(xp,yp)
theta_q = getAngleRad(xq,yq)
delta_theta = np.abs(theta_p - theta_q)
circle_segment_area = 0.5*(delta_theta-np.sin(delta_theta))*(r**2)
img[j,i] = trapezoid_area+circle_segment_area
if 202.5<angle<247.5:
if d4<r:
yp = y
yq = y-1
xp = -np.sqrt(r**2-yp**2)
xq = -np.sqrt(r**2-yq**2)
trapezoid_area = x+1-(0.5*(xp+xq))
else:
xp = x
xq = x+1
yp = -np.sqrt(r**2-xp**2)
yq = -np.sqrt(r**2-xq**2)
trapezoid_area = y-(0.5*(yp+yq))
theta_p = getAngleRad(xp,yp)
theta_q = getAngleRad(xq,yq)
delta_theta = np.abs(theta_p - theta_q)
circle_segment_area = 0.5*(delta_theta-np.sin(delta_theta))*(r**2)
img[j,i] = trapezoid_area+circle_segment_area
if 247.5<angle<292.5:
xp = x
xq = x+1
yp = -np.sqrt(r**2-xp**2)
yq = -np.sqrt(r**2-xq**2)
trapezoid_area = y-(0.5*(yp+yq))
theta_p = getAngleRad(xp,yp)
theta_q = getAngleRad(xq,yq)
delta_theta = np.abs(theta_p - theta_q)
circle_segment_area = 0.5*(delta_theta-np.sin(delta_theta))*(r**2)
img[j,i] = trapezoid_area+circle_segment_area
if 292<angle<337.5:
if d3<r:
yp = y
yq = y-1
xp = np.sqrt(r**2-yp**2)
xq = np.sqrt(r**2-yq**2)
trapezoid_area = (0.5*(xp+xq))-x
else:
xq = x
xp = x+1
yp = -np.sqrt(r**2-xp**2)
yq = -np.sqrt(r**2-xq**2)
trapezoid_area = y-(0.5*(yp+yq))
theta_p = getAngleRad(xp,yp)
theta_q = getAngleRad(xq,yq)
delta_theta = np.abs(theta_p - theta_q)
circle_segment_area = 0.5*(delta_theta-np.sin(delta_theta))*(r**2)
img[j,i] = trapezoid_area+circle_segment_area
return img
输出与问题中发布的值的预期结果相匹配:
编辑:我发现了一些错误并解决了它们(代码已更新),但请注意,我可能还没有发现更多错误。使用代码需要您自担风险。