我的目标是声明一个空的2D数组,然后将其初始化,然后在每次do()运行时都填充值。问题是即使初始化数组,我也会得到nil指针取消引用。
这是我要在服务器模拟器上尝试完成的简单版本。
package main
import "fmt"
type Srv struct {
A *[][]int
}
func (s Srv) init() {
arr := make([][]int, 0)
*s.A = arr
}
func main() {
s := Srv{nil}
s.init()
printSlice(*s.A)
do(s.A)
do(s.A)
}
func printSlice(s [][]int) {
fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}
func do(s *[][]int) {
*s = append(*s, make([]int, 0))
printSlice(*s)
(*s)[0] = append((*s)[0], 5)
(*s)[0] = append((*s)[0], 6)
*s = append(*s, make([]int, 0))
printSlice(*s)
}
我希望得到类似[[5 6 5 6] [] [] []]的输出,但是我得到了nil指针取消引用。
答案 0 :(得分:1)
在init执行import numpy as np
import cv2
from matplotlib import pyplot as plt
MIN_MATCH_COUNT = 10
img1 = cv2.imread('box.png',0) # queryImage
img2 = cv2.imread('box_in_scene.png',0) # trainImage
# Initiate SIFT detector
sift = cv.xfeatures2d.SIFT_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks = 50)
flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1,des2,k=2)
matchesMask = [[0,0] for i in range(len(matches))]
# ratio test as per Lowe's paper
for i,(m,n) in enumerate(matches):
if m.distance < 0.2*n.distance:
matchesMask[i]=[1,0]
draw_params = dict(matchColor = (0,255,0),
singlePointColor = (255,0,0),
matchesMask = matchesMask,
flags = 0)
img3 = cv.drawMatchesKnn(img1,kp1,img2,kp2,matches,None,**draw_params)
plt.figure(1234)
plt.imshow(img3)
的地方,它取消引用了nil指针。 *s.A =
尚未初始化(即s.A
),而nil
是解除引用运算符。但这只是一个问题,因为它在一开始就不必要地复杂。应该是:
*