许多对点之间的箭头

时间:2017-09-21 11:45:50

标签: python matplotlib plot arrow-functions

我正在寻找位于np.array中的两个点之间的箭头。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 15 12:31:29 2017
test for the res matrix

@author: arun
"""
import cv2
import numpy as np 
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import time
import math
#import matplotlib as mpl
#import matplotlib.pyplot as plt
#import trackpy as tp
#import pims
from PIL import Image



def makecorr(imga,imgb):

    img = imga
    img2 = img.copy()
    template = imgb

    # All the 6 methods for comparison in a list
    methods = ['cv2.TM_CCORR_NORMED']
    for meth in methods:
        img = img2.copy()
        method = eval(meth)
        # Apply template Matching
        res = cv2.matchTemplate(img,template,method)
        min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
        # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
        if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
            top_left = min_loc
        else:
            top_left = max_loc
            a=max_val
        center = (top_left[0]-32, top_left[1]-32 )

    return res,center

def subpixel(res,max_loc):

    peak1_i, peak1_j = max_loc

    # the peak and its neighbours: left, right, down, up
    c = res[peak1_i,   peak1_j]
    cl = res[peak1_i - 1, peak1_j]
    cr = res[peak1_i + 1, peak1_j]
    cd = res[peak1_i,   peak1_j - 1]
    cu = res[peak1_i,   peak1_j + 1]
    subp = (peak1_i + ((math.ln(cl) - math.ln(cr)) / (2 * math.ln(cl) - 4 * math.ln(c) + 2 * math.ln(cr))),peak1_j + ((math.ln(cd) - math.log(cu)) / (2 * math.ln(cd) - 4 * math.ln(c) + 2 * math.ln(cu))))



    return subp       








#-------------------------------------------------------------------------------------------------------------

"""Read the images before and after movement"""

img1=cv2.imread("initial_image1.jpg",0)
img2=cv2.imread("displaced.jpg",0)

#res1,x6,a=makecorr(img1,img2)
#cent=subpixel(res1,x6)
w, h = img1.shape[::-1]
n=int(w/64)
m=int(h/64)
d2=64
d=32
k=0 
num=285
list=np.empty([num,2])
list1=np.empty([num,2])
xb=np.empty([num,1])
yb=np.empty([num,1])
xt=np.empty([num,1])
yt=np.empty([num,1])

for i in range (0,19):
    for j in range(0,15):          # the image is of dimensions 1280x1024. 
        x=64+i*64   
        y=64+j*64
        x1=x-d2
        x2=x-d
        x3=x+d
        x4=x+d2
        y1=y-d2
        y2=y-d
        y3=y+d
        y4=y+d2

##imgB=img1[y1:y2, x1:x2] #template to be matched in image 2
#        imgA=img2[int(y1-d/2):int(y2+d/2),int(x1-d/2):int(x2+d/2)] #Bigger search area to find the matching image
#        
##""" We use Correlation function to obtain the location of the matched area """      
#        topleft1=Crosscorr.makecor(imgA,imgB)


#        y3=(y1-d)
#        y4=(y2+d)
#        x3=int(x1-d)
#        x4=int(x2+d)
#        
        #""" we need to crop the images from the main image according to the locations specified above.Image A is the template and Image B is the search area which is larger by a width of d/2 on all sides of the template imageA"""    
        img3=img1[x2:x3,y2:y3] #template to be matched in image 2
        img4=img2[x1:x4,y1:y4] #Bigger search area to find the matching image
#          
        res1,x6=makecorr(img4,img3)
#        print(x6)
        xb[k],yb[k]=x6[0]+x1+64,x6[1]+y1+64

        xt[k],yt[k]=x,y
        print(xb[k],xt[k],yb[k],yt[k])





#        #cent=subpixel(res1,x6)
#        
        #        res,topleft1=Crosscorr.makecor(imgA,imgB)
        #        
        ##        truexy=Crosscorr.subpixel(res,topleft1) 
        ##        a,b=truexy
        ##        list1[list][0]=a        
        ##        list1[list][1]=b
        #        
        #        





        k=k+1


print(xb)        

#soa = np.array(list1[:][0],list1[k][1],list[k][0], list[k][1])
X, Y, U, V = xt,yt,xb,yb
np.X= list[:][0]
plt.figure()
ax = plt.gca()
ax.quiver(X, Y, U, V, angles='xy', scale_units='xy', scale=15)
ax.set_xlim([0, 1280])
ax.set_ylim([0, 1024])
plt.draw()
plt.show()      

当前代码给出了相同的结果,无论我输入点的顺序如何(X,Y,U,V = xt,yt,xb,yb)。我希望有一种更简单的方法可以使用pyplot在一组配对点之间绘制矢量。我在论坛中找到了一组两分或三分,但没有多少分。

初始图片: initial image

流离失所的形象: displaced image

我应该在右侧获得箭头,因为我将右侧的粒子移动了20个像素。相反,我得到如下图所示的箭头:

arrows make me crazy

0 个答案:

没有答案