【问题标题】:Drawing a line using centroid point in opencv在opencv中使用质心点绘制一条线
【发布时间】:2018-12-28 06:23:21
【问题描述】:

如何让一条线穿过轮廓的中心?我有轮廓的中心坐标。

【问题讨论】:

  • 一条线有两个点,即 start(x,y) 和 stop(x,y)。那么你希望你的生产线从哪里开始,又应该在哪里停止呢?你想让这条线从边界开始,通过中心并在边界结束吗?请详细说明
  • 我在轮廓周围放置了一个矩形。我需要在该矩形的中心水平放置一条线。就像将矩形切成两半一样。如何获得四个角的坐标画了那个矩形?

标签: python-3.x opencv opencv3.0 opencv-contour


【解决方案1】:

这就是你解决这个问题的方法 -

原图-

结果图片 -

您首先需要进行基本过滤并找到轮廓。那么——

1) 找出轮廓的面积 (minAreaRect)
2) 从轮廓中提取点 (BoxPoints)
3) 将其转换为 numpy 数组 (np.array)
4) 对点进行排序 (perspective.order_points)
5) 取出 Top-left、Top-right、Bottom-right 和 Bottom-left(tl, tr, br, bl) = box(第 52 行)
6) 计算中点 ((point1 + point2) / 2)
7) 画线(第 76 行)

这是它的代码

# import the necessary packages
from scipy.spatial import distance as dist
from imutils import perspective
from imutils import contours
import numpy as np
import imutils
import cv2

# Method to find the mid point
def midpoint(ptA, ptB):
    return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)

# load the image, convert it to grayscale, and blur it slightly
image = cv2.imread("test.png")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)

# perform edge detection, then perform a dilation + erosion to
# close gaps in between object edges
edged = cv2.Canny(gray, 50, 100)
edged = cv2.dilate(edged, None, iterations=1)
edged = cv2.erode(edged, None, iterations=1)

# find contours in the edge map
cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
    cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]

# loop over the contours individually
for c in cnts:
    # This is to ignore that small hair countour which is not big enough
    if cv2.contourArea(c) < 1000:
        continue

    # compute the rotated bounding box of the contour
    box = cv2.minAreaRect(c)
    box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
    box = np.array(box, dtype="int")

    # order the points in the contour such that they appear
    # in top-left, top-right, bottom-right, and bottom-left
    # order, then draw the outline of the rotated bounding
    # box
    box = perspective.order_points(box)
    # draw the contours on the image
    orig = image.copy()
    cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 3)

    # unpack the ordered bounding box, then compute the midpoint
    # between the top-left and top-right coordinates, followed by
    # the midpoint between bottom-left and bottom-right coordinates
    (tl, tr, br, bl) = box
    (tltrX, tltrY) = midpoint(tl, tr)
    (blbrX, blbrY) = midpoint(bl, br)

    # compute the midpoint between the top-left and top-right points,
    # followed by the midpoint between the top-righ and bottom-right
    (tlblX, tlblY) = midpoint(tl, bl)
    (trbrX, trbrY) = midpoint(tr, br)

    # draw and write the midpoints on the image
    cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1)
    cv2.putText(orig, "({},{})".format(tltrX, tltrY), (int(tltrX - 50), int(tltrY - 10) - 20),
            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,0,0), 2)
    cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1)
    cv2.putText(orig, "({},{})".format(blbrX, blbrY), (int(blbrX - 50), int(blbrY - 10) - 20),
            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,0,0), 2)
    cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1)
    cv2.putText(orig, "({},{})".format(tlblX, tlblY), (int(tlblX - 50), int(tlblY - 10) - 20),
            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,0,0), 2)
    cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1)
    cv2.putText(orig, "({},{})".format(trbrX, trbrY), (int(trbrX     - 50), int(trbrY - 10) - 20),
            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,0,0), 2)

    # draw lines between the midpoints
    cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)),
        (255, 0, 255), 2)
    cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)),
        (255, 0, 255), 2)

    # compute the Euclidean distance between the midpoints
    dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))
    dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))

    # loop over the original points
    for (xA, yA) in list(box):
        # draw circles corresponding to the current points and
        cv2.circle(orig, (int(xA), int(yA)), 5, (0,0,255), -1)
        cv2.putText(orig, "({},{})".format(xA, yA), (int(xA - 50), int(yA - 10) - 20),
            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,0,0), 2)

        # show the output image, resize it as per your requirements
        cv2.imshow("Image", orig) 

    cv2.waitKey(0)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-19
    • 2018-03-15
    • 1970-01-01
    相关资源
    最近更新 更多