【问题标题】:how i can control in area of Contours in opencv python我如何在opencv python中控制轮廓区域
【发布时间】:2020-01-02 15:17:38
【问题描述】:

您好,我在这里遇到了这段代码

我的问题是 -->>> 我如何控制轮廓内的区域 .. 例如我想在这个框中模糊或画一条线

我正在阅读这里的视频

    while True:
    ret, frames = cap.read()
    if frames is None:
        break
    frames = imutils.resize(frames, width=600)
    gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
    gray = cv2.bilateralFilter(gray, 11, 17, 17)
    blurred = cv2.GaussianBlur(gray, (5, 5), 0)
    ret, thresh = cv2.threshold(gray, 127, 255, 0)

    # find contours in the thresholded image and initialize the
    # shape detector
    (new, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:30]

    for c in cnts:
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)
        if len(approx) == 4:  # Select the contour with 4 corners
            NumberPlateCnt = approx  # This is our approx Number Plate Contour
            cv2.GaussianBlur(NumberPlateCnt, (43, 43), 30) # here when i blured

            break
    cv2.drawContours(frames, [NumberPlateCnt], -1, (0, 255, 0), 3)
    cv2.imshow("Final frames With Number Plate Detected", frames)
    cv2.waitKey(0)
    # cv2.imshow("Final frames With Number Plate Detect", cnts)

    if cv2.waitKey(1) & 0xFF == ord('q'):
             break

在这里当我模糊..我应该替换什么??..

          if len(approx) == 4:  # Select the contour with 4 corners
            NumberPlateCnt = approx  # This is our approx Number Plate Contour
            cv2.GaussianBlur(NumberPlateCnt, (43, 43), 30)

            break

【问题讨论】:

  • 有两种方法可以做到这一点:1)创建一个绘制轮廓的方法,而在计数内部更改像素 2)绘制轮廓;找到其中的所有像素,模糊每个像素
  • 你问的是模糊还是裁剪图像?
  • @Meisam 我问的是绿框内的模糊
  • 所以你想让盒子内部模糊而外部不模糊?
  • @Meisam 这正是我想要的

标签: python opencv graphics computer-vision


【解决方案1】:

对不起,我没有原图,所以我解释一下:

制作一个与frames 大小相同的黑色image,并像这样更改drawcontour:

cv2.drawContours(image, [NumberPlateCnt], -1, (255, 255, 255), -1) # I changed 3 to -1

这将为您提供一个只有板区域的二进制掩码。反转此掩码 (255-image) 并将其命名为 image2

现在将第一个蒙版 (image) 乘以 frames 并对结果进行模糊处理。 然后将第二个蒙版 (image2) 乘以 frames,并将结果与​​最后一个模糊图像相加。这是伪代码:

final_image = blur((image/255.)*frames) + ((image2/255.)*frames)

就是这样。

【讨论】:

  • 谢谢人...这对我有用..但我只是放弃(255-图像),因为它会影响我的整个视频..因为当我显示 image2 时它只是一个白色图像.. 。这是正确的?? ..或者不应该是白色的???.....
  • @AhmadOmar 你是对的,这个概念是正确的,但我在代码中有一个小错误。我修改了那个。
猜你喜欢
  • 1970-01-01
  • 2015-02-02
  • 2012-01-12
  • 2015-06-24
  • 1970-01-01
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
  • 2012-11-09
相关资源
最近更新 更多