首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >矩形区域的中心线和中心点的求取

矩形区域的中心线和中心点的求取
EN

Stack Overflow用户
提问于 2019-07-24 19:42:41
回答 2查看 4.3K关注 0票数 2

我运行了以下代码来创建一个矩形轮廓:

代码语言:javascript
复制
#import the necessary packages
import argparse
import imutils
import cv2
import numpy as np

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
                help="path to the input image")
args = vars(ap.parse_args())

# load the image, convert it to grayscale, blur it slightly, and threshold it
image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)

# threshold the image, then perform a series of erosions + dilations to remove any small regions of noise
thresh = cv2.threshold(gray, 45, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.erode(thresh, None, iterations=2)
thresh = cv2.dilate(thresh, None, iterations=2)

contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

# Find the index of the largest contour
areas = [cv2.contourArea(c) for c in contours]
max_index = np.argmax(areas)
cnt=contours[max_index]

x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)

# show the output image
cv2.imshow("Image", image)
cv2.waitKey(0)

我想找到矩形轮廓的中心线和中点。请给我建议。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-24 20:42:24

由于您已经在上面的代码中使用(x, y, w, h)获得了所需轮廓的x,y,w,h = cv2.boundingRect(cnt),所以垂直中线的中心可以由(x+w//2, y+h//2)给出,而垂直线可以使用下面的代码绘制:

代码语言:javascript
复制
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)
# center line
cv2.line(image, (x+w//2, y), (x+w//2, y+h), (0, 0, 255), 2)
# below circle to denote mid point of center line
center = (x+w//2, y+h//2)
radius = 2
cv2.circle(image, center, radius, (255, 255, 0), 2)

产出:

票数 7
EN

Stack Overflow用户

发布于 2019-07-24 20:23:49

因为您已经有了边界框,所以可以使用cv2.moments()查找中心坐标。这给出了质心(即对象的中心(x,y)-coordinates )。

代码语言:javascript
复制
M = cv2.moments(cnt)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])

中心点是(cX, cY),您可以用cv2.circle()绘制这个

代码语言:javascript
复制
cv2.circle(image, (cX, cY), 5, (36, 255, 12), -1)

同样,我们可以使用cv2.line()或Numpy切片绘制中心线。

代码语言:javascript
复制
cv2.line(image, (x + int(w/2), y), (x + int(w/2), y+h), (0, 0, 255), 3)
image[int(cY - h/2):int(cY+h/2), cX] = (0, 0, 255)
代码语言:javascript
复制
import imutils
import cv2
import numpy as np

# load the image, convert it to grayscale, blur it slightly, and threshold it
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)

# threshold the image, then perform a series of erosions + dilations to remove any small regions of noise
thresh = cv2.threshold(gray, 45, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.erode(thresh, None, iterations=2)
thresh = cv2.dilate(thresh, None, iterations=2)

contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

# Find the index of the largest contour
areas = [cv2.contourArea(c) for c in contours]
max_index = np.argmax(areas)
cnt=contours[max_index]

x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)

M = cv2.moments(cnt)

cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])

cv2.circle(image, (cX, cY), 5, (36, 255, 12), -1)

# To draw line you can use cv2.line or numpy slicing
cv2.line(image, (x + int(w/2), y), (x + int(w/2), y+h), (0, 0, 255), 3)
# image[int(cY - h/2):int(cY+h/2), cX] = (36, 255, 12)

# show the output image
cv2.imshow("Image", image)
cv2.imwrite("Image.png", image)
cv2.waitKey(0)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57190197

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档