Skip to content

Instantly share code, notes, and snippets.

@azidanit
Created April 29, 2021 06:02
Show Gist options
  • Save azidanit/df7108b7287ab99d200eaae9a33fb348 to your computer and use it in GitHub Desktop.
Save azidanit/df7108b7287ab99d200eaae9a33fb348 to your computer and use it in GitHub Desktop.
PCD Tugas \Segmentasi
import cv2 as cv
import numpy as np
import math
#saved
low_H_set = 0
high_H_set = 25
low_S_set = 32
high_S_set = 255
low_V_set = 197
high_V_set = 255
erode_set = 1
dilate_set = 3
max_value = 255
max_value_H = 360 // 2
low_H = 0
low_S = 0
low_V = 0
high_H = max_value_H
high_S = max_value
high_V = max_value
low_H_name = 'Low H'
low_S_name = 'Low S'
low_V_name = 'Low V'
high_H_name = 'High H'
high_S_name = 'High S'
high_V_name = 'High V'
window_capture_name = 'Video Capture'
window_detection_name = 'Object Detection'
window_morph_name = 'Morphology'
window_hough_line = 'Hough Line'
erosion_name = 'Erosion Kernel Size'
dilation_name = 'Dilation Kernel Size'
max_kernel_size = 21
erode_size = 0
dilate_size = 0
def on_low_H_thresh_trackbar(val):
global low_H
global high_H
low_H = val
low_H = min(high_H - 1, low_H)
cv.setTrackbarPos(low_H_name, window_detection_name, low_H)
def on_high_H_thresh_trackbar(val):
global low_H
global high_H
high_H = val
high_H = max(high_H, low_H + 1)
cv.setTrackbarPos(high_H_name, window_detection_name, high_H)
def on_low_S_thresh_trackbar(val):
global low_S
global high_S
low_S = val
low_S = min(high_S - 1, low_S)
cv.setTrackbarPos(low_S_name, window_detection_name, low_S)
def on_high_S_thresh_trackbar(val):
global low_S
global high_S
high_S = val
high_S = max(high_S, low_S + 1)
cv.setTrackbarPos(high_S_name, window_detection_name, high_S)
def on_low_V_thresh_trackbar(val):
global low_V
global high_V
low_V = val
low_V = min(high_V - 1, low_V)
cv.setTrackbarPos(low_V_name, window_detection_name, low_V)
def on_high_V_thresh_trackbar(val):
global low_V
global high_V
high_V = val
high_V = max(high_V, low_V + 1)
cv.setTrackbarPos(high_V_name, window_detection_name, high_V)
def on_dilate_thresh_trackbar(val):
global dilate_size
dilate_size = val
dilate_size = max(dilate_size, 0)
cv.setTrackbarPos(dilation_name, window_morph_name, dilate_size)
def on_erode_thresh_trackbar(val):
global erode_size
erode_size = val
erode_size = max(erode_size, 0)
cv.setTrackbarPos(erosion_name, window_morph_name, erode_size)
img = cv.imread('img/4.png')
img = cv.resize(img, (640,480))
cv.namedWindow(window_capture_name)
cv.namedWindow(window_detection_name)
cv.namedWindow(window_morph_name)
cv.createTrackbar(low_H_name, window_detection_name, low_H, max_value_H, on_low_H_thresh_trackbar)
cv.createTrackbar(high_H_name, window_detection_name, high_H, max_value_H, on_high_H_thresh_trackbar)
cv.createTrackbar(low_S_name, window_detection_name, low_S, max_value, on_low_S_thresh_trackbar)
cv.createTrackbar(high_S_name, window_detection_name, high_S, max_value, on_high_S_thresh_trackbar)
cv.createTrackbar(low_V_name, window_detection_name, low_V, max_value, on_low_V_thresh_trackbar)
cv.createTrackbar(high_V_name, window_detection_name, high_V, max_value, on_high_V_thresh_trackbar)
cv.createTrackbar(dilation_name, window_morph_name, dilate_size, max_kernel_size, on_dilate_thresh_trackbar)
cv.createTrackbar(erosion_name, window_morph_name, erode_size, max_kernel_size, on_erode_thresh_trackbar)
on_low_H_thresh_trackbar(low_H_set)
on_high_H_thresh_trackbar(high_H_set)
on_low_S_thresh_trackbar(low_S_set)
on_high_S_thresh_trackbar(high_S)
on_low_V_thresh_trackbar(low_V_set)
on_high_V_thresh_trackbar(high_V_set)
on_erode_thresh_trackbar(erode_set)
on_dilate_thresh_trackbar(dilate_set)
while True:
frame = img
if frame is None:
break
frame_HSV = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
frame_threshold = cv.inRange(frame_HSV, (low_H, low_S, low_V), (high_H, high_S, high_V))
element = cv.getStructuringElement(cv.MORPH_RECT, (2 * erode_size + 1, 2 * erode_size + 1),
(erode_size, erode_size))
erosion_dst = cv.erode(frame_threshold, element)
element = cv.getStructuringElement(cv.MORPH_RECT, (2 * dilate_size + 1, 2 * dilate_size + 1),
(dilate_size, dilate_size))
frame_morph = cv.dilate(erosion_dst, element)
dst = cv.Canny(frame_morph, 50, 200, None, 3)
cdstP = frame.copy()
cdst = frame.copy()
lines = cv.HoughLines(dst, 1, np.pi / 180, 60, None, 0, 0)
linesP = cv.HoughLinesP(dst, 0.75, np.pi / 180, 30, None, 10, 10)
if lines is not None:
for i in range(0, len(lines)):
rho = lines[i][0][0]
theta = lines[i][0][1]
a = math.cos(theta)
b = math.sin(theta)
x0 = a * rho
y0 = b * rho
pt1 = (int(x0 + 1000 * (-b)), int(y0 + 1000 * (a)))
pt2 = (int(x0 - 1000 * (-b)), int(y0 - 1000 * (a)))
cv.line(cdst, pt1, pt2, (0, 0, 255), 2, cv.LINE_AA)
if linesP is not None:
for i in range(0, len(linesP)):
l = linesP[i][0]
cv.line(cdstP, (l[0], l[1]), (l[2], l[3]), (0, 0, 255), 2, cv.LINE_AA)
cv.imshow(window_capture_name, frame)
cv.imshow(window_detection_name, frame_threshold)
cv.imshow(window_morph_name, frame_morph)
cv.imshow(window_hough_line, cdst)
cv.imshow("cdstP", cdstP)
cv.imshow("canny", dst)
cv.imshow("HSV", frame_HSV)
key = cv.waitKey(30)
if key == ord('q') or key == 27:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment