我正在尝试过滤呈现电缆的图像的背景。我尝试执行以下操作:
void IntListInsertInOrder(IntList L, int v)
{
//check if list is empty
if (L->first == NULL) {
IntListInsert(L, v);
}
}
$./list 1 2 3
1
或cv2.Laplacian
的2倍在两个方向上查找边缘。cv2.Sobel
,cv2.THRESH_BINARY(_INV)
cv2.THRESH_OTSU
和cv2.Canny
总体而言,结果根本不令人满意。我将举例说明2张图片:
我的脚本的输出:
我也玩过cv2.HoughLinesP
中的值,但是结果相差不大。
这是我设法做的小脚本:
config
我认为我需要做更好的过滤。但是,我也尝试了图像分割,但是结果根本没有希望。 关于如何改善这一点的任何想法? 谢谢
答案 0 :(得分:1)
这是在Python / OpenCV中执行此操作的一种方法。我阈值,然后视情况清理形态。然后获取轮廓,并为每个轮廓计算其旋转的矩形。然后获取旋转后的矩形的尺寸,并计算纵横比(最大尺寸/最小尺寸)以及面积。然后我对宽高比(以及可选的面积)进行阈值设置,仅保留通过的那些轮廓。
输入:
import cv2
import numpy as np
image = cv2.imread("DCIM-100-MEDIA-DJI-0009-JPG.jpg")
hh, ww = image.shape[:2]
# convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# create a binary thresholded image
thresh = cv2.threshold(gray, 64, 255, cv2.THRESH_BINARY)[1]
# invert so line is white on black background
thresh = 255 - thresh
# apply morphology
kernel = np.ones((11,11), np.uint8)
clean = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
# get external contours
contours = cv2.findContours(clean, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
area_thresh = ww / 2
aspect_thresh = ww / 30
print(area_thresh,aspect_thresh)
print('')
result = image.copy()
for c in contours:
# get rotated rectangle from contour
# get its dimensions
rotrect = cv2.minAreaRect(c)
(center), (dim1,dim2), angle = rotrect
maxdim = max(dim1,dim2)
mindim = min(dim1,dim2)
area = dim1 * dim2
if mindim != 0:
aspect = maxdim / mindim
#print(area, aspect)
#if area > area_thresh and aspect > aspect_thresh:
if aspect > aspect_thresh:
# draw contour on input
cv2.drawContours(result,[c],0,(0,0,255),3)
print(area, aspect)
# save result
cv2.imwrite("DCIM-100-MEDIA-DJI-0009-JPG_thresh.jpg",thresh)
cv2.imwrite("DCIM-100-MEDIA-DJI-0009-JPG_clean.jpg",clean)
cv2.imwrite("DCIM-100-MEDIA-DJI-0009-JPG_result.jpg",result)
# display result
cv2.imshow("thresh", thresh)
cv2.imshow("clean", clean)
cv2.imshow("result", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
形态学清洁图像:
结果图片: