我有这个雕像的图像.
我正在尝试找到雕像的顶部、底部、左侧和最右侧的点.有没有办法测量每边的边缘以确定雕像上的最外点?我想得到每一边的 (x,y)
坐标.我尝试使用 cv2.findContours()
和 cv2.drawContours()
来获得雕像的轮廓.
导入 cv2img = cv2.imread('statue.png')灰色 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)轮廓 = cv2.findContours(灰色,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)[0]cv2.drawContours(img, 轮廓, -1, (0, 200, 0), 3)cv2.imshow('img', img)cv2.waitKey()
这是一个潜在的方法:
将图像转换为 或 argmax()
像这样确定外左、右、上、下坐标
left = tuple(c[c[:, :, 0].argmin()][0])对 = 元组(c[c[:, :, 0].argmax()][0])top = tuple(c[c[:, :, 1].argmin()][0])底部 = 元组(c[c[:, :, 1].argmax()][0])
这是结果
<块引用>左:(162, 527)
<块引用>
右:(463, 467)
<块引用>
顶部:(250, 8)
<块引用>
底部:(381, 580)
导入 cv2将 numpy 导入为 np# 加载图像,灰度,高斯模糊,阈值图像 = cv2.imread('1.png')灰色 = cv2.cvtColor(图像,cv2.COLOR_BGR2GRAY)模糊 = cv2.GaussianBlur(灰色, (3,3), 0)thresh = cv2.threshold(模糊, 220, 255, cv2.THRESH_BINARY_INV)[1]# 寻找轮廓cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)cnts = cnts[0] 如果 len(cnts) == 2 否则 cnts[1]c = max(cnts, key=cv2.contourArea)# 获取外坐标left = tuple(c[c[:, :, 0].argmin()][0])对 = 元组(c[c[:, :, 0].argmax()][0])top = tuple(c[c[:, :, 1].argmin()][0])底部 = 元组(c[c[:, :, 1].argmax()][0])# 在图像上画点cv2.drawContours(图像, [c], -1, (36, 255, 12), 2)cv2.circle(图像, 左, 8, (0, 50, 255), -1)cv2.circle(图像, 右, 8, (0, 255, 255), -1)cv2.circle(图像, 顶部, 8, (255, 50, 0), -1)cv2.circle(图像, 底部, 8, (255, 255, 0), -1)print('left: {}'.format(left))print('right: {}'.format(right))print('top: {}'.format(top))print('bottom: {}'.format(bottom))cv2.imshow('thresh', thresh)cv2.imshow('图像', 图像)cv2.waitKey()
I have this image of a statue.
I'm trying to find the top, bottom, left, and right most points on the statue. Is there a way to measure the edge of each side to determine the outer most point on the statue? I want to get the (x,y)
coordinate of each side. I have tried to use cv2.findContours()
and cv2.drawContours()
to get an outline of the statue.
import cv2
img = cv2.imread('statue.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
contours = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[0]
cv2.drawContours(img, contours, -1, (0, 200, 0), 3)
cv2.imshow('img', img)
cv2.waitKey()
Here's a potential approach:
Convert image to grayscale and Gaussian blur
Threshold to obtain a binary image
Find contours
Obtain outer coordinates
After converting to grayscale and blurring image, we threshold to get a binary image
Now we find contours using cv2.findContours()
. Since OpenCV uses Numpy arrays to encode images, a contour is simply a Numpy array of (x,y)
coordinates. We can slice the Numpy array and use argmin()
or argmax()
to determine the outer left, right, top, and bottom coordinates like this
left = tuple(c[c[:, :, 0].argmin()][0])
right = tuple(c[c[:, :, 0].argmax()][0])
top = tuple(c[c[:, :, 1].argmin()][0])
bottom = tuple(c[c[:, :, 1].argmax()][0])
Here's the result
left: (162, 527)
right: (463, 467)
top: (250, 8)
bottom: (381, 580)
import cv2
import numpy as np
# Load image, grayscale, Gaussian blur, threshold
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
thresh = cv2.threshold(blur, 220, 255, cv2.THRESH_BINARY_INV)[1]
# Find contours
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
c = max(cnts, key=cv2.contourArea)
# Obtain outer coordinates
left = tuple(c[c[:, :, 0].argmin()][0])
right = tuple(c[c[:, :, 0].argmax()][0])
top = tuple(c[c[:, :, 1].argmin()][0])
bottom = tuple(c[c[:, :, 1].argmax()][0])
# Draw dots onto image
cv2.drawContours(image, [c], -1, (36, 255, 12), 2)
cv2.circle(image, left, 8, (0, 50, 255), -1)
cv2.circle(image, right, 8, (0, 255, 255), -1)
cv2.circle(image, top, 8, (255, 50, 0), -1)
cv2.circle(image, bottom, 8, (255, 255, 0), -1)
print('left: {}'.format(left))
print('right: {}'.format(right))
print('top: {}'.format(top))
print('bottom: {}'.format(bottom))
cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey()
这篇关于使用 Python OpenCV 查找图像中的极端外部点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!