mirror of
https://github.com/zebrajr/opencv.git
synced 2026-01-15 12:15:17 +00:00
Tutorial Mask Operations on Matrices
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
import time
|
||||
|
||||
import numpy as np
|
||||
import cv2
|
||||
|
||||
|
||||
## [basic_method]
|
||||
def is_grayscale(my_image):
|
||||
return len(my_image.shape) < 3
|
||||
@@ -26,7 +27,6 @@ def sharpen(my_image):
|
||||
height, width, n_channels = my_image.shape
|
||||
|
||||
result = np.zeros(my_image.shape, my_image.dtype)
|
||||
|
||||
## [basic_method_loop]
|
||||
for j in range(1, height - 1):
|
||||
for i in range(1, width - 1):
|
||||
@@ -36,17 +36,16 @@ def sharpen(my_image):
|
||||
result[j, i] = saturated(sum_value)
|
||||
else:
|
||||
for k in range(0, n_channels):
|
||||
sum_value = 5 * my_image[j, i, k] - my_image[j + 1, i, k] - my_image[j - 1, i, k] \
|
||||
- my_image[j, i + 1, k] - my_image[j, i - 1, k]
|
||||
sum_value = 5 * my_image[j, i, k] - my_image[j + 1, i, k] \
|
||||
- my_image[j - 1, i, k] - my_image[j, i + 1, k]\
|
||||
- my_image[j, i - 1, k]
|
||||
result[j, i, k] = saturated(sum_value)
|
||||
## [basic_method_loop]
|
||||
|
||||
return result
|
||||
## [basic_method]
|
||||
|
||||
|
||||
def main(argv):
|
||||
filename = "../data/lena.jpg"
|
||||
filename = "../../../../data/lena.jpg"
|
||||
|
||||
img_codec = cv2.IMREAD_COLOR
|
||||
if argv:
|
||||
@@ -57,8 +56,9 @@ def main(argv):
|
||||
src = cv2.imread(filename, img_codec)
|
||||
|
||||
if src is None:
|
||||
print "Can't open image [" + filename + "]"
|
||||
print "Usage:\nmat_mask_operations.py [image_path -- default ../data/lena.jpg] [G -- grayscale]"
|
||||
print("Can't open image [" + filename + "]")
|
||||
print("Usage:")
|
||||
print("mat_mask_operations.py [image_path -- default ../../../../data/lena.jpg] [G -- grayscale]")
|
||||
return -1
|
||||
|
||||
cv2.namedWindow("Input", cv2.WINDOW_AUTOSIZE)
|
||||
@@ -70,7 +70,7 @@ def main(argv):
|
||||
dst0 = sharpen(src)
|
||||
|
||||
t = (time.time() - t) / 1000
|
||||
print "Hand written function time passed in seconds: %s" % t
|
||||
print("Hand written function time passed in seconds: %s" % t)
|
||||
|
||||
cv2.imshow("Output", dst0)
|
||||
cv2.waitKey()
|
||||
@@ -81,13 +81,13 @@ def main(argv):
|
||||
[-1, 5, -1],
|
||||
[0, -1, 0]], np.float32) # kernel should be floating point type
|
||||
## [kern]
|
||||
|
||||
## [filter2D]
|
||||
dst1 = cv2.filter2D(src, -1, kernel) # ddepth = -1, means destination image has depth same as input image
|
||||
dst1 = cv2.filter2D(src, -1, kernel)
|
||||
# ddepth = -1, means destination image has depth same as input image
|
||||
## [filter2D]
|
||||
|
||||
t = (time.time() - t) / 1000
|
||||
print "Built-in filter2D time passed in seconds: %s" % t
|
||||
print("Built-in filter2D time passed in seconds: %s" % t)
|
||||
|
||||
cv2.imshow("Output", dst1)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user