1 from __future__ import division
5 # Snippet code for Operations with images tutorial (not intended to be run)
10 ## [Load an image from a file]
11 img = cv.imread(filename)
12 ## [Load an image from a file]
14 ## [Load an image from a file in grayscale]
15 img = cv.imread(filename, cv.IMREAD_GRAYSCALE)
16 ## [Load an image from a file in grayscale]
19 cv.imwrite(filename, img)
23 # Accessing pixel intensity values
24 img = np.empty((4,4,3), np.uint8)
41 def reference_counting():
42 # Memory management and reference counting
43 ## [Reference counting 2]
44 img = cv.imread('image.jpg')
46 ## [Reference counting 2]
48 ## [Reference counting 3]
49 img = cv.imread('image.jpg')
50 sobelx = cv.Sobel(img, cv.CV_32F, 1, 0);
51 ## [Reference counting 3]
53 def primitive_operations():
54 img = np.empty((4,4,3), np.uint8)
55 ## [Set image to black]
57 ## [Set image to black]
60 smallImg = img[10:110,10:110]
64 img = cv.imread('image.jpg')
65 grey = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
68 src = np.ones((4,4), np.uint8)
69 ## [Convert to CV_32F]
70 dst = src.astype(np.float32)
71 ## [Convert to CV_32F]
73 def visualize_images():
75 img = cv.imread('image.jpg')
76 cv.namedWindow('image', cv.WINDOW_AUTOSIZE)
77 cv.imshow('image', img)
82 img = cv.imread('image.jpg')
83 grey = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
84 sobelx = cv.Sobel(grey, cv.CV_32F, 1, 0)
85 # find minimum and maximum intensities
86 minVal = np.amin(sobelx)
87 maxVal = np.amax(sobelx)
88 draw = cv.convertScaleAbs(sobelx, alpha=255.0/(maxVal - minVal), beta=-minVal * 255.0/(maxVal - minVal))
89 cv.namedWindow('image', cv.WINDOW_AUTOSIZE)
90 cv.imshow('image', draw)