解决方案并不确切,但它提供了一个很好的起点。尽管你必须使用参数。如果您使用某种阈值方法分割条,然后单独应用@ api55提到的hough线,它将对您有很大的帮助。
这是我得到的结果。
代码。
import cv2
import numpy as np
# read image
img = cv2.imread('KbxN6.jpg')
# filter it
img = cv2.GaussianBlur(img, (11, 11), 0)
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# get edges using laplacian
laplacian_val = cv2.Laplacian(gray_img, cv2.CV_32F)
# lap_img = np.zeros_like(laplacian_val, dtype=np.float32)
# cv2.normalize(laplacian_val, lap_img, 1, 255, cv2.NORM_MINMAX)
# cv2.imwrite('laplacian_val.jpg', lap_img)
# apply threshold to edges
ret, laplacian_th = cv2.threshold(laplacian_val, thresh=2, maxval=255, type=cv2.THRESH_BINARY)
# filter out salt and pepper noise
laplacian_med = cv2.medianBlur(laplacian_th, 5)
# cv2.imwrite('laplacian_blur.jpg', laplacian_med)
laplacian_fin = np.array(laplacian_med, dtype=np.uint8)
# get lines in the filtered laplacian using Hough lines
lines = cv2.HoughLines(laplacian_fin,1,np.pi/180,480)
for rho,theta in lines[0]:
a = np.cos(theta)
b = np.sin(theta)
x0 = a*rho
y0 = b*rho
x1 = int(x0 + 1000*(-b))
y1 = int(y0 + 1000*(a))
x2 = int(x0 - 1000*(-b))
y2 = int(y0 - 1000*(a))
# overlay line on original image
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)
# cv2.imwrite('processed.jpg', img)
# cv2.imshow('Window', img)
# cv2.waitKey(0)
只是一些sugestions,1)试图消除背景(阈值)。 2)找线,也许[hough line transform](http://docs.opencv.org/2。4/doc/tutorials/imgproc/imgtrans/hough_lines/hough_lines.html)可以帮助,3)找到这些线的交点4)用这个交点创建矩形。奖金)您可以尝试将每个物体(轮廓)与其余物体(其余物体=黑色)进行隔离,然后逐个分析它们,而没有任何其他数据可能会影响结果 – api55
您是否还有更多图像可以查看其中的变化? –
@MarkSetchell请参阅我的更新 – zhengyue