簡體   English   中英

如何從 python 中的圖像中獲取特定像素(藍色)的 x、y 坐標?

[英]How to fetch x,y coordinates of specific pixels(blue colored) from an image in python?

我的任務是從代碼生成的分割圖像(藍點)中獲取 x、y 坐標。 我如何自動化這個過程? 我的最終結果應該是第二張圖片中生成的這些藍點的 x、y 坐標的 zip。 在此處輸入圖像描述

生成藍點的代碼:

    import cv2
    import numpy as np
    import matplotlib.pyplot as plt
# import cv2_imshow
# from google.colab.patches import cv2_imshow
 
    image = cv2.imread('./S3/frame35.jpg')
#cv2_imshow(image)

    result = image.copy()
 
    image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

    # lower boundary RED color range values; Hue (0 - 10)
    lower1 = np.array([0, 100, 20])
    upper1 = np.array([10, 255, 255])

    # upper boundary RED color range values; Hue (160 - 180)
    lower2 = np.array([160,100,20])
    upper2 = np.array([179,255,255])

    lower_mask = cv2.inRange(image, lower1, upper1)
    upper_mask = cv2.inRange(image, lower2, upper2)

    full_mask = lower_mask + upper_mask;

    result = cv2.bitwise_and(result, result, mask=full_mask)

    plt.figure(figsize=[20,20])
    plt.axis("off")
#     plt.subplot(121);plt.imshow(image[:,:,::-1]);plt.title("Original Image",fontdict={'fontsize': 25});plt.axis('off');
    plt.subplot(122);plt.imshow(result, cmap='gray');plt.title("Mask of red Color",fontdict={'fontsize': 25});plt.axis('on');
    plt.savefig('mask_1.jpg', bbox_inches = 'tight')
#     cv2_imshow(full_mask)
#     cv2_imshow(result)
    #print(full_mask)
    #print(result) 
    cv2.waitKey(0)
    cv2.destroyAllWindows()

通過計算二值圖像的連通分量可以很簡單地做到這一點。

首先,您必須對圖像設置閾值以獲得分割點的二值圖像。 例如,您可以使用cv2.threshold執行此操作。 然后你可以使用cv2.connectedComponentsWithStats ,這將 - 除其他事項外 - 返回所有組件的質心列表。 其中之一將是背景,但在返回值中有一個相同大小的 integer 數組,其中所有組件都有不同的 label。因此您可以查找背景組件的 label 並將其從質心中刪除坐標。


編輯:您添加的代碼甚至沒有運行,但我將您的第二個屏幕截圖作為mask.png提供給它,並按如下方式使用提到的命令,以及圖像上的centroids plot:

src = cv2.imread('mask.png')
_, thresh = cv2.threshold(src[:, :, 0:1], 120, 255, cv2.THRESH_BINARY)
_, _, _, centroids = cv2.connectedComponentsWithStats(thresh, 8, cv2.CV_32S)

在此處輸入圖像描述


EDIT2:將以下片段附加到您的最新版本會導致:

在此處輸入圖像描述

_, thresh = cv2.threshold(result[:, :, 0:1], 20, 255, cv2.THRESH_BINARY)
_, _, _, centroids = cv2.connectedComponentsWithStats(thresh, 8, cv2.CV_32S)
plt.imshow(thresh)
plt.plot(centroids[:,0], centroids[:, 1], 'or', mfc='none')
plt.show()

具有所有上述建議更改的最終代碼已在下面實現。 在閾值之前使用中值濾波器,我能夠限制檢測到的輪廓數量。 接下來,我可以使用索引值訪問特定的 x、y 坐標。

    import cv2
    import numpy as np
    import matplotlib.pyplot as plt
# import cv2_imshow
# from google.colab.patches import cv2_imshow
 
    image = cv2.imread('./S3/frame35.jpg')
#cv2_imshow(image)

    result = image.copy()
 
    image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

    # lower boundary RED color range values; Hue (0 - 10)
    lower1 = np.array([0, 100, 20])
    upper1 = np.array([10, 255, 255])

    # upper boundary RED color range values; Hue (160 - 180)
    lower2 = np.array([160,100,20])
    upper2 = np.array([179,255,255])

    lower_mask = cv2.inRange(image, lower1, upper1)
    upper_mask = cv2.inRange(image, lower2, upper2)

    full_mask = lower_mask + upper_mask;

    result = cv2.bitwise_and(result, result, mask=full_mask)

    plt.figure(figsize=[20,20])
    plt.axis("off")
#     plt.subplot(121);plt.imshow(image[:,:,::-1]);plt.title("Original Image",fontdict={'fontsize': 25});plt.axis('off');
    plt.subplot(122);plt.imshow(result, cmap='gray');plt.title("Mask of red Color",fontdict={'fontsize': 25});plt.axis('on');
#     plt.savefig('mask_1.jpg', bbox_inches = 'tight')

    median = cv2.medianBlur(result,9)
    _, thresh = cv2.threshold(median[:, :, 0:1],20, 255, cv2.THRESH_BINARY)
    _, _, _, centroids = cv2.connectedComponentsWithStats(thresh, 8, cv2.CV_32S)
    plt.imshow(thresh)
   
    plt.plot(centroids[:,0], centroids[:, 1], 'or', mfc='none')
    plt.show()

    cv2.waitKey(0)
    cv2.destroyAllWindows()

在此處輸入圖像描述

暫無
暫無

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

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