I am currently working on identifying lines or curves within an image and assessing whether they intersect. This is illustrated in the images below:
In Image 1 and Image 2, the lines or curves intersect.
In Image 3, the lines or curves are parallel and do not intersect.
expected outputs :
I have attempted the following code in pursuit of achieving my objective; however, I am encountering challenges. The code seems to identify multiple separate lines or fragmented curves rather than treating them as cohesive entities. Additionally, I am struggling to accurately determine whether these lines or curves intersect.
def check_intersect(line1, line2):
x1, y1, x2, y2 = line1[0]
x3, y3, x4, y4 = line2[0]
# Calculate the determinants
det1 = (x1*y2 - y1*x2)
det2 = (x3*y4 - y3*x4)
det = (x1 - x2)*(y3 - y4) - (y1 - y2)*(x3 - x4)
# If the determinant is zero, the lines are parallel
if det == 0:
return False
# Calculate the x and y coordinates of the intersection point
x = (det1*(x3 - x4) - (x1 - x2)*det2) / det
y = (det1*(y3 - y4) - (y1 - y2)*det2) / det
# Check if the intersection point lies on both lines
if min(x1, x2) <= x <= max(x1, x2) and min(y1, y2) <= y <= max(y1, y2) and min(x3, x4) <= x <= max(x3, x4) and min(y3, y4) <= y <= max(y3, y4):
return True
return False
def find_intersect(crop_roi):
gray = cv2.cvtColor(crop_roi, cv2.COLOR_BGR2GRAY)
# Apply Gaussian blur
image = cv2.GaussianBlur(gray, (5, 5), 0)
# Apply Canny Edge Detection
edges = cv2.Canny(image, 50, 150)
# Hough Line Transform
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength=100, maxLineGap=100)
# lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100)
# Draw lines on the image
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(image, (x1, y1), (x2, y2), (255, 0, 0), 3)
# Check every pair of lines for intersection
for i in range(len(lines)):
for j in range(i + 1, len(lines)):
if check_intersect(lines[i], lines[j]):
print("Lines", i, "and", j, "intersect.")
return True ,image
return False , image
for img_f in tqdm(get_all_imgs):
filename = img_f.split('\\')[-1]
read_img = cv2.imread(img_f)
# read_img.shape
# Convert the image to HSV colorspace
hsv = cv2.cvtColor(read_img, cv2.COLOR_BGR2HSV)
# Define the color range for the object of interest
lower_range = np.array([58, 255, 0])
upper_range = np.array([179, 255, 255])
# Create a mask of the HSV image
mask = cv2.inRange(hsv, lower_range, upper_range)
contours, _ = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
unique_contours = []
for c in contours:
x,y,w,h = cv2.boundingRect(c)
if (x,y,w,h) not in unique_contours:
unique_contours.append((x,y,w,h))
bbox1 = unique_contours[0]
bbox2 = unique_contours[1]
# Extract the individual components
x1, y1, w1, h1 = bbox1
x2, y2, w2, h2 = bbox2
bbox1_center = ((x1+x1+w1)/2 , (y1+y1+h1)/2)
bbox2_center = ((x2+x2+w2)/2 , (y2+y2+h2)/2)
# Draw rectangles on the image
color = (0, 0, 255) # BGR color for OpenCV (red)
thickness = 3
xmin = int(bbox1_center[0])
ymin = int(bbox1_center[1])
xmax = int(bbox2_center[0])
ymax = int(bbox2_center[1])
# image = cv2.rectangle(read_img, (xmin,ymin), (xmax,ymax), color, thickness)
# Calculate the center coordinates
center_x = (xmin + xmax) // 2
center_y = (ymin + ymax) // 2
# Define circle parameters
circle_radius = 100
circle_color = (0, 255, 0) # Green color
circle_thickness = -1 # Negative thickness fills the circle
# Draw the rectangle on the image
rectangle_color = (0, 0, 255) # Red color for rectangle
rectangle_thickness = 2
# cv2.rectangle(read_img, (xmin, ymin), (xmax, ymax), rectangle_color, rectangle_thickness)
# Draw the circle at the center
# cv2.circle(read_img, (center_x, center_y), circle_radius, circle_color, circle_thickness)
# plt.imshow(read_img[:,:,::-1])
crop_roi = read_img[ymax:ymin,xmax:xmin]
try:
# Convert the image to gray scale
final_result,result_obj = find_intersect(crop_roi)
except Exception as e :
continue
# Set the text and position
if final_result:
text = "Line Intercept"
else :
text = "Line not Intercept"
I am seeking guidance on the approach I should adopt to overcome these obstacles and successfully fulfill my requirements. Any assistance or suggestions would be greatly appreciated.