I would like to count the number of sheets in a stack, as you can see in a side view of the stack.
I have already implemented some solutions but It did not work. I still get the number of the lines 0 as output. Is there anyone who may support me to fix it?
Fyi: The output image is attached after canny edge detection. Thanks in advance!
import numpy as np
import os
import cv2
import matplotlib.pyplot as plt
import scipy
from skimage import io
def Canny_detector(img, weak_th=None, strong_th=None):
# conversion of image to grayscale
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Noise reduction step
img = cv2.GaussianBlur(img, (5, 5), 1.4)
# Calculating the gradients
gx = cv2.Sobel(np.float32(img), cv2.CV_64F, 1, 0, 3)
gy = cv2.Sobel(np.float32(img), cv2.CV_64F, 0, 1, 3)
# Conversion of Cartesian coordinates to polar
mag, ang = cv2.cartToPolar(gx, gy, angleInDegrees=True)
# setting the minimum and maximum thresholds
# for double thresholding
mag_max = np.max(mag)
if not weak_th: weak_th = mag_max * 0.1
if not strong_th: strong_th = mag_max * 0.5
# getting the dimensions of the input image
height, width = img.shape
# Looping through every pixel of the grayscale
# image
for i_x in range(width):
for i_y in range(height):
grad_ang = ang[i_y, i_x]
grad_ang = abs(grad_ang - 180) if abs(grad_ang) > 180 else abs(grad_ang)
#print("yyy")
# selecting the neighbours of the target pixel
# according to the gradient direction
# In the x axis direction
if grad_ang <= 22.5:
neighb_1_x, neighb_1_y = i_x - 1, i_y
neighb_2_x, neighb_2_y = i_x + 1, i_y
# top right (diagonal-1) direction
elif grad_ang > 22.5 and grad_ang <= (22.5 + 45):
neighb_1_x, neighb_1_y = i_x - 1, i_y - 1
neighb_2_x, neighb_2_y = i_x + 1, i_y + 1
# In y-axis direction
elif grad_ang > (22.5 + 45) and grad_ang <= (22.5 + 90):
neighb_1_x, neighb_1_y = i_x, i_y - 1
neighb_2_x, neighb_2_y = i_x, i_y + 1
# top left (diagonal-2) direction
elif grad_ang > (22.5 + 90) and grad_ang <= (22.5 + 135):
neighb_1_x, neighb_1_y = i_x - 1, i_y + 1
neighb_2_x, neighb_2_y = i_x + 1, i_y - 1
# Now it restarts the cycle
elif grad_ang > (22.5 + 135) and grad_ang <= (22.5 + 180):
neighb_1_x, neighb_1_y = i_x - 1, i_y
neighb_2_x, neighb_2_y = i_x + 1, i_y
# Non-maximum suppression step
if width > neighb_1_x >= 0 and height > neighb_1_y >= 0:
if mag[i_y, i_x] < mag[neighb_1_y, neighb_1_x]:
mag[i_y, i_x] = 0
continue
if width > neighb_2_x >= 0 and height > neighb_2_y >= 0:
if mag[i_y, i_x] < mag[neighb_2_y, neighb_2_x]:
mag[i_y, i_x] = 0
weak_ids = np.zeros_like(img)
strong_ids = np.zeros_like(img)
ids = np.zeros_like(img)
# double thresholding step
for i_x in range(width):
for i_y in range(height):
grad_mag = mag[i_y, i_x]
if grad_mag < weak_th:
mag[i_y, i_x] = 0
elif strong_th > grad_mag >= weak_th:
ids[i_y, i_x] = 1
else:
ids[i_y, i_x] = 2
# finally returning the magnitude of gradients of edges
return mag
frame = cv2.imread('/Users/Projects/Image/IMG1.jpg')
print("Hi there")
# calling the designed function for finding edges
canny_img = Canny_detector(frame)
# Displaying the input and output image
plt.figure()
plot1 = plt.figure(1)
plt.imshow(frame)
plot2 = plt.figure(2)
plt.imshow(canny_img)
print("Hallo Hallo")
plt.show()
#J. Canny. 1986. (Canny)
#Smooth Image with Gaussian filter
#Compute Derivative of filtered image
#Find Magnitude and Orientation of gradient
#Apply Non-max suppression
#Apply Thresholding (Hysteresis)
rho = 1 # distance resolution in pixels of the Hough grid
theta = np.pi / 180 # angular resolution in radians of the Hough grid
threshold = 15 # minimum number of votes (intersections in Hough grid cell)
min_line_length = 50 # minimum number of pixels making up a line
max_line_gap = 20 # maximum gap in pixels between connectable line segments
line_image = np.copy(frame) * 0 # creating a blank to draw lines on
print(rho)
print("Hey there")
# After you apply Hough on edge detected image. Let's define the function which turns
these edges into lines
canny_img = canny_img.astype(np.uint8)
lines = cv2.HoughLinesP(canny_img, rho, theta, threshold, np.array([]),
min_line_length, max_line_gap)
print(lines)
# calculate the distances between points (x1,y1), (x2,y2) :
distance = []
for line in lines:
distance.append(np.linalg.norm(line[:,:2] - line[:,2:]))
print(distance)
print('max distance:', max(distance), '\nmin distance:', min(distance))
# Adjusting the best distance
bestDistance=1110
numberOfLines=[]
count=0
for x in distance:
if x>bestDistance:
numberOfLines.append(x)
count=count+1
print('Number of lines:', count)