Find The Center Of A White Line In An Image Using OpenCV · GitHub

Skip to content All gists Back to GitHub Sign in Sign up Sign in Sign up Dismiss alert {{ message }}

Instantly share code, notes, and snippets.

@araffin araffin/color_mask.py Created October 6, 2017 15:56 Show Gist options
  • Star () You must be signed in to star a gist
  • Fork () You must be signed in to fork a gist
  • Embed Clone this repository at <script src="https://gist.github.com/araffin/6728aec43b2ed6b1b07738f4c3a42cf5.js"></script>
  • Save araffin/6728aec43b2ed6b1b07738f4c3a42cf5 to your computer and use it in GitHub Desktop.
Code Revisions 1 Stars 3 Embed Clone this repository at <script src="https://gist.github.com/araffin/6728aec43b2ed6b1b07738f4c3a42cf5.js"></script> Save araffin/6728aec43b2ed6b1b07738f4c3a42cf5 to your computer and use it in GitHub Desktop. Download ZIP Find the center of a white line in an image using OpenCV Raw color_mask.py This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters Show hidden characters
from __future__ import division
import cv2
import numpy as np
# Input Image
image = cv2.imread("my_image.jpg")
# Convert to HSV color space
hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
# Define range of white color in HSV
lower_white = np.array([0, 0, 212])
upper_white = np.array([131, 255, 255])
# Threshold the HSV image
mask = cv2.inRange(hsv, lower_white, upper_white)
# Remove noise
kernel_erode = np.ones((4,4), np.uint8)
eroded_mask = cv2.erode(mask, kernel_erode, iterations=1)
kernel_dilate = np.ones((6,6),np.uint8)
dilated_mask = cv2.dilate(eroded_mask, kernel_dilate, iterations=1)
# Find the different contours
im2, contours, hierarchy = cv2.findContours(dilated_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Sort by area (keep only the biggest one)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:1]
if len(contours) > 0:
M = cv2.moments(contours[0])
# Centroid
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
print("Centroid of the biggest area: ({}, {})".format(cx, cy))
else:
print("No Centroid Found")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment You can’t perform that action at this time.

Từ khóa » Hsv Color Space White