将图像坐标转换为矩阵索引

问题描述 投票:-1回答:1

我试图从图像中删除blob(Python3)。问题是我在笛卡尔坐标中得到斑点的位置。我的目的是获得blob在矩阵索引中的位置,这样我就可以访问它们并将它们删除(使它们等于零)。

这是我的代码:

for i in range(1, 2):

    name                = str(i) + ".jpg"
    nameBW              = str(i) + "_bw.jpg"
    img                 = cv2.imread(name,0) #zero -> abre em grayscale
    #threshold manual
    retval, threshold_manual    = cv2.threshold(img, 50, 255, cv2.THRESH_BINARY)
    #threshold adaptativo
    threshold_adaptativo        = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY,11,2)
    # medianblur por cima do threshold adaptativo
    median                      = cv2.medianBlur(threshold_adaptativo,5)
    #2ª dose de medianBlur
    median2                     = cv2.medianBlur(median,1)


    # Setup SimpleBlobDetector parameters.
    params = cv2.SimpleBlobDetector_Params()

    # Change thresholds
    params.minThreshold = 1
    params.maxThreshold = 2000
    # Filter by Area.
    params.filterByArea = True
    params.minArea = 0.1
    # Filter by Circularity
    params.filterByCircularity = False
    params.minCircularity = 0.1
    # Filter by Inertia
    # params.filterByInertia = True
    # params.minInertiaRatio = 0.001

    detector                = cv2.SimpleBlobDetector_create(params)
    keypoints               = detector.detect(median2)
    im_with_keypoints       = cv2.drawKeypoints(median2, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

    for j in range(1, len(keypoints)):
        x = keypoints[j].pt[0] #i is the index of the blob you want to get the position
        y = keypoints[j].pt[1]
        median2[x, y] = 0

这是我要清理的图像:enter image description here

关键点是笛卡尔坐标中的内容。我想要的是将其转换为矩阵索引,以便我可以访问图像median2上的相同索引,然后删除blob。

python image python-3.x opencv image-processing
1个回答
1
投票

(1)语法错误:

你的代码:

for j in range(1, len(keypoints)):
    x = keypoints[j].pt[0] #i is the index of the blob you want to get the position
    y = keypoints[j].pt[1]
    median2[x, y] = 0

我们知道,关键点是浮点数,而索引应该是int;我们用[rows,cols]索引图像,即[y,x]。

改性:

for kpt in keypoints:
    x = int(pt[0])     # change float to int 
    y = int(pt[1])
    median2[y, x] = 0  # index in [row,col] order, that is (y,x)

(2)逻辑错误

您不能通过将blob关键点设置为零来删除小区域。

© www.soinside.com 2019 - 2024. All rights reserved.