2016-11-07 69 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

相關問題