[英]How to find top_left, top_right, bottom_left, right coordinates in 2d mask where cell has specified value?
[英]How can i extract out the coordinates (top, bottom, left, right) of white mask (polygon) in image?
我正在尝试提取二进制图像的坐标 (x, y)。 我需要白色面具的顶部、底部、左右 (x, y) 点,我猜这是多边形。 如下图,怎么去掉点数?
图片:图片
我正在做,但它给了我坐标数组:
我的代码:
import numpy as np
import cv2
img = cv2.imread('Image.png')
x, y = np.where(img==(255,255))
points = list(zip(x,y))
print(points)
这是在 Python/OpenCV 中获取图像中白色斑点的极值点的两种简单方法。
输入:
import cv2
import numpy as np
# read image
img = cv2.imread('white_blob.png')
# Method 1: bounding rect
# threshold on white
lower =(255,255,255)
upper = (255,255,255)
thresh = cv2.inRange(img, lower, upper)
# get points
points = np.column_stack(np.where(thresh.transpose() > 0))
x,y,w,h = cv2.boundingRect(points)
print(x,y,w,h)
min = (x,y)
max = (x+w-1,y+h-1)
print("min:", min)
print("max:", max)
# Method 2: Numpy
(Note: Numpy coordinates are y,x. So need to rearrange)
a = np.where(img != 0)
x1,y1,x2,y2 = np.min(a[1]), np.min(a[0]), np.max(a[1]), np.max(a[0])
min = (x1,y1)
max = (x2,y2)
print("min:", min)
print("max:", max)
两种方法都会产生 (x,y):
min: (0, 293)
max: (127, 363)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.