gpt4 book ai didi

python - 如何在图像的黑色区域上绘制绿线?

转载 作者:行者123 更新时间:2023-12-02 16:13:23 24 4
gpt4 key购买 nike

这是原始图片:
enter image description here

我想在红色部分标记的区域上画一条绿线:
enter image description here

我想要这种效果:
enter image description here

或像这样:
enter image description here
enter image description here

但是我用下面的代码来测试,效果不好:

import cv2
import numpy as np

img = cv2.imread(r"E:\test_opencv\images\luyuan1.jpg")
blur_img = cv2.GaussianBlur(img, (3, 3), 0)
gray = cv2.cvtColor(blur_img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 150, 250, apertureSize=3)
rho = 1 #Distance resolution of the accumulator in pixels.
theta = np.pi/180 # Angle resolution of the accumulator in radians.
threshold = 100 #Accumulator threshold parameter. Only those lines are returned that get enough votes ( >\texttt{threshold} ).
lines = cv2.HoughLines(edges, rho, theta, threshold)
if lines is not None:
for i in range(len(lines)):
for r,th in lines[i]:
a = np.cos(th)
b = np.sin(th)
x0 = a*r
y0 = b*r
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))

cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)
print(x1,y1,x2,y2)
# cv2.imshow('canny', edges)
cv2.imshow('Result', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

效果是这样的:
enter image description here

我应该如何修改它,我认为边缘检测和霍夫变换不可行。

我也尝试使用以下代码:
import cv2
import numpy as np

src = cv2.imread(r"E:\test_opencv\images\luyuan1.jpg")
cv2.imshow("src", src)

hsv = cv2.cvtColor(src, cv2.COLOR_BGR2HSV)
low_hsv = np.array([0, 0, 46])
high_hsv = np.array([180, 43, 220])
mask = cv2.inRange(hsv, lowerb=low_hsv, upperb=high_hsv)
cv2.imshow("mask", mask)
cv2.waitKey(0)
cv2.destroyAllWindows()

但是效果不好:
enter image description here

最佳答案

这是在Python / OpenCV中提取左侧边缘的一种方法。

  • 读取输入
  • 转换为灰度
  • 应用自适应阈值并反转白黑极性
  • 侵 eclipse 以分离感兴趣的区域
  • 获取最大轮廓
  • 在黑色背景上绘制填充轮廓
  • 关闭白色区域
  • 将x-sobel边缘应用于该区域,仅获得左侧边缘
  • 将提取的边缘覆盖到输入图像
  • 保存输出

  • 输入:

    enter image description here
    import cv2
    import numpy as np

    # read image
    img = cv2.imread("blinds.png")

    # convert img to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # apply gaussian blur (sigma=2)
    blur = cv2.GaussianBlur(gray, (5,5), 0, 0)

    # do adaptive threshold on gray image
    thresh = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 91, 7)

    # invert
    thresh = 255 - thresh

    # apply morphology erode then close
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
    erode = cv2.morphologyEx(thresh, cv2.MORPH_ERODE, kernel)

    # Get largest contour
    cnts = cv2.findContours(erode, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    result = img.copy()
    area_thresh = 0
    for c in cnts:
    area = cv2.contourArea(c)
    if area > area_thresh:
    area_thresh=area
    big_contour = c

    # draw largest contour only
    big_c = img.copy()
    cv2.drawContours(big_c, [big_contour], -1, (0, 255, 0), 1)

    # draw white contour region on black background image
    region = np.full_like(img, (0,0,0))
    cv2.drawContours(region, [big_contour], -1, (255,255,255), -1)

    # apply morphology close to region
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (57,57))
    closed = cv2.morphologyEx(region, cv2.MORPH_CLOSE, kernel)

    # get left-side edge as single channel
    sobel = cv2.Sobel(closed, cv2.CV_8U, 1, 0, 3)[:,:,0]

    # get result image overlaying edge on input
    result = img.copy()
    result[sobel==255] = (0,0,255)

    # write results to disk
    cv2.imwrite("blinds_thresh.png", thresh)
    cv2.imwrite("blinds_erode.png", erode)
    cv2.imwrite("blinds_big_c.png", big_c)
    cv2.imwrite("blinds_region.png", region)
    cv2.imwrite("blinds_closed.png", closed)
    cv2.imwrite("blinds_sobel.png", sobel)
    cv2.imwrite("blinds_left_edge.png", result)


    # display it
    cv2.imshow("IMAGE", img)
    cv2.imshow("THRESHOLD", thresh)
    cv2.imshow("ERODE", erode)
    cv2.imshow("BIG_C", big_c)
    cv2.imshow("REGION", region)
    cv2.imshow("CLOSED", closed)
    cv2.imshow("SOBEL", sobel)
    cv2.imshow("RESULT", result)
    cv2.waitKey(0)

    倒置阈值图像:

    enter image description here

    腐 eclipse 的图像:

    enter image description here

    轮廓图:

    enter image description here

    黑色图像上的填充轮廓区域:

    enter image description here

    封闭轮廓区域图像:

    enter image description here

    Sobel边缘图片:

    enter image description here

    输入图像上的结果边缘:

    enter image description here

    关于python - 如何在图像的黑色区域上绘制绿线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60273336/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com