現在Pythonを勉強中の初学者になります。
足の画像上で2点を結び、その長さを測りたいと思っていたのですが
ユークリッド距離を使用したもののこれからどう実寸の距離を出せばよろしいでしょうか?
import cv2 import numpy as np from matplotlib import pyplot as plt from __future__ import print_function from skimage.feature import peak_local_max from skimage.morphology import watershed from scipy import ndimage from scipy.spatial import distance img = cv2.imread(*********, 1) height = img.shape[0] width = img.shape[1] img1 = cv2.resize(img , (int(width*0.5), int(height*0.5))) class PointList(): def __init__(self, npoints): self.npoints = npoints self.ptlist = np.empty((npoints, 2), dtype=int)#2次元配列 self.pos = 0 def add(self, x, y): if self.pos < self.npoints: f=self.ptlist[ self.pos,:] = [x, y] print("f",f) self.pos += 1 return True return False def onMouse(event, x, y, flag, params): wname, img, ptlist = params if event == cv2.EVENT_MOUSEMOVE: # マウスが移動したときにx線とy線を更新する img2 = np.copy(img) h, w = img2.shape[0], img2.shape[1] #縦、横 cv2.line(img2, (x, 0), (x, h - 1), (255, 0, 0)) cv2.line(img2, (0, y), (w - 1, y), (255, 0, 0)) cv2.imshow(wname, img2) if event == cv2.EVENT_LBUTTONDOWN: # レフトボタンをクリックしたとき、ptlist配列にx,y座標を格納する if ptlist.add(x, y): print('[%d] ( %d, %d )' % (ptlist.pos - 1, x, y)) cv2.circle(img1, (x, y), 3, (0, 255, 0), 3) cv2.imshow(wname, img1) else: print('All points have selected. Press ESC-key.') if(ptlist.pos == ptlist.npoints): print("ptlist.ptlist",ptlist.ptlist) xx=cv2.line(img, (ptlist.ptlist[0][0], ptlist.ptlist[0][1]), (ptlist.ptlist[1][0], ptlist.ptlist[1][1]), (0, 255, 0), 3) aa=np.array([ptlist.ptlist[0][0]]) bb=np.array([ptlist.ptlist[0][1]]) cc=np.array([ptlist.ptlist[1][0]]) dd=np.array([ptlist.ptlist[1][1]]) for a in aa: print(a) for b in bb: print(b) for c in cc: print(c) for d in dd: print(d) aca=np.array([a,b]) bcb=np.array([c,d]) df=bcb - aca print(np.linalg.norm(df)) if __name__ == '__main__': wname = "MouseEven" cv2.namedWindow(wname) npoints = 2 ptlist = PointList(npoints) cv2.setMouseCallback(wname, onMouse, [wname, img1, ptlist]) cv2.waitKey() cv2.destroyAllWindows()
回答2件
あなたの回答
tips
プレビュー