2016-11-07 85 views
0

我正在尝试在Python和Kivy中制作级联培训应用程序,您可以在其中轻松加载图像并使用多点触控来放大和旋转图像,并用双点触摸指示要将图像裁剪掉以保存图像的对象他们在一个单独的文件夹中以便稍后用于训练级联分类器。我目前面临的问题是,我只能得到窗口的坐标,我无法使用to_parant将其转换为图像的坐标等任何建议来解决这个问题将非常受欢迎!Python - Kivy级联训练:如何获取相对于图像的触摸坐标?

if touch.is_double_tap: 
     user_textinput = self.ids['user_textinput'] # change the text in the box where the user can put in the file directory by the created image name 
     image = cv2.imread(user_textinput.text) 
     x = int(touch.x) 
     y = int(touch.y) 
     w_double = 30 
     h_double = 30 

     crop_img = image[x-w_double/2:y-h_double/2, w_double:h_double] # Crop from x, y, w, h 
     cv2.imwrite("positive.jpg", crop_img) 

     cv2.rectangle(image,(x-w_double/2,y-h_double/2),(x+w_double/2,y+h_double/2),(255,255,255),3)    
     feedback_img_dir = 'product_pictures/feedback_' + str(time.strftime("%Y%m%d%H%M%S")) + ".jpg" # create name for image with a time stamp 
     cv2.imwrite(feedback_img_dir, image) 
     print "feedback_img saved as " + feedback_img_dir 

     user_textinput = self.ids['user_textinput'] # change the text in the box where the user can put in the file directory by the created image name 
     user_textinput.text = str(feedback_img_dir) 
    else: 
     return super(GUI, self).on_touch_down(touch) # assures that if noth double click or ... the other withgets can still be used 

回答

0

To_parent(朋友)只需要从一个系统改变坐标到另一个一样,当如RelativeLayout的,或滚动型使用,保存的是,图像的位置坐标系中它接收触摸事件,其位置与其相关,因此您只需将它的位置减去触摸位置即可获得相对位置。

X, Y = touch.x - image.x, touch.y - image.y

或者,你可以窝在你一个RelativeLayout的图像直接获取转变。

+0

我可以同意,如果图像将以比例1显示,这将工作,但我目前在散点图中显示图像,以启用双触摸缩放和旋转以及单点触摸平移。任何建议如何在这种情况下获得正确的坐标? – JEPE

+0

好吧,如果你使用Scatter,它已经在正确的位置上触摸了,如果你想从另一个widget检查它的坐标系,那么你确实需要使用scatter.to_widget()方法。但是,我不确定裁剪会如何工作,特别是如果涉及到旋转,更完整的代码版本将会有所帮助,因此您可以看到Scatter和您提供的代码是如何相关的。一个更好的解决方案可能是使用FBO来渲染父级坐标系中散点的当前状态,然后裁剪这个散点。 – Tshirtman