Question
Break the curve wherever there is a significant change in the curve
Wherever there's a significant change in the curve, I need to break the curve with a break thickness of just 1 pixel (I just want to break the curves into multiple parts). I have attached an image for reference. So after i read the image, i am thinning the curve and wherever there are red dots, i need to split it around that area.
The first image is the input image and red dots indicate where I want the cut (the image will not actually have the red dot)/ The second image is the current output that I am getting. The third image is the unaltered image for reference.
I have tried implementing the following codes:
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()
and the other logic is
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
So far I am not getting what I want. Its breaking at multiple points and not just where I intend it to. Is there any library or and code to do this?