2017-08-01 10 views
1

Ich benutze OpenCV in Python. Ich habe den folgenden Code auswählen und kopieren einen Abschnitt eines Hauptbildes in ein Teilbild verwendet:Übersetzen Sie Pixel von beschnittenen Bildausschnitt auf Originalbild OpenCV

boundingBoxRotatedRect = cv2.minAreaRect(boxPoints) 
if boundingBoxRotatedRect[2] < -45: 
    boundingBoxRotatedRect = (boundingBoxRotatedRect[0], (boundingBoxRotatedRect[1][1],boundingBoxRotatedRect[1][0]), boundingBoxRotatedRect[2] + 90) 
M = cv2.getRotationMatrix2D(boundingBoxRotatedRect[0], boundingBoxRotatedRect[2], 1.0) 
size = np.int0(boundingBoxRotatedRect[1]) 
size = (size[0],size[1]) 
dst = cv2.warpAffine(mainImage, M, (mainImage.shape[1], mainImage.shape[0])) 
subImage = cv2.getRectSubPix(dst, size, boundingBoxRotatedRect[0]) 

Wo boxPoints ist ein Array von 4 Punkten der Begrenzungsrahmen um den Bereich von der abgeschnitten werden, bilden main image, boundingBoxRotatedRect ist die gleiche Box, die als rotiertes rect-Objekt dargestellt wird, M ist die Rotationsmatrix, Größe ist die Breite/Höhe der Bounding Box, mainImage ist das Hauptbild, von dem abgeschnitten wird und subImage ist letztendlich das letzte Bild wurde aus dem Hauptbild abgeschnitten. Das Bild unten erklärt weiter, was passiert.

Explanation Image

Meine Frage ist: Wenn ich OpenCV Zeichenfunktionen verwenden, um die Unter-Bild zu bearbeiten, wie kann ich wieder auf die entsprechenden Pixel des Mainimage die gleichen Zeichnungen setzen? So zum Beispiel (wenn ich die Bildformen in meinem erklärten Erklärungsbild verwende) zeichne ich ein aufrechtes Smiley-Gesicht auf dem subImage, wie kann ich das in das korrekt orientierte diagonale Smiley-Gesicht an der richtigen Stelle auf dem Hauptbild übertragen?

Antwort

1

Eine Lösung gefunden. Homographie!

oben Code Ersetzen durch:

#If bottomLeft = true, boxPoints[0] corresponds to btm-lft corner of subImage. 
#If bottomLeft = false, boxPoints[0] corresponds to btm-right corner of subImage. 
bottomLeft = True 

boundingBoxRotatedRect = cv2.minAreaRect(boxPoints) 
if boundingBoxRotatedRect[2] < -45: 
    boundingBoxRotatedRect = (boundingBoxRotatedRect[0], (boundingBoxRotatedRect[1][1],boundingBoxRotatedRect[1][0]), boundingBoxRotatedRect[2] + 90) 
    bottomLeft = False 
M = cv2.getRotationMatrix2D(boundingBoxRotatedRect[0], boundingBoxRotatedRect[2], 1.0) 
size = np.int0(boundingBoxRotatedRect[1]) 
size = (size[0],size[1]) 
dst = cv2.warpAffine(mainImage, M, (mainImage.shape[1], mainImage.shape[0])) 
subImage = cv2.getRectSubPix(dst, size, boundingBoxRotatedRect[0]) 

#Get homography matrix 
if bottomLeft: 
    pts_src = np.array([[0, size[0] - 1], [0, 0], [size[1] - 1, 0],[size[1] - 1, size[0] - 1]]) 
else: 
    pts_src = np.array([[size[1] - 1, size[0] - 1], [0, size[0] - 1], [0, 0], [size[1] - 1, 0]]) 
pts_dst = boxPoints 
h, status = cv2.findHomography(pts_src, pts_dst) 


## BELOW, REPLACE "[x, y], [X2, Y2], ...]" WITH LIST OF POINTS FROM SUBIMAGE TO BE TRANSLATED TO MAINIMAGE 
a = np.array([[x, y], [X2, Y2], ...] dtype='float32') 
a = np.array([a]) 
pointsOut = cv2.perspectiveTransform(a, h) 
pointsOut = pointsOut[0] 

#Then use the points in pointsOut to draw whatever you want! 
Verwandte Themen