如何在图像曲线的显著变化位置实现1像素宽度切割?
问题描述
- 处理流程:读取图像后,需先对曲线执行**thinning(细化)**处理
- 核心需求:在曲线发生显著变化的位置,将曲线切割为多个部分,切割宽度仅为1像素;切割位置需对应参考图中标注红点的位置(实际待处理图像无红点)
参考图说明:
- 第一张:标记切割位置的输入图
- 第二张:当前输出图
- 第三张:原始参考图
已尝试代码
第一段代码
import cv2 import numpy as np from matplotlib import pyplot as plt image_path = rf'C:\Users\User\Desktop\output.png' img = cv2.imread(image_path, 0) ret, binary = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY) binary = cv2.ximgproc.thinning(binary, thinningType=cv2.ximgproc.THINNING_GUOHALL) coords = np.column_stack(np.where(binary > 0)) def calculateAngle(p1, p2, p3): v1 = np.array(p2) - np.array(p1) v2 = np.array(p3) - np.array(p2) angle = np.arctan2(v2[1], v2[0]) - np.arctan2(v1[1], v1[0]) angle = np.degrees(angle) if angle < 0: angle += 360 return angle startBlackImg = np.zeros((binary.shape[0], binary.shape[1], 1), np.uint8) i = 1 while i < (len(coords) - 1): p1 = coords[i - 1] p2 = coords[i] p3 = coords[i + 1] i += 1 angle = calculateAngle(p1, p2, p3) if angle < 45 or angle > 315: startBlackImg[p2[0], p2[1]] = 255 else: startBlackImg[p2[0], p2[1]] = 0 cv2.namedWindow('Check', 0) cv2.imshow('Check', startBlackImg) cv2.waitKey(0) cv2.destroyAllWindows()
第二段逻辑代码
while kk < len(cutContour) - 10: xCdte = cutContour[kk][0][0] yCdte = cutContour[kk][0][1] xNextCdte = cutContour[kk + 10][0][0] yNextCdte = cutContour[kk + 10][0][1] kk += 1 if totalDistance <= 0.3048: startBlackImg[yCdte, xCdte] = np.array([255, 255, 255]) startBlackImg[yNextCdte, xNextCdte] = np.array([255, 255, 255]) else: if (abs(xCdte - xNextCdte) < 10 and abs(yCdte - yNextCdte) >= 10) or (abs(xCdte - xNextCdte) >= 10 and abs(yCdte - yNextCdte) < 10): startBlackImg[yCdte, xCdte] = np.array([255, 255, 255]) startBlackImg[yNextCdte, xNextCdte] = np.array([255, 255, 255]) else: startBlackImg[yCdte, xCdte] = np.array([0, 0, 0]) kk += 10
当前问题
目前实现的代码会在多个非预期位置切割曲线,无法达到需求,求合适的库或代码实现该功能。
内容的提问来源于stack exchange,提问作者AHFromHogwarts
相关产品推荐
相关产品推荐

