Skip to content

Instantly share code, notes, and snippets.

@ttaqinmu
Created March 16, 2017 14:29
Show Gist options
  • Save ttaqinmu/984c5b7e6610158850f1e41b6d636feb to your computer and use it in GitHub Desktop.
Save ttaqinmu/984c5b7e6610158850f1e41b6d636feb to your computer and use it in GitHub Desktop.
'''
program ini berasumsi bahwa pengendalian kapal (belok kanan,kiri dan lurus) diatur oleh program yg berbeda(mikrokontroler)
lalu program ini akan memberikan trigger melalui inputan serial
+ hanya menambahakan sensor ultrasonik (jarak)
'''
import cv2 #import OpenCV
import numpy as np #import Numpy
import serial
ser = serial.Serial( #inisialisasi serial port
port='\\\\.\\COM4',
baudrate=115200,
parity=serial.PARITY_ODD,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
ser.open()
cap = cv2.VideoCapture(0) #meng-capture video dari webcam/sensor kamera
while(1):
_, frame = cap.read() #membuat frame dari video yang telah di capture
height = frame.shape[0] #hitung tinggi gambar
width = frame.shape[1] #hitung lebar gambar
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) #mengganti frame dari BGR(Blue,Green,Red) menjadi HSV(Hue,Saturation,Value)
lower_red = np.array([0,50,50], dtype=np.uint8) #inisialisasi kode warna merah dalam bentuk BGR
upper_red = np.array([10,255,255], dtype=np.uint8) #inisialisasi kode warna merah dalam bentuk BGR
lower_green = np.array([50,100,100], dtype=np.uint8) #inisialisasi kode warna hijau dalam bentuk BGR
upper_green = np.array([70,255,255], dtype=np.uint8) #inisialisasi kode warna hijau dalam bentuk BGR
hijau = cv2.inRange(hsv, lower_green, upper_green) #filter frame HSV sesuai warna hijau
merah = cv2.inRange(hsv, lower_red, upper_red) #filter frame HSV sesuai warna merah
hasil_hijau = cv2.bitwise_and(frame,frame, mask = hijau) #membuat mask dari warna hijau
hasil_merah = cv2.bitwise_and(frame,frame, mask = merah) #membuat mask dari warna merah
hasil_dual = cv2.bitwise_or(hasil_hijau,hasil_merah) #membuat gambar baru hanya berwarna hijau dan merah
img = cv2.cvtColor(hasil_dual, cv2.COLOR_BGR2HSV) #convert ke HSV
count_red=0
count_green=0
for y in range (height): #counting perpixel
for x in range (width):
color = np.array(img[y,x],dtype=np.uint8)
if(color.all() >= lower_green.all()): #deteksi pixel berwarna hijau
if(color.all() <= upper_green.all()):
count_green =count_green +1
elif(color.all() >= lower_red.all()): #deteksi pixel berwarna merah
if(color.all() <= upper_red.all()):
count_red = count_red + 1
ser.write("R")
RightDistance = ser.readline()
ser.write("L")
LeftDistance = ser.readline()
ser.write("F")
FrontDistance = ser.readline()
if(count_red > count_green and FrontDistance <= 20 and RightDistance <=20 ): #pengendalian hasil akhir untuk membelokan perahu/boat
ser.write("1".encode()) #trigger untuk serial
elif(count_red < count_green and FrontDistance <= 20 and RightDistance <=20):
ser.write("2".encode()) #trigger untuk serial
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment