Skip to content

Instantly share code, notes, and snippets.

@1UC1F3R616
Last active February 9, 2020 13:17
Show Gist options
  • Save 1UC1F3R616/227496dd9b8723c8769cb9bf76062cfb to your computer and use it in GitHub Desktop.
Save 1UC1F3R616/227496dd9b8723c8769cb9bf76062cfb to your computer and use it in GitHub Desktop.

Read Write and Destory

import cv2
import numpy as np

def debug(x):
    print('\n\n{}\n\n'.format(str(x)))

image = 'images/image2.jpg'

img = cv2.imread(image, 0) # Reading an image

# To show image
cv2.imshow('image', img)
cv2.waitKey(1000)

# Destroying it's window
cv2.destroyAllWindows()

# Writing an Image
cv2.imwrite('images/bimage2.png', img)

Using laptop Cam

import cv2

cap = cv2.VideoCapture(0);

while True:
    ret, frame = cap.read() # ret is boolean, frame gets the frame

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.imshow('frame', gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Drawing Geometry on a picture

import cv2

def debug(x):
    print('\n\n{}\n\n'.format(str(x)))

image = 'images/image2.jpg'

img = cv2.imread(image, 1)
img = cv2.line(img, (0, 0), (255, 255), (0, 255, 0), 5) # BGR

cv2.imshow('image', img)

cv2.waitKey(0)
cv2.destroyAllWindows()

Setting Camera Parameters in OpenCV Python

import cv2

cap = cv2.VideoCapture(0) # Use -1, 1, 2 ... for using other connected devices

# Getting values
print(cap.get(3)) # 3 is for cv2.CAP_PROP_FRAME_WIDTH, which gives you the frame size. Check docs for others

# Setting values
cap.set(3, 1280)

# Displaying the video
while(cap.isOpened()):
    ret, frame = cap.read() # ret gets value True or False, frame gets the Frame
    if ret == True:
        gray = cv2.cvtVolor(frame, cv2.COLOR_BGR2GRAY) # Converting color from BGR to Gray
        cv2.imshow('frame', gray)
        
        if cv2.waitKey(1) & 0xFF == ord('q'): # quit when q is presed
            break
    else:
        break

cap.release() # Releaseing the resources
cv2.destroyAllWindows() # Destroy any window if any

Show DateTime on Videos

import cv2
import datetime
cap = cv2.VideoCapture(0)

while cap.isOpened():
    ret, frame = cap.read()
    
    if ret == True:
        font = cv2.FONT_HERSHEY_SIMPLEX
        myText = str(datetime.datetime.now())
        frame = cv2.putText(frame, myText, (10, 50), font, 1, (0, 255, 255), 2, cv2.LINE_AA)
        cv2.imshow('frame', frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

cap.release()
cv2.destroyAllWindows()

Handling Mouse Events (coordinate and color channel)

import numpy as np
import cv2

# Finding all present Events
events = [event for event in dir(cv2) if 'EVENT' in event]
for event in events:
    print(event)

def click_event(event, x, y, flags, param): # cv will pass all parameters
    if event == cv2.EVENT_LBUTTONDOWN: # left button down event
        font = cv2.FONT_HERSHEY_SIMPLEX
        strXY = "{}, {}".format(str(x), str(y))
        cv2.putText(img, strXY, (x, y), font, 1, (255, 0, 0), 1)
        cv2.imshow('image', img) # 'image' is our frame name
    
    if event == cv2.EVENT_RBUTTONDOWN: # right button down event
        blue = img[y, x, 0] # b channel value at index x, y in img
        green = img[y, x, 1]
        red = img[y, x, 2]
        font = cv2.FONT_HERSHEY_SIMPLEX
        strBGR = "{}, {}, {}".format(str(blue), str(green), str(red))
        cv2.putText(img, strBGR, (x, y), font, .5, (0, 255, 255), 2)
        cv2.imshow('image', img)

img = np.zeros((512, 512, 3), np.uint8)
cv2.imshow('image', img)

cv2.setMouseCallback('image', click_event)

cv2.waitKey(0) # exit on escape
cv2.destroyAllWindows()

Handling Mouse Events (Joining Coordinates by Clicking)

import numpy as np
import cv2

def click_event(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        cv2.circle(img, (x, y), 3, (0, 0, 255), -1)
        points.append((x, y))
        if len(points)>=2:
            cv2.line(img, points[-1], points[-2], (255, 0, 0), 5)
        cv2.imshow('image', img)


img = np.zeros((512, 512, 3), np.uint8)
cv2.imshow('image', img) # image is frame name

points = []

cv2.setMouseCallback('image', click_event) # image is frame name

cv2.waitKey(0)
cv2.destroyAllWindows()

Handling Mouse Events (Get Color from Image using a second window)

import numpy as np
import cv2

def click_event(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        blue = img[x, y, 0]
        green = img[x, y, 1]
        red = img[x, y, 2]
        cv2.circle(img, (x, y), 3, (0, 255, 200), -1)
        myColorImage = np.zeros((512, 512, 3), np.uint8)

        myColorImage[:] = [blue, green, red]

        cv2.imshow('color', myColorImage)


img = np.zeros((512, 512, 3), np.uint8)

cv2.imshow('image', img)

cv2.setMouseCallback('image', click_event)

cv2.waitKey(0)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment