我尝试了Estimations:
2 -> 1: [ 527.15670903 222.57196904]
3 -> 1: [ 527.21339222 221.86819147]
4 -> 1: [ 527.63122722 222.30614892]
Avg loc: [ 527.33377616 222.24876981]
Est loc: [527 222]
True loc: [527 222]
但有时会返回5个,有时会返回6个数字。我希望它一直是6的长度。我更喜欢它的格式如import numpy as np
import cv2
# read images, taken from http://kahlan.eps.surrey.ac.uk/featurespace/web/
img1 = cv2.imread("img1.png", 1)
img2 = cv2.imread("img2.png", 1)
img3 = cv2.imread("img3.png", 1)
img4 = cv2.imread("img4.png", 1)
# true locations of the chicken's crossed eye (labeled myself)
loc1 = np.array([527, 222, 1])
loc2 = np.array([449, 241, 1])
loc3 = np.array([476, 275, 1])
loc4 = np.array([385, 236, 1])
# define ground truth homographies, also from http://kahlan.eps.surrey.ac.uk/featurespace/web/
H12 = np.array([
[8.7976964e-01, 3.1245438e-01, -3.9430589e+01],
[-1.8389418e-01, 9.3847198e-01, 1.5315784e+02],
[1.9641425e-04, -1.6015275e-05, 1.0000000e+00]])
H13 = np.array([
[7.6285898e-01, -2.9922929e-01, 2.2567123e+02],
[3.3443473e-01, 1.0143901e+00, -7.6999973e+01],
[3.4663091e-04, -1.4364524e-05, 1.0000000e+00]])
H14 = np.array([
[6.6378505e-01, 6.8003334e-01, -3.1230335e+01],
[-1.4495500e-01, 9.7128304e-01, 1.4877420e+02],
[4.2518504e-04, -1.3930359e-05, 1.0000000e+00]])
# need the homographies going the other direction
H21 = np.linalg.inv(H12)
H31 = np.linalg.inv(H13)
H41 = np.linalg.inv(H14)
# ensure they are homogeneous by dividing by the last entry
H21 = H21/H21[-1,-1]
H31 = H31/H31[-1,-1]
H41 = H41/H41[-1,-1]
# warp the locations loc2, loc3, loc4 to the coordinates of img1
est21 = np.matmul(H21, loc2)
est31 = np.matmul(H31, loc3)
est41 = np.matmul(H41, loc4)
# make homogeneous, toss the final 1
est21 = est21[:-1]/est21[-1]
est31 = est31[:-1]/est31[-1]
est41 = est41[:-1]/est41[-1]
# remove the last coordinate, take an average
avgest = (est21 + est31 + est41)/3
estloc = np.around(avgest).astype(int)
# output
print("Estimations:"
"\n2 -> 1: ", est21,
"\n3 -> 1: ", est31,
"\n4 -> 1: ", est41,
"\nAvg loc: ", avgest,
"\nEst loc: ", estloc,
"\nTrue loc: ", loc1[:-1])
# show images
cv2.circle(img1, (estloc[0], estloc[1]), 2, (0,0,255), -1) # filled
cv2.circle(img1, (estloc[0], estloc[1]), 20, (255,255,255)) # outline
cv2.imshow('img1-est', img1)
cv2.waitKey(0)
cv2.circle(img2, (loc2[0], loc2[1]), 2, (0,0,255), -1) # filled
cv2.circle(img2, (loc2[0], loc2[1]), 20, (255,255,255)) # outline
cv2.imshow('img2-loc', img2)
cv2.waitKey(0)
cv2.circle(img3, (loc3[0], loc3[1]), 2, (0,0,255), -1) # filled
cv2.circle(img3, (loc3[0], loc3[1]), 20, (255,255,255)) # outline
cv2.imshow('img3-log', img3)
cv2.waitKey(0)
cv2.circle(img4, (loc4[0], loc4[1]), 2, (0,0,255), -1) # filled
cv2.circle(img4, (loc4[0], loc4[1]), 20, (255,255,255)) # outline
cv2.imshow('img4-log', img4)
cv2.waitKey(0)
cv2.destroyAllWindows()
而不使用像SecureRandom.random_number(9**6)
这样的语法,因此在我的控制器测试中更容易被删除。
答案 0 :(得分:4)
你可以用数学做到这一点:
(SecureRandom.random_number(9e5) + 1e5).to_i
然后验证:
100000.times.map do
(SecureRandom.random_number(9e5) + 1e5).to_i
end.map { |v| v.to_s.length }.uniq
# => [6]
这将产生100000..999999范围内的值:
10000000.times.map do
(SecureRandom.random_number(9e5) + 1e5).to_i
end.minmax
# => [100000, 999999]
如果您需要更简洁的格式,只需将其转换为方法:
def six_digit_rand
(SecureRandom.random_number(9e5) + 1e5).to_i
end
答案 1 :(得分:2)
要生成一个随机的6位数字字符串:
copy_to
这是正在发生的事情的更多细节,通过将单行分成多行并解释变量来显示:
# This generates a 6-digit string, where the
# minimum possible value is "000000", and the
# maximum possible value is "999999"
SecureRandom.random_number(10**6).to_s.rjust(6, '0')
答案 2 :(得分:0)
SecureRandom.random_number(n)给出0到n之间的随机值。你可以使用rand函数来实现它。
2.3.1 :025 > rand(10**5..10**6-1)
=> 742840
rand(a..b)给出a和b之间的随机数。在这里,你总是得到一个介于10 ^ 5和10 ^ 6-1之间的6位数随机数。