import cv2
import numpy as np
image_path = input("Tanımlama yapılacak JPG dosyasının yolunu girin: ")
image = cv2.imread(image_path)
hsl_lower = [0, 20, 0]
hsl_upper = [4, 100, 100]
hsv_lower = [0, 0, 50]
hsv_upper = [4, 100, 100]
lower_hls = np.array([
int(hsl_lower[0] / 2),
int(hsl_lower[2] * 255 / 100),
int(hsl_lower[1] * 255 / 100)
])
upper_hls = np.array([
int(hsl_upper[0] / 2),
int(hsl_upper[2] * 255 / 100),
int(hsl_upper[1] * 255 / 100)
])
lower_hsv = np.array([
int(hsv_lower[0] / 2),
int(hsv_lower[1] * 255 / 100),
int(hsv_lower[2] * 255 / 100)
])
upper_hsv = np.array([
int(hsv_upper[0] / 2),
int(hsv_upper[1] * 255 / 100),
int(hsv_upper[2] * 255 / 100)
])
hls = cv2.cvtColor(image, cv2.COLOR_BGR2HLS)
mask_hls = cv2.inRange(hls, lower_hls, upper_hls)
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask_hsv = cv2.inRange(hsv, lower_hsv, upper_hsv)
combined_mask = cv2.bitwise_and(mask_hls, mask_hsv)
kernel = np.ones((3, 3), np.uint8)
dilated = cv2.dilate(combined_mask, kernel, iterations=5)
thresh = cv2.threshold(dilated, 60, 255, cv2.THRESH_BINARY)[1]
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
if len(contours) != 0:
cv2.drawContours(image, contours, -1, (255, 0, 0), 2)
largest_contour = max(contours, key=cv2.contourArea)
M = cv2.moments(largest_contour)
if M["m00"] != 0:
point_to_aim = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
cv2.circle(image, point_to_aim, 5, (0, 0, 255), -1)
print(f"Tespit edilen düşman koordinatları: {point_to_aim}")
else:
print("Düşman bulunamadı.")
cv2.imshow("HLS Maskesi", mask_hls)
cv2.imshow("HSV Maskesi", mask_hsv)
cv2.imshow("Birleşik Maske", combined_mask)
cv2.imshow("Sonuç", image)
cv2.waitKey(0)
cv2.destroyAllWindows()