RYAN'S WEBSITE

Just something small and tiny.

Held by Ryan Ruoshui Yan.


Here is the world of a nerdy guy.

Visited:

OpenCV Notes

import cv2
import numpy as np

# Show image
def imshow(img):
    cv2.imshow("windows name", img)
    cv2.waitKey(0)
    # Need to add .waitKey() for the image to stay on the screen

# Read an image using .imread()
img = cv2.imread('pie.png')

# Show image
imshow(img)

# Get subarea
imshow(img[100:200, 100:200, :])

# Change subarea to black
img[100:150, 50:100, :] = np.zeros([50, 50, 3])
imshow(img)

# Change subarea to white
img[100:150, 50:100, :] = 255
imshow(img)

# Change subarea to red
img[100:150, 50:100, 0] = 0
    # 0 before "]" stands for blue channel
img[100:150, 50:100, 1] = 0
    # 1 before "]" stands for green channel
    # 2 stands for red channel
# Note: CV2 reads images in BGR order rather that RGB.
imshow(img)

# Get dimension
# This is a colored image, so it has 3 channels and each channel is a 227*227 matrix. Another name for 3-dimensional array is TENSOR.
print(img.shape)

# Get type.
# Images in CV2 are usually uint8 and if you have float values some functions behaviours would be different. The assumption is either have float between 0-1 or int between 0-255! Keep track of it ALWAYS.
print(type(img)) # numpy.ndarray
print(img.dtype) # uint8, which means "unsigned integer 8 bits", so each value must between 0 and 255. (Default dtype in CV2)

# Read an image in greyscale
img = cv2.imread("pie.png", cv2.CV_LOAD_IMAGE_GRAYSCALE)
print(img.dtype) # uint8
print(img.shape) # 2 dimensional since color dimension is removed
imshow(img)

# Flip the image upside-down
# .imread() reads an image into an array, so we can do array operation on the image
img_flipped = img[::-1, :]
imshow(img_flipped)

# Brighten the image (Worse)
img_brighter = img + 100
    # Note: It will overflow since the max possible intensity for a pixel is 255. And if it overflows, the value will change back to 0, and will start from 0.
imshow(img_brighter)

# Brighten the image (Better)
img_brighter_2 = img / 2 + 128
# or:
# img_brighter_2 = img + (255 - img) / 2
imshow(img_brighter_2)

# Darken the image by changing type
img_darker = img.astype(np.uint16)
imshow(img_darker)

# Create a black image
img_circle = np.zeros([300, 300], dtype=np.uint8)
    # Size 300*300
    # There is no color channel here (why?).
    # So it is a grayscale image.
imshow(img_circle)

# Copy an image
img_rectangle = img.copy()

# Draw a rectangle on the image
img = cv2.imread("pie.png")
img_rectangle = img.copy()
cv2.rectangle(
    img_rectangle,
    (50, 50),   # Top left corner
    (100, 200), # Bottom right corner
    (255, 0, 0),# Color & Brightness
    5           # Thickness of sides
)
imshow(img_rectangle)

# Draw a line on the image
img_line = img.copy()
cv2.line(
    img_line,
    (50, 50),   # Start point
    (100, 200), # End point
    (255, 0, 0),# Color
    5           # Thickness
)
imshow(img_line)

# Draw a circle
img_circle = np.zeros([300, 300], dtype=np.uint8)
# (150, 120) is the center of the circle, 50 is the radius, 150 is the brightness, and -1 stands for making the circle a solid one
cv2.circle(
    img_circle,
    (150, 120), # Center
    50,         # Radius
    150,        # Brightness
    -1          # Thickness, -1: a solid circle
)
cv2.circle(img_circle, (150, 120), 100, 255, 10)
imshow(img_circle)

# Save the image file on the computer
cv2.imwrite("circle.jpg", img_circle)
RECENT

Angular

Introduction A JavaScript framework Extends HTML with ng-directive ng-app directive: Defines an AngularJS application ng-model directive: Binds value to application data ng-bind directive: Binds application data to HTML view ...…

MORE
EARLIER

NumPy Notes

Basic# Importimport numpy as np# Create an arraymatrix = np.array([1, 2, 3, 4])# Important: Not "matrix = np.array(1, 2, 3, 4)"# 2D array:matrix2 = np.array([[1,2], [3,4]])# Note: "matrix2[1][0]" or "matrix2[1, 0]" gives 3, not 2. Vertical first, ...…

MORE