Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用OpenCV验证绘制的线与计算机生成的线的相似性_Opencv_Line_Similarity - Fatal编程技术网

使用OpenCV验证绘制的线与计算机生成的线的相似性

使用OpenCV验证绘制的线与计算机生成的线的相似性,opencv,line,similarity,Opencv,Line,Similarity,我在另一张纸上随意画了一条蜿蜒的线。在Python和OpenCV中,我希望验证我绘制的线条是否与计算机绘制的线条相同(或者说这条线条足够相似,达到一定的百分比)。这可能吗?我已经附上了图片。 非常感谢。 安得烈 ,这里有一种在Python/OpenCV中使用形状匹配的方法 读取两个输入图像并计算其中心 将两个图像的中心裁剪为一些共同的大小 将它们转换为灰色 大津阈值法 将形态学腐蚀应用于手绘图形,使线条与计算机生成的图像中的线条一样粗 进行形状匹配并获得3种不同的度量距离 计算机生成的图形:

我在另一张纸上随意画了一条蜿蜒的线。在Python和OpenCV中,我希望验证我绘制的线条是否与计算机绘制的线条相同(或者说这条线条足够相似,达到一定的百分比)。这可能吗?我已经附上了图片。 非常感谢。 安得烈
,这里有一种在Python/OpenCV中使用形状匹配的方法

  • 读取两个输入图像并计算其中心
  • 将两个图像的中心裁剪为一些共同的大小
  • 将它们转换为灰色
  • 大津阈值法
  • 将形态学腐蚀应用于手绘图形,使线条与计算机生成的图像中的线条一样粗
  • 进行形状匹配并获得3种不同的度量距离
计算机生成的图形:

手绘图形:


阈值计算机图形:

阈值化和腐蚀绘制的图形:

结果分数列在上面的代码中


有关距离度量,请参见尝试形状匹配。看,谢谢,为什么你会完全错误地匹配纯白色图像和全黑色图像?我想看看匹配指标的极限值范围。所以我添加了另外两个不匹配比较。
import cv2
import numpy as np

# read computer generated figure and find center
img1 = cv2.imread('computer_figure.jpg')
hh1, ww1 = img1.shape[:2]
cx1 = ww1 // 2
cy1 = hh1 // 2

# read hand drawn figure and find center
img2 = cv2.imread('drawn_figure.jpg')
hh2, ww2 = img2.shape[:2]
cx2 = ww2 // 2
cy2 = hh2 // 2

# specify crop size and crop both images
wd = 1450
ht = 1450
xoff = wd // 2
yoff = ht // 2
img1_crop = img1[cy1-yoff:cy1+yoff, cx1-xoff:cx1+xoff]
img2_crop = img2[cy2-yoff:cy2+yoff, cx2-xoff:cx2+xoff]

# convert to grayscale
gray1 = cv2.cvtColor(img1_crop,cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2_crop,cv2.COLOR_BGR2GRAY)

# threshold
thresh1 = cv2.threshold(gray1, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
thresh2 = cv2.threshold(gray2, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]

# erode thresh2 to get black lines approx as thick as thresh1
# apply close and open morphology to fill tiny black and white holes and save as mask
kernel = np.ones((13,13), np.uint8)
thresh2 = cv2.morphologyEx(thresh2, cv2.MORPH_ERODE, kernel)

# do shape matching (the smaller the distance the better the match)
distance1 = cv2.matchShapes(thresh1, thresh2, cv2.CONTOURS_MATCH_I1, 0)
distance2 = cv2.matchShapes(thresh1, thresh2, cv2.CONTOURS_MATCH_I2, 0)
distance3 = cv2.matchShapes(thresh1, thresh2, cv2.CONTOURS_MATCH_I3, 0)
print("distance 1:",distance1)
print("distance 2:",distance2)
print("distance 3:",distance3)
print("")

#distance 1: 0.00019690372821457025
#distance 2: 0.001971857215556483
#distance 3: 0.0006233041352955213

# compare to mis-match with pure white image
thresh3 = np.full_like(thresh1, 255)
distance1 = cv2.matchShapes(thresh3, thresh2, cv2.CONTOURS_MATCH_I1, 0)
distance2 = cv2.matchShapes(thresh3, thresh2, cv2.CONTOURS_MATCH_I2, 0)
distance3 = cv2.matchShapes(thresh3, thresh2, cv2.CONTOURS_MATCH_I3, 0)
print("distance 1:",distance1)
print("distance 2:",distance2)
print("distance 3:",distance3)
print("")

#distance 1: 0.0019009881608588741
#distance 2: 0.019164295934527953
#distance 3: 0.006017629998960382

# compare to total mis-match of pure white image with pure black image
thresh4 = np.zeros_like(thresh1)
distance1 = cv2.matchShapes(thresh3, thresh4, cv2.CONTOURS_MATCH_I1, 0)
distance2 = cv2.matchShapes(thresh3, thresh4, cv2.CONTOURS_MATCH_I2, 0)
distance3 = cv2.matchShapes(thresh3, thresh4, cv2.CONTOURS_MATCH_I3, 0)
print("distance 1:",distance1)
print("distance 2:",distance2)
print("distance 3:",distance3)
print("")

#distance 1: 1.7976931348623157e+308
#distance 2: 1.7976931348623157e+308
#distance 3: 1.7976931348623157e+308

# save cropped image
cv2.imwrite('drawn_figure_thresh.jpg',thresh1)
cv2.imwrite('computer_figure_thresh.jpg',thresh2)

# show the images
cv2.imshow("crop1", img1_crop)
cv2.imshow("crop2", img2_crop)
cv2.imshow("thresh1", thresh1)
cv2.imshow("thresh2", thresh2)
cv2.waitKey(0)
cv2.destroyAllWindows()