forked from NomotoK/COMP338_Labs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab2.py
76 lines (62 loc) · 2.22 KB
/
lab2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import numpy as np
import cv2
image = cv2.imread('/Users/hailin/Documents/COMP338_Labs/output.png')
cv2.imshow('image', image)
# cv2.waitKey(0)
(h, w, d) = image.shape
print("width={}, height={}, depth={}".format(w, h, d))
# print the pixel values of the image
print("image pixel values", image)
# access the RGB pixel located at x=50, y=100, keepind in mind that
# OpenCV stores images in BGR order rather than RGB
(B, G, R) = image[100, 50]
print("R={}, G={}, B={}".format(R, G, B))
# extract a 100x100 pixel square ROI (Region of Interest) from the
# input image starting at x=320,y=60 at ending at x=420,y=160
roi = image[60:160, 320:420]
cv2.imshow('image',roi)
# cv2.waitKey(0)
# resize the image to 200x200px, ignoring aspect ratio
resized = cv2.resize(image, (200, 200))
cv2.imshow('image',resized)
# cv2.waitKey(0)
# fixed resizing and distort aspect ratio so let's resize the width
# to be 300px but compute the new height based on the aspect ratio
r = 300.0 / w
dim = (300, int(h * r))
resized = cv2.resize(image, dim)
cv2.imshow('image',resized)
# cv2.waitKey(0)
# draw a 2px thick red rectangle surrounding the face
output = image.copy()
cv2.rectangle(output, (320, 60), (420, 160), (0, 0, 255), 2)
cv2.imshow('image',output)
# cv2.waitKey(0)
# draw a blue 20px (filled in) circle on the image centered at
# x=300,y=150
output = image.copy()
cv2.circle(output, (300, 150), 20, (255, 0, 0), -1)
cv2.imshow('image',output)
# cv2.waitKey(0)
# draw a 5px thick red line from x=60,y=20 to x=400,y=200
output = image.copy()
cv2.line(output, (60, 20), (400, 200), (0, 0, 255), 5)
cv2.imshow('image',output)
# cv2.waitKey(0)
# draw green text on the image
output = image.copy()
cv2.putText(output, "This is my text!", (10, 25),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
cv2.imshow('image',output)
# cv2.waitKey(0)
# convert the image to grayscale
output = image.copy()
gray = cv2.cvtColor(output, cv2.COLOR_BGR2GRAY)
cv2.imshow('image',gray)
# cv2.waitKey(0)
# threshold the image by setting all pixel values less than 225
# to 255 (white; foreground) and all pixel values >= 225 to 255
# (black; background), thereby segmenting the image
thresh = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)[1]
cv2.imshow('image',thresh)
cv2.waitKey(0)