Last active
January 4, 2017 17:04
-
-
Save vivekseth/497cd9c1328e4a2c398418c0ebb19d24 to your computer and use it in GitHub Desktop.
Gentle Intro to OpenCV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
def magnitude(image, pos): | |
x, y = pos | |
return np.mean(image[x, y]) | |
def isPeak(image, pos, threshold): | |
x, y = pos | |
mag = magnitude(image, pos) | |
for i in range(x-1, x+1): | |
for j in range(y-1, y+1): | |
if i == x and j == y: | |
continue | |
elif (magnitude(image, (i, j)) / mag) > (1 - threshold) : | |
return False | |
return True | |
def feature_detector(image, threshold): | |
features = [] | |
for i in range(1, image.shape[0]-1): | |
for j in range(1, image.shape[1]-1): | |
if isPeak(image, (i, j), threshold): | |
features.append((i, j)) | |
return features | |
image = cv2.imread('./small.tiff') | |
features = feature_detector(image, 0.1) | |
for (y, x) in features: | |
image = cv2.circle(image, (x, y), 2, (0, 0, 0), 0) | |
cv2.imshow('features', image) | |
k = cv2.waitKey(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
image = cv2.imread('./4.2.04.tiff') | |
cv2.imshow('Hello World', image) | |
k = cv2.waitKey(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
image = cv2.imread('./4.2.04.tiff') | |
cv2.imshow('Hello World', image) | |
#k = cv2.waitKey(0) | |
# Nothing happens! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
image = cv2.imread('./4.2.04.tiff') | |
# Why does this remove the Blue channel? | |
image[:, :, 0] = 0 | |
cv2.imshow('Hello World', image) | |
k = cv2.waitKey(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
image = cv2.imread('./4.2.04.tiff') | |
# Since OpenCV uses BGR, this code will correctly remove the red channel | |
image[:, :, 2] = 0 | |
cv2.imshow('Hello World', image) | |
k = cv2.waitKey(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
image = cv2.imread('./4.2.04.tiff') | |
new_image = np.zeros_like(image) | |
new_image[0:256, 0:256] = image[256:512, 0:256] | |
new_image[256:512, 0:256] = image[256:512, 256:512] | |
new_image[256:512, 256:512] = image[0:256, 256:512] | |
new_image[0:256, 256:512] = image[0:256, 0:256] | |
cv2.imshow('Hello World', new_image) | |
k = cv2.waitKey(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
image = cv2.imread('./4.2.04.tiff') | |
# OpenCV automatically converts to the correct output format | |
cv2.imwrite('out.png', image) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
image = np.zeros([400, 400, 3]) | |
image = cv2.circle(image, (0, 0), 50, (255, 0, 0), 1) | |
image = cv2.circle(image, (200, 200), 50, (0, 255, 0), 1) | |
image = cv2.circle(image, (400, 400), 50, (0, 0, 255), 1) | |
cv2.imshow('Hello World', image) | |
k = cv2.waitKey(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
image = np.zeros([400, 400, 3]) | |
for i in range(20): | |
pos = tuple(np.random.random_integers(400, size=2)) | |
radius = np.random.random_integers(5, 50) | |
# If using a numpy array, OpenCV expects values from 0 to 1.0 | |
color = np.random.uniform(size=3) | |
image = cv2.circle(image, pos, radius, color, 1) | |
cv2.imshow('Hello World', image) | |
k = cv2.waitKey(0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
image = np.zeros([400, 400, 3]) | |
for i in range(20): | |
pos1 = tuple(np.random.random_integers(400, size=2)) | |
pos2 = tuple(np.random.random_integers(400, size=2)) | |
# If using a numpy array, OpenCV expects values from 0 to 1.0 | |
color = np.random.uniform(size=3) | |
image = cv2.line(image, pos1, pos2, color, 1) | |
cv2.imshow('Hello World', image) | |
k = cv2.waitKey(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment