Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. # coding: UTF-8
  2.  
  3. import numpy as np
  4. import sys
  5. import math
  6. import cv2
  7. from time import sleep
  8.  
  9. mode = 1
  10. SELECT_VP = 1
  11. SELECT_REFLINE = 2
  12. SELECT_CLCLINE = 3
  13.  
  14. dragOn = False
  15. nVp = 0 #Nunber of vanishing point
  16. nVline = 0 #Nunber of vanishing line
  17.  
  18. wsize=5
  19. cnt=0
  20. filename = './man.jpg' #デフォルトで読み込むファイル名 defaut filename
  21.  
  22. xary = np.array([])
  23. yary = np.array([])
  24. refx = np.array([])
  25. refy = np.array([])
  26. rref = 0
  27. calcLinex = np.array([])
  28. calcLiney = np.array([])
  29. vlinePoint =[[],[]]
  30.  
  31. msg1 = ['Click 1st point:', 'Click 2nd point:', 'Click 3rd point:', 'Click 4th point:']
  32. msg2 = ['Input 1st coord(x,y):','Input 2nd coord(x,y):','Input 3rd coord(x,y):','Input 4th coord(x,y):']
  33.  
  34. #マウス操作のためのコールバック関数
  35. #Callback function for mouse
  36. def onMouse( event, u, v, flag, params ):
  37. #wname, img = params
  38. global dragOn
  39. global cnt
  40. global xary,yary,uary,vary
  41. global refx, refy
  42. global rref
  43. global calcLinex, calcLiney
  44. # if event == cv2.EVENT_MOUSEMOVE:
  45. """
  46. if dragOn == True:
  47. u = 2.0*np.pi*(x-sizex/2)#/float(sizex)
  48. v = 2.0*np.pi*(y-sizey/2)#/float(sizey)
  49. im = np.zeros((sizex,sizey,1), dtype = np.float32)
  50. MakeSinImage(im,u,v)
  51. cv2.imshow( 'sine', im )
  52. for j in range(y-wsize,y+wsize):
  53. for i in range(x-wsize,x+wsize):
  54. fil[j][i] = 1.0
  55. fil[sizey-j][sizex-i]=1.0
  56. #fil[y][x] = 1.0
  57. #fil[sizey-y][sizex-x] = 1.0
  58. cv2.imshow('filter',fil)
  59. Finv = Fimg * fil
  60. finv = np.fft.ifft2(np.fft.fftshift(Finv))
  61. finv = finv/255.0
  62. cv2.imshow('inv', np.real(finv))
  63. """
  64. # if event == cv2.EVENT_LBUTTONUP:
  65. # dragOn = False
  66. if event == cv2.EVENT_LBUTTONDOWN:
  67. dragOn = True
  68. # クリックしたときの動き
  69. # if mode == hoge 的な感じで分けたい
  70. cv2.circle(img, (u, v), 3, (0, 255, 0), -1)
  71. cv2.imshow('image', img)
  72. if mode == SELECT_VP:
  73. xary = np.append(xary, u)
  74. yary = np.append(yary, v)
  75. elif mode == SELECT_REFLINE:
  76. refx = np.append(refx, u)
  77. refy = np.append(refy, v)
  78. elif mode == SELECT_CLCLINE:
  79. calcLinex = np.append(calcLinex, u)
  80. calcLiney = np.append(calcLiney, v)
  81. else:
  82. pass
  83.  
  84. param = sys.argv
  85. mode = SELECT_VP
  86.  
  87. if len(param)==2:
  88. filename = param[1]
  89.  
  90. img = cv2.imread(filename)
  91. sizex=img.shape[0]
  92. sizey=img.shape[1]
  93.  
  94. # nVp = input('Input nunber of vanishing point : ')
  95. nVline = input('Input nunber of vanishig line : ')
  96.  
  97.  
  98. cv2.imshow('image', img)
  99. sleep(0.01)
  100.  
  101. cv2.setMouseCallback('image', onMouse)
  102. print('消失線ごとに2箇所クリックしてください')
  103. # print(msg1[cnt%4])
  104.  
  105. # 消失線を引くための点を選択
  106. while(1):
  107. if cv2.waitKey(20) & 0xFF == 27:
  108. break
  109. if bFlag==True:
  110. pass
  111.  
  112. # 消失線を引く、消失点を求める、VHを引く
  113. mode = SELECT_REFLINE
  114. print(xary)
  115. print(yary)
  116.  
  117. # y = ax + b
  118. a = np.array([])
  119. b = np.array([])
  120. for i in range(0, nVline * 2, 2):
  121. a = np.append(a, (yary[i + 1] - yary[i]) / (xary[i + 1] - xary[i]))
  122. print(a)
  123. b = np.append(b, (-a[i / 2] * xary[i] + yary[i]))
  124.  
  125. print('y = {}x + {}'.format(a[0], b[0]))
  126. print('y = {}x + {}'.format(a[1], b[1]))
  127.  
  128. v = np.array([(b[1] - b[0]) / (a[0] - a[1]),(a[0] * b[1] - a[1] * b[0]) / (a[0] - a[1])])
  129. print(v)
  130.  
  131. # カメラ高さHを求める
  132. cv2.line(img, (0, int(v[1])), (sizey, int(v[1])), (0, 0, 255), 3)
  133. cv2.imshow('image', img)
  134. print('基準の高さを選択してください')
  135.  
  136. # 既知の長さを選択
  137. while(1):
  138. if cv2.waitKey(20) & 0xFF == 27:
  139. break
  140. if bFlag==True:
  141. pass
  142.  
  143. rref = input('tall[mm] : ')
  144.  
  145.  
  146. # 求める長さを選択
  147. mode = SELECT_CLCLINE
  148. print('求めたい高さを選択してください')
  149.  
  150. while(1):
  151. if cv2.waitKey(20) & 0xFF == 27:
  152. break
  153. if bFlag==True:
  154. pass
  155.  
  156. tf = ((calcLinex[0] - calcLinex[1])**2 + (calcLiney[0] - calcLiney[1])**2)**0.5
  157. print(tf)
  158. hf = abs(calcLiney[1] - v[1])
  159. print(hf)
  160. hb = abs(refy[1] - v[1])
  161. print(hb)
  162. cb = ((refx[0] - refx[1])**2 + (refy[0] - refy[1])**2)**0.5
  163. print(cb)
  164. tv = ((calcLinex[0] - v[0])**2 + (calcLiney[0] - v[1])**2)**0.5
  165. cv = ((refx[0] - v[0])**2 + (refy[0] - v[1])**2)**0.5
  166.  
  167.  
  168. result = (tf / hf) * (hb / cb) * (cv / tv) * rref
  169.  
  170. print(result)
  171.  
  172. cv2.waitKey(0)
  173. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement