但是我用下面的代码测试,效果不好:
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()
我应该如何修改它,我认为边缘检测和Huff变换不可行。
我也尝试使用以下代码:
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()
答案 0 :(得分:4)
这是在Python / OpenCV中提取左侧边缘的一种方法。
输入:
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)
倒置阈值图像:
腐蚀的图像:
轮廓图片:
在黑色图像上填充轮廓区域:
闭合的轮廓区域图像:
Sobel边缘图片:
输入图像上的结果边缘:
答案 1 :(得分:2)