Home
English العربية

🎯 X or O Detector


Result will appear here...

🧠 How the Detection Algorithm Works

🔍 X Detection Strategy:

  • • Scans diagonal lines from top-left to bottom-right and top-right to bottom-left
  • • Checks for crossing patterns in the center area (where diagonals intersect)
  • • Weights center crossings more heavily as they're characteristic of X shapes

⭕ O Detection Strategy:

  • • Samples pixels in circular patterns at multiple radii (15, 20, 25, 30 pixels)
  • • Checks for hollow center - a key characteristic of O shapes
  • • Awards bonus points when center area is mostly empty (60%+ hollow)

⚖️ Decision Making Process:

  • • Calculates separate scores for X and O patterns
  • • Requires 20% score difference for high confidence classification
  • • Uses fallback heuristics when scores are close (hollow center, total dark pixels)
  • • Provides confidence levels: High, Medium, or Low based on score differences

💡 Note: This algorithm is completely deterministic and uses geometric pattern recognition without any AI or machine learning.

🐍 Python Implementation

import numpy as np from PIL import Image import math def analyze_image(image_path): # Load and convert image to grayscale img = Image.open(image_path).convert('L') img = img.resize((100, 100)) # Resize to 100x100 data = np.array(img) w, h = data.shape center_x, center_y = w // 2, h // 2 # Enhanced diagonal pattern detection (X pattern) diag_score = 0 for i in range(15, 85, 3): # Top-left to bottom-right diagonal x1 = int((i / 100) * w) y1 = int((i / 100) * h) # Top-right to bottom-left diagonal x2 = int(((100 - i) / 100) * w) y2 = int((i / 100) * h) if is_dark(data[y1, x1]): diag_score += 1 if is_dark(data[y2, x2]): diag_score += 1 # Check for crossing pattern in center area center_crossing = 0 for offset in range(-8, 9, 2): x1, y1 = center_x + offset, center_y + offset x2, y2 = center_x - offset, center_y + offset if 0 <= x1 < w and 0 <= y1 < h and is_dark(data[y1, x1]): center_crossing += 1 if 0 <= x2 < w and 0 <= y2 < h and is_dark(data[y2, x2]): center_crossing += 1 # Enhanced circular pattern detection (O pattern) circle_score = 0 radiuses = [15, 20, 25, 30] for radius in radiuses: for angle in range(0, 360, 15): x = int(center_x + radius * math.cos(math.radians(angle))) y = int(center_y + radius * math.sin(math.radians(angle))) if 0 <= x < w and 0 <= y < h and is_dark(data[y, x]): circle_score += 1 # Check for hollow center (characteristic of O) center_hollow = 0 center_total = 0 for x in range(center_x - 10, center_x + 11, 2): for y in range(center_y - 10, center_y + 11, 2): if 0 <= x < w and 0 <= y < h: center_total += 1 if not is_dark(data[y, x]): center_hollow += 1 # Enhanced scoring system x_score = diag_score + center_crossing * 2 o_score = circle_score + (20 if center_hollow > center_total * 0.6 else 0) # Decision making if x_score > o_score * 1.2: return "X", "High" elif o_score > x_score * 1.2: return "O", "High" else: return ("X" if x_score > o_score else "O"), "Low" def is_dark(pixel_value): return pixel_value < 100 # Threshold for dark pixels