解決方案並不確切,但它提供了一個很好的起點。儘管你必須使用參數。如果您使用某種閾值方法分割條,然後單獨應用@ 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