首页 > 解决方案 > 如何在图像的黑色区域绘制绿线?

问题描述

这是原图:<br> 在此处输入图像描述

我想在红色部分标注的区域画一条绿线: 在此处输入图像描述

我想要这个效果:<br> 在此处输入图像描述

或者这样:<br> 在此处输入图像描述 在此处输入图像描述

但是我用下面的代码测试,效果不好:</p>

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()

效果是这样的:<br> 在此处输入图像描述

我应该怎么修改呢,我觉得边缘检测和哈夫变换都不可行。

我也试过用下面的代码:</p>

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()

但是效果不好: 在此处输入图像描述

标签: pythonopencv

解决方案


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

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

输入:

在此处输入图像描述

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)


倒置阈值图像:

在此处输入图像描述

侵蚀图像:

在此处输入图像描述

轮廓图像:

在此处输入图像描述

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

在此处输入图像描述

闭合轮廓区域图像:

在此处输入图像描述

索贝尔边缘图像:

在此处输入图像描述

输入图像上的结果边缘:

在此处输入图像描述


推荐阅读