JAYLEE900 2022-04-14 00:07 采纳率: 37.5%
浏览 179
已结题

如何使用opencv清晰地提取图像中的六边形轮廓,求效果及代码

img

img


本人初学者一名,上图是我提取出来的轮廓,图像去噪效果不是很好,还是有很多,效果太差,分别使用了sobel,canny锐化边缘,用了高斯模糊除了,效果还是没有明显改善,向各位请教一下有什么好的方法能够吧图像中的六边形高质量的完全提取出来吗

  • 写回答

3条回答 默认 最新

  • 歇歇 2022-04-14 01:22
    关注

    安装imutils
    pip install imutils
    代码
    ShapeDetector.Py

    import cv2
    class ShapeDetector:
        def __init__(self):
            pass
        def detect(self, c):
            shape = ""
            peri = cv2.arcLength(c, True)
            approx = cv2.approxPolyDP(c, 0.04 * peri, True)
            if len(approx) == 6:
            shape ="六边形"
            return shape
    

    detect_shapes.py

    from pyimagesearch.shapedetector import ShapeDetector
    import argparse
    import imutils
    import cv2
    
    ap = argparse.ArgumentParser()
    ap.add_argument("-i", "--image", required=True,
        help="输入图像的路径")
    args = vars(ap.parse_args())
    image = cv2.imread(args["image"])
    resized = imutils.resize(image, width=300)
    ratio = image.shape[0] / float(resized.shape[0])
    gray = cv2.cvtColor(resized, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    thresh = cv2.threshold(blurred, 60, 255, cv2.THRESH_BINARY)[1]
    cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
        cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    sd = ShapeDetector()
    for c in cnts:
        M = cv2.moments(c)
        cX = int((M["m10"] / M["m00"]) * ratio)
        cY = int((M["m01"] / M["m00"]) * ratio)
        shape = sd.detect(c)
        c = c.astype("float")
        c *= ratio
        c = c.astype("int")
        cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
        cv2.putText(image, shape, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX,
            0.5, (255, 255, 255), 2)
        cv2.imshow("Image", image)
        cv2.waitKey(0)
    

    python detect_shapes.py --image 你的图形文件.png

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月24日
  • 已采纳回答 4月16日
  • 创建了问题 4月14日

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行