簡體   English   中英

如何使用 Opencv 定位矩形的像素坐標?

[英]How to locate pixel coordinates of a rectangle using Opencv?

我提出這個問題是因為當我在原始帖子中編輯我的答案時,它被刪除了。

原來的問題是:

我正在嘗試獲取下圖中綠色矩形的像素坐標。 我想可以使用 OpenCV。 (我模糊了所有私人細節) <code>在此輸入圖片說明</code>

我必須使用 Chrome 顏色選擇器確保矩形完全是綠色的。

使用 Chrome 顏色選擇器查找矩形顏色

因為顏色是十六進制格式#00FF00,相當於RGB符號RGB(0, 255, 0)。 但是在 OpenCV 中,顏色以 BGR 表示法表示,然后要在 OpenCV 中得到綠色,我們有以下定義:BGR(0, 255, 0)。

我們應該做的是遍歷每個像素並檢查其值。 當我們第一次找到一個匹配 BGR(0, 255, 0) 的像素時,我們存儲這個坐標,它將是綠色矩形的左上角,因為循環從圖像的左上角開始並向右移動直到結束,然后它向下移動 1 px 並從左到右再次開始,依此類推,直到到達最后一個圖像的像素。

每次像素為綠色時,我都會存儲它的坐標,因為在綠色矩形的末端,我將擁有右下角綠色矩形的坐標。 我決定在下面的代碼中逐步解釋:

import cv2

coordinates = []  # list of the green rectangle coordinates
green_color = [0, 255, 0]
last_x_green, last_y_green = 0, 0  # store the last x and y green positions

# reads the image in the color mode
img = cv2.imread('original.jpg', 1)
rows, cols, _ = img.shape  # gets the image's rows and color, which are height and width

for x in range(rows):
    for y in range(cols):
        px = list(img[x, y])
        if px == green_color:
            # find the first coordinate of the green rectangle (top left corner)
            if len(coordinates) == 0:
                coordinates.append((y, x))  # top left corner of the green rectangle

            last_x_green, last_y_green = x, y

coordinates.append((last_y_green, last_x_green))

# Now we have the top left corner and the bottom right corner of the green rectangle
print(coordinates)

# As printed above, the coordinates of top left corner and bottom right corner of the green rectangle are (167, 2508)
# and (615, 2951), respectivelly.
# We can find the other coordinates based on these two coordinates the following way:
# Let's assume these coordinates are (x1, y1) and (x2, y2). The bottom left corner must be at (x1, y2) and the top
# right corner must be (x2, y1). Therefore, the bottom left coordinate is (167, 2951) and the top right coordinate is
# (615, 2580).
# Generically, we would have the four points represents in this form:
# coordinates: [(x1, y1), (x2, y2)]
top_left = coordinates[0]  # (x1, y1)
bottom_left = (coordinates[0][0], coordinates[1][1])  # (x1, y2)
top_right = (coordinates[1][0], coordinates[0][1])  # (x2, y1)
bottom_right = coordinates[1]

print('The coordinates of the green rectangle, from left to right and from top to bottom, are:')
print(f'Top Left: {top_left}, Top Right: {top_right}, Bottom Left: {bottom_left}, Bottom Right: {bottom_right}')

# Syntax: cv2.rectangle(image, start_point, end_point, color, thickness)
# Draw a 10 px red rectangle around the green rectangle and save the image.
# We only need the top left and bottom right corner to draw it
cv2.rectangle(img, coordinates[0], coordinates[len(coordinates) - 1], (0, 0, 255), 10)
cv2.imwrite('rectangle_detected.jpg', img)
[(167, 2508), (615, 2951)]
The coordinates of the green rectangle, from left to right and from top to bottom, are: 
Top Left: (167, 2508), Top Right: (615, 2508), Bottom Left: (167, 2951), Bottom Right: (615, 2951)
Process finished with exit code 0

這是圖像結果: 綠色矩形周圍的紅色矩形

暫無
暫無

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

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