本算法旨在对 CAD 图纸进行比对,通过一系列图像处理步骤,包括格式转换、边缘切割对齐、去噪、差值比对、腐蚀膨胀和标注归一,快速发现两幅 CAD 图纸之间的差异。原文地址:https://kns.cnki.net/kcms2/article/abstract?v=QenloEQs_R_P8kxDecokeBUD05QHydGF4xG_296KoAoDsp8OX7i3Q7IeoGFGj8KFvSSH-Re2Tuqm4QoRztSGCbWOodm65_GtpA1zMPijQCGheGM5ScU36ndre7Gv3rHLdvTX1nkIljt84rKdxM91ntHDJ4FfimYaVw5obiB1zsw=&uniplatform=NZKPT
为解决人工比对 CAD 图纸效率低且易受主观因素影响的问题,提出一种基于图像处理的 CAD 图纸比对算法,通过一系列图像处理步骤实现自动比对,快速发现图纸差异。
import cv2
# 读取转换后的PDF图像(假设已转换为可识别的图像格式,如JPEG或PNG)
image1 = cv2.imread('image1.jpg') # 第一张图纸图像
image2 = cv2.imread('image2.jpg') # 第二张图纸图像
(2)图纸边缘切割对齐
def edge_cutting_and_alignment(image):
edges = cv2.Canny(image, 100, 200)
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
rect = cv2.boundingRect(max(contours, key=cv2.contourArea))
angle = cv2.minAreaRect(max(contours, key=cv2.contourArea))[2]
center = (rect[0] + rect[2] // 2, rect[1] + rect[3] // 2)
rotated_image = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated_image = cv2.warpAffine(image, rotated_image, (image.shape[1], image.shape[0]))
cropped_image = rotated_image[rect[1]:rect[1] + rect[3], rect[0]:rect[0] + rect[2]]
return cropped_image
gray_image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
gray_image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
aligned_image1 = edge_cutting_and_alignment(gray_image1)
aligned_image2 = edge_cutting_and_alignment(gray_image2)
(3)高斯滤波去噪
def gaussian_filtering(image):
return cv2.GaussianBlur(image, (3, 3), 0)
filtered_image1 = gaussian_filtering(aligned_image1)
filtered_image2 = gaussian_filtering(aligned_image2)
def difference_comparison(image1, image2):
diff = cv2.absdiff(image1, image2)
_, binary_diff = cv2.threshold(diff, 30, 255, cv2.THRESH_BINARY)
return binary_diff
diff_image = difference_comparison(filtered_image1, filtered_image2)
腐蚀操作
膨胀操作
def erosion_and_dilation(image):
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
eroded_image = cv2.erode(image, kernel, iterations=1)
dilated_image = cv2.dilate(eroded_image, kernel, iterations=1)
return dilated_image
processed_image = erosion_and_dilation(diff_image)
(6)标注归一
def annotation_normalization(image, original_image):
contours, _ = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in of the above is just an example and may need to be adjusted according to the actual situation. You can further optimize the code and parameters according to your own needs.
结果
编程语言:Python 主要库:OpenCV(用于图像处理)
希望对你有帮助!加油!
若您认为本文内容有益,请不吝赐予赞同并订阅,以便持续接收有价值的信息。衷心感谢您的关注和支持!