# AAAAAAAAAAAAAAAAAAAA

unknown
python
a year ago
5.1 kB
1
Indexable
Never
```import cv2
import numpy as np

original = image
image2 = original

# Get the shape of the image
height, width = image.shape[:2]
kernel_size = int(max(height, width)*0.1)

# Calculating the scale factor
scale_factor = 1/(min(height, width)/600)

# Define the font type, scale, and thickness
font_scale = height * 0.05 / 24 # 24 is the height of the font
font = cv2.FONT_HERSHEY_SIMPLEX
thickness = 10

# Calculate the kernel size as a fraction of the image size

# Make sure the kernel size is odd
if kernel_size % 2 == 0:
kernel_size += 1

print("kernel size = " + str(kernel_size))
# Create the kernel
kernel_size = 17
kernel = (kernel_size, kernel_size)

#######################################################################

# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Blur the image to reduce noise
blurred = cv2.medianBlur(gray, kernel_size)

# Blur2 the image to reduce noise
blurred = cv2.GaussianBlur(gray, kernel, 3)

# Calculating the maximuv value of the pixel in the blurred image
maximum = np.max(blurred)

# Threshold the image to extract the object
darkest_px = np.min(gray)
_, thresh = cv2.threshold(blurred, darkest_px+50, 255, cv2.THRESH_BINARY)

# Blur the image to reduce noise
blurred2 = cv2.medianBlur(thresh, kernel_size)
# Calculating the maximuv value of the pixel in the blurred image
maximum2 = np.max(blurred2)

# Undo the Erosiohn
dilated = cv2.dilate(thresh, kernel, iterations=4)

# Calculate the median of the image to use as the lower threshold
median = np.median(thresh2)

# # Set the lower and upper thresholds
lower = int(max(0, 0.7 * median))
upper = int(min(255, 1.3 * median))

# Try to use canny

canny = cv2.Canny(thresh2, lower,upper)
blank = np.zeros(image.shape, dtype='uint8')

# Eroding the image
eroded = cv2.erode(canny, (3,3), iterations=1)

contours, hierarchies = cv2.findContours(thresh2, cv2.RETR_LIST, cv2.CHAIN_APPROX_TC89_KCOS)

if len(contours) > 0:
print("N. contours found: ")
print(len(contours))
cv2.drawContours(blank, contours, -1, (0,0, 255), 50)
cv2.imshow("Contours", cv2.resize(blank, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_AREA))
else:
print("No contours found")

# Displaying the images

images = [image, blurred,  thresh, thresh2, canny, eroded]
titles = ["Gray", "Blurred", "Thresh", "Thresh2", "Canny Edges", "Eroded"]

# images = [image]
# titles = ["Gray"]

# Calculate Harris corners
gray = cv2.cvtColor(blank, cv2.COLOR_BGR2GRAY)
h_corners = np.float32(gray)

###############################################################################################################################

# Defining the variables for the Harris corner function
window_size = 2
Sobel_derivative_aperture = 3
Harris_detector_free_parameter = 0.04

# Generating the text
text = "(" + str(window_size) + " "+ str(Sobel_derivative_aperture) +" "+ str(Harris_detector_free_parameter) + ")"

# Get the size of the text
text_size, baseline = cv2.getTextSize(text, font, font_scale, thickness)

# Define the text origin and bottom-left position
text_x = width - text_size[0] - 10
text_y = text_size[1] + 10
text = ""

while 1:

# Draw the text on the image
dst = cv2.cornerHarris(h_corners, window_size, Sobel_derivative_aperture, Harris_detector_free_parameter)

# Threshold the result
dst = cv2.dilate(dst, None)
image2[dst > 0.01 * dst.max()] = [0, 0, 255]

# Generating the text
text = "(" + str(window_size) + " "+ str(Sobel_derivative_aperture) +" "+ str(Harris_detector_free_parameter) + ")"

# Adding the text to the image
cv2.putText(image2, text, (text_x, text_y), font, font_scale, (0, 0, 255), thickness)
cv2.destroyAllWindows()
cv2.imshow("Test",  cv2.resize(image2, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_AREA))
cv2.imshow("Original",  cv2.resize(original, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_AREA))
# image2 = original
text = ""
key = cv2.waitKey(0)

if key == ord('r'):
break
elif key == ord('q'):
window_size = window_size + 1
elif key == ord('a'):
window_size = window_size - 1

elif key == ord('w'):
Sobel_derivative_aperture = Sobel_derivative_aperture + 2
elif key == ord('s'):
Sobel_derivative_aperture = Sobel_derivative_aperture - 2

elif key == ord('e'):
Harris_detector_free_parameter = Harris_detector_free_parameter + 0.1
elif key == ord('d'):
Harris_detector_free_parameter = Harris_detector_free_parameter - 0.1

cv2.destroyAllWindows()

for i, img in enumerate(images):
img = cv2.resize(img, None, fx=scale_factor, fy=scale_factor, interpolation=cv2.INTER_AREA)
cv2.imshow(titles[i], img)

cv2.waitKey(0)
cv2.destroyAllWindows()
```