Identify if a Specific Edge Pixel Belongs to Horizontal, Vertical or Diagonal Edge Line I have a code below that identifies the edge of the text image. Input image is:



Output display is:



I want to select random points near the edge border of the text. I was thinking to identify if the edge pixel is part of the horizontal or vertical line. If it  is part of the horizontal line, I can choose a vertical random point, else if the edge pixel is part of the vertical line, then I can choose a random horizontal point. How to it in Python? Aside from my logic, is there any better way to select the random points? Below is my initial code:

from PIL import Image
from matplotlib import pyplot as plt
import numpy as np
import cv2

image_counter = 1
path = 'sample/' + str(image_counter) + '.png'
image = Image.open(path).convert('L')
image_width, image_height = image.size
image = np.array(image)

sigma = .33
# compute the median of the single channel pixel intensities
v = np.median(image)
# apply automatic Canny edge detection using the computed median
lower = int(max(0, (1.0 - sigma) * v))
upper = int(min(255, (1.0 + sigma) * v))


#Edge Detection
edge_image = cv2.Canny(image,lower,upper)
plt.subplot(121),plt.imshow(image,cmap = 'gray')
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(edge_image,cmap = 'gray')
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
plt.show()

#Select random points outside text borders
#Select random points outside text borders

for x in range(image_width):
    for y in range(image_height):
        if (edge_image[x][y] == 255):
            #Check if the edge pixel is part of the horizontal or vertical line.
            #If part of the horizontal line, pick a random point vertically.
            #If part of the vertical line, pick a random point horizontally.
            # How to do it?


Desired output:
Not sure how you do it in python, but the idea can be as simple as the following:

After you select a random point on the edge pixel, calculate the horizontal gradient and the vertical gradient with this pixel at the centre of the gradient filter.

The width of the  kernel for the gradient filters will depend on the maximum width of the edges. For example if the edges are 3 pixel wide have a 5x5 gradient filter.

If the magnitude of vertical gradient is more than horizontal gradient then the pixel belongs to horizontal edge and vice versa for vertical edge.

Also for diagonal edge similarly you can have a kernel which calculated diagonal gradients, if diagonal gradients are not exactly 45 degrees, you might need to to increase the size of the kernel to get more angular resolution of gradients.

Hope that helps.