簡體   English   中英

如何按棋盤順序收集斑點的中心?

[英]How do I collect the center of the blobs in chessboard order?

我在 python 中使用 opencv 來校准鏡頭。 我在不同的角度和位置拍了一張照片,如下所示: 56個黑底LED點

然后,我在 opencv 中運行了斑點檢測器。

import cv2
import numpy
img = cv2.imread('./image.png', cv2.IMREAD_GRAYSCALE)

params = cv2.SimpleBlobDetector_Params()
params.minThreshold = 50
params.maxThreshold = 255
params.filterByArea = True
params.minArea = 0
params.maxArea = 80
params.filterByColor = True
params.blobColor = 255
params.filterByCircularity = False
params.filterByConvexity = False
params.filterByInertia = False

detector = cv2.SimpleBlobDetector_create(params)

keypoints = detector.detect(img)

檢測到的斑點像這樣用紅色圓圈顯示。

檢測到的斑點以紅色圓圈顯示。

對象關鍵點包含檢測到的斑點的中心位置。 我這樣收集中心位置:

ips = []
for keypoint in keypoints: ips.append((keypoint.pt[0], keypoint.pt[1]))
ips = np.array(ips, np.float32) # image points.

我想要的是,我想像 cv2 一樣按順序存儲那些紅色圓圈的中心位置。 找到棋盤角。

圓圈的順序。

所以我定義了一個函數,先沿水平軸排序,然后再沿垂直軸排序。

def index_sorting_to_checkerboard(arr, shrinker):

    ind1 = arr[:,1].argsort()
    arr = arr[ind1]
    floored = np.floor(arr).astype(int)
    floored_shrinked = np.zeros_like(floored)
    std = floored[0,0]
    for i, (x,y) in enumerate(floored):
        if abs(y-std) <= shrinker: floored_shrinked[i] = (x, std)
        else: 
            std = y
            floored_shrinked[i] = (x, y)

    # sort by the second col, then the first column.
    ind2 = np.lexsort((floored_shrinked[:,0], floored_shrinked[:,1]))
    ret = floored_shrinked[ind2]
    ind3 = ind1[ind2] # An accumulation of all argument changes.

    return ret, ind3

shk, ind = index_sorting_to_checkerboard(ips, 30)
ips = ips[ind]

此功能在圖像低傾斜時起作用。 但是,當圖像傾斜很多時: 傾斜的圖像。

它不能正常工作。 這是因為上一行的斑點的垂直順序可能低於下一行的斑點。 也就是說,當圖像傾斜很多時,blob 13(右上角)的垂直位置可能低於 blob 14(在 blob 0 下方)。 所以每次拍攝新圖像時,我都必須手動更改“shrinker”的值。

你能建議我更好的算法來按照棋盤順序排序,無論傾向如何? 我認為這是可能的,因為 cv2.findChessboardCorners 總是按此順序返回位置,但我不知道它是如何做到的。

由於您的模式被拉長,您可以估計網格兩個軸的大致方向(例如使用 PCA)。

根據估計的方向和點之間的距離,您將能夠搜索哪個點與某個點相鄰。 所以,似乎可以識別點的順序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM