简体   繁体   中英

How to locate pixel coordinates of a rectangle using Opencv?

I'm making this Question because when I was editing my answer in the original post it was deleted.

The original question was:

I'm trying to get the pixel coordinates of the green rectangle on the image below. I suppose it's possible using OpenCV. (i blurred all private details) <code>在此输入图片说明</code>

I had to make sure that the rectangle was totally green by using the Chrome color picker.

使用 Chrome 颜色选择器查找矩形颜色

As the color is in hex format #00FF00, which is equivalent to RGB notation RGB(0, 255, 0). But in OpenCV the color is represented in the BGR notation, then to get the green color in OpenCV, we have the following definition: BGR(0, 255, 0).

What we should do is to iterate over every pixel and check its value. The first time we find a pixel that matches BGR(0, 255, 0), we store this coordinate, which will be the top left corner of the green rectange, as loop starts at top left corner in the image and goes to the right until the end, then it moves 1 px down and starts again on the left to the right and so on, until it reaches the last image's pixel.

Every time a pixel is green I store its coordinates, because at the end of the green rectangle I'll have the bottom right green rectangle's coordinate. I dicided to explain step by step inside the code below:

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

That's the image result: 绿色矩形周围的红色矩形

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM