Numpy scipy matplotlib opencv nstall objects python m
Numpy, scipy, matplotlib, opencv
İnstall objects python -m pip install matplotlib. python -m pip install opencv-python -m pip install numpy.
Creating Array: Basics >>> from numpy import * >>> a = array([1. 1, 2. 2, 3. 3]) >>> print a [ 1. 1 2. 2 3. 3] # two-dimension array >>> b = array(([1, 2, 3], [4, 5, 6])) >>> print b [[1 2 3] [4 5 6]] >>> b. shape (2, 3) >>> print ones((2, 3), float) [[1. 1. 1. ]] >>> print resize(b, (2, 6)) [[1 2 3 4 5 6]] >>> print reshape(b, (3, 2)) [[1 2] [3 4] [5 6]]
Creating Arrays: Strategies # user reshape with range >>> a = reshape(range(12), (2, 6)) >>> print a [[0 1 2 3 4 5] [6 7 8 9 10 11]] # set an entire row (or column) >>> a[0, : ] = range(1, 12, 2) >>> print a [[1 3 5 7 9 11] [6 7 8 9 10 11]] >>> a = zeros([50, 100]) # loop to set individual values >>> for i in range(50): … for j in range(100): … a[i, j] = i + j # call user function set(x, y) >>> shape = (50, 100) >>> a = fromfunction(set, shape) # use scipy. io module to read # values from a file into an # array
Simple Array Operations >>> a = arange(1, 4); print a [1 2 3] # addition (element wise) >>> print 3 + a [4 5 6] # multiplication (element wise) >>> print 3*a [3 6 9] # it really is element wise >>> print a*a [1 4 9] # power: a**b -> power(a, b) >>> print a**a [1 4 27] # functions: sin(x), log(x), … >>> print sqrt(a*a) [1. 2. 3. ] # comparison: ==, >, and, … >>> print a < a [False] # reductions >>> add. reduce(a) 6
Slicing Arrays >>> [[0 [3 [6 a = reshape(range(9), (3, 3)) print a 1 2] 4 5] 7 8]] # second column >>> print a[: , 1] [1 4 7] # last row >>> print a[-1, : ] [6 7 8] # slices are references to # original memory, true for # all array/sequence assignment # work on the first row of a >>> b = a[0, : ] >>> b[0] = 99 ; print b [99 1 2] # what is a[0, : ] now? >>> print a[0, : ] [99 1 2]
Array Temporaries and ufuncs >>> a = arange(10) >>> b = arange(10, 20) # What will the following do? >>> a = a + b # Is the following different? >>> c = a + b >>> a = c # Does “a” reference old or new # memory? Answer, new memory! # Watch out for array # temporaries with large arrays! # Universal functions, ufuncs >>> type(add) <type 'numpy. ufunc'> # add is a binary operator >>> a = add(a, b) # in place operation >>> add(a, b, a)
Array Functions >>> a = arange(1, 11); print a [1 2 3 4 5 6 7 8 9 10] # create an index array >>> ind = [0, 5, 8] # take values from the array >>> print take(a, ind) >>> print a[ind] [1 6 9] # put values to the array >>> put(a, ind, [0, 0, 0]); print a >>> a[ind] = (0, 0, 0); print a [0 2 3 4 5 0 7 8 0 10] >>> a = reshape(range(9), (3, 3)) >>> b = transpose(a); print b [[0 3 6] [1 4 7] [2 5 8]] >>> print diagonal(b) [0 4 8] >>> print trace(b) 12 >>> print where(b >= 3, 9, 0) [[0 9 9]]
Numpy objects >>> A= np. array([range(3) for x in range(4)], np. float 64) >>> np. matrix([range(3) for x in range(4)], np. float 64) matrix([[ 0. , 1. , 2. ], [ 0. , 1. , 2. ]]) >>> M= np. matrix([range(3) for x in range(4)], np. float 64) >>> type(A), type(M) (<type 'numpy. ndarray'>, <class 'numpy. core. defmatrix'>) >>> np. matrix(range(3), np. float 64) matrix([[ 0. , 1. , 2. ]]) >>> np. array(range(3), np. float 64) array([ 0. , 1. , 2. ]) >>> M=np. matrix(range(3), np. float 64) >>> A=np. array(range(3), np. float 64) >>> A. shape (3, ) >>> M. shape (1, 3)
Numpy objects >>> M=np. matrix(range(3), np. float 64) >>> A=np. array(range(3), np. float 64) >>> A. T array([ 0. , 1. , 2. ]) >>> M. T matrix([[ 0. ], [ 1. ], [ 2. ]]) >>> A=np. array([[1, 2], [2, -2]], np. float 64) >>> M=np. matrix([[1, 2], [2, -2]], np. float 64) >>> M. T matrix([[ 1. , 2. ], [ 2. , -2. ]]) >>> A. T array([[ 1. , 2. ], [ 2. , -2. ]]) >>> A. I Traceback (most recent call last): File "<stdin>", line 1, in <module> Attribute. Error: 'numpy. ndarray' object has no attribute 'I' >>> M. I matrix([[ 0. 3333, 0. 3333], [ 0. 3333, -0. 16666667]])
Numpy objects >>> M matrix([[ 1. , 2. ], [ 2. , -2. ]]) >>> M. I matrix([[ 0. 3333, 0. 3333], [ 0. 3333, -0. 16666667]]) >>> np. linalg. inv(A) array([[ 0. 3333, 0. 3333], [ 0. 3333, -0. 16666667]]) >>> M 2=np. matrix([[1, 1], [0, 0]], np. float 64) >>> A 2=np. array([[1, 1], [0, 0]], np. float 64) >>> M*M 2 matrix([[ 1. , 1. ], [ 2. , 2. ]]) >>> A*A 2 array([[ 1. , 2. ], [ 0. , -0. ]]) >>> A 2*A array([[ 1. , 2. ], [ 0. , -0. ]]) >>> M 2*M matrix([[ 3. , 0. ], [ 0. , 0. ]])
Numpy objects >>> A == M matrix([[ True, True], [ True, True]], dtype=bool) >>> Ai=np. linalg. inv(A) >>> np. dot(A, Ai) array([[ 1. 0000 e+00, 5. 55111512 e-17], [ 0. 0000 e+00, 1. 0000 e+00]]) >>> np. dot(Ai, A) array([[ 1. 0000 e+00, -1. 11022302 e-16], [ 0. 0000 e+00, 1. 0000 e+00]]) >>> M*M. I matrix([[ 1. 0000 e+00, 5. 55111512 e-17], [ 0. 0000 e+00, 1. 0000 e+00]]) >>> M. I*M matrix([[ 1. 0000 e+00, -1. 11022302 e-16], [ 0. 0000 e+00, 1. 0000 e+00]])
Numpy objects >>> np. random. uniform() 0. 70420105296872415 >>> np. random. uniform(-0. 1, 0. 1) -0. 059314492060574403 >>> np. random. uniform(-0. 1, 4) array([ 0. 06606591, 0. 07766084, 0. 08635536, 0. 08010191]) >>> np. random. uniform(-0. 1, (2, 2)) array([[-0. 06448909, 0. 07906606], [-0. 04752628, -0. 02955906]]) >>> np. random. normal(-1, 1, (3, 2)) array([[-0. 32922971, -0. 05700329], [-2. 81944081, 0. 43708656], [-1. 4274894 , -0. 61651697]]) >>> np. sort([1, 5. 9, 2, -1]) array([-1. , 2. , 5. 9]) >>> np. argsort([1, 5. 9, 2, -1]) array([3, 0, 2, 1]) >>>
Numpy objects >>>np. ones((4, 4)) array([[ 1. , 1. ], [ 1. , 1. ]]) >>> np. zeros((4, 4)) array([[ 0. , 0. ], [ 0. , 0. ]]) >>> A=np. zeros((4, 4)) >>> for i in range(len(A)): . . . A[i][: ]=np. random. uniform(-10, len(A)). . . >>> A array([[-2. 74761648, -3. 81873152, 8. 5612057 , -6. 50432488], [ 4. 04987279, -9. 37231031, -7. 96381121, 0. 4987925 ], [-5. 86088041, 7. 90005728, 7. 30370647, 8. 57564479], [ 9. 09765827, 2. 85531062, 9. 41611209, -8. 07463238]]) >>> for i in range(len(A)): . . . A[i, : ]=np. random. uniform(-10, len(A))
Numpy objects >>> B array([[ 0. , 1. ], [ 1. , 0. ]]) >>> evals, evecs=np. linalg. eig(B) >>> evals array([ 1. , -1. ]) >>> for i in range(len(B)): . . . print evals[i], evecs[i]. . . 1. 0 [ 0. 70710678 -0. 70710678] -1. 0 [ 0. 70710678] >>>
Numpy objects >>> A+A. T array([[ -9. 87072296, -8. 6819582 , 1. 05438839, 0. 77621665], [ -8. 6819582 , -6. 20131579, 12. 1133606 , -4. 29184743], [ 1. 05438839, 12. 1133606 , 13. 24488407, -7. 21060701], [ 0. 77621665, -4. 29184743, -7. 21060701, 19. 57503177]]) >>> A=A+A. T >>> evals, evecs=np. linalg. eig(A) >>> evals array([-19. 38912856, -2. 79317213, 27. 87492659, 11. 05525118])
Numpy objects >>> inputs=np. array([[ 1. ], [ 2. ], [ 3. ], [ 4. ], [ 5. ]]) >>> targets=np. array([[ 3. ], [ 5. ], [ 7. ], [ 9. ], [ 11. ]]) >>> np. concatenate((inputs, targets)) array([[ 1. ], [ 2. ], [ 3. ], [ 4. ], [ 5. ], [ 3. ], [ 5. ], [ 7. ], [ 9. ], [ 11. ]]) >>> np. concatenate((inputs, targets), axis=1) array([[ 1. , 3. ], [ 2. , 5. ], [ 3. , 7. ], [ 4. , 9. ], [ 5. , 11. ]]) >>> it=np. concatenate((inputs, targets), axis=1)
Numpy objects >>> print np. sum(it), it. size 50. 0 10 >>> print np. mean(it), np. sum(it)/it. size 5. 0 >>> np. sum(it, axis=0), np. sum(it, axis=1) (array([ 15. , 35. ]), array([ 4. , 7. , 10. , 13. , 16. ])) >>> len(it), len(it[0]) (5, 2) >>> np. mean(it, axis=0), np. mean(it, axis=1) (array([ 3. , 7. ]), array([ 2. , 3. 5, 5. , 6. 5, 8. ])) >>>
Numpy objects >>> D=np. zeros((20, 3)) >>> for i in range(len(D)): . . . D[i, : ]=np. random. uniform(-10, len(D[i])) >>> D. mean(axis=0) array([-1. 02909335, 2. 77430392, -1. 00094332]) >>> np. std(D) 5. 8341355973553766 >>> np. std(D, axis=0) array([ 6. 37529467, 5. 64920856, 4. 46994818]) >>> np. var(D, axis=0) array([ 40. 64438211, 31. 91355732, 19. 98043675]) >>> np. sqrt(np. var(D, axis=0)) array([ 6. 37529467, 5. 64920856, 4. 46994818]) >>>
Numpy objects >>> r=0. 1 >>> a=np. array(range(4), np. float)+np. random. uniform(-r, r) >>> b=np. array(range(4), np. float)+np. random. uniform(-r, r) -5 >>> c=np. array(range(4, 0, -1), np. float)+np. random. uniform(-r, r) -5 >>> d=np. matrix([np. random. uniform(-r, r) for i in range(4)], np. float) >>> np. cov(a, a) array([[ 1. 66666667, 1. 66666667], [ 1. 66666667, 1. 66666667]]) >>> np. cov(a, b) array([[ 1. 66666667, 1. 66666667], [ 1. 66666667, 1. 66666667]]) >>> np. cov(a, c) array([[ 1. 66666667, -1. 66666667], [-1. 66666667, 1. 66666667]]) >>> np. cov(a, d) array([[ 1. 66666667, -0. 01641726], [-0. 01641726, 0. 0033877 ]])
Matplotlib with numpy • The matplotlib package is compatible with numpy arrays. # create data using numpy t = arange(0. 0, 2. 0, 0. 01) s = sin(2*pi*t) # create the plot(t, s, linewidth=1. 0) # decorate the plot xlabel('time (s)') ylabel('voltage (m. V)') title('About as simple as it gets, folks') grid(True) show() 21
Simple Plot 22
Improving the axis settings # get axis settings >>> axis() (0. 0, 2. 0, -1. 0, 1. 0) # changes should show up immediately >>> axis([0. 0, 2. 0, -1. 5, 1. 5]) # a plot can be saved from the menu bar 23
Better axes 24
Colorful background subplot(111, axisbg=‘darkslategray’) t = arange(0. 0, 2. 0, 0. 01) # first plot(t, sin(2*pi*t), ‘y’) # second plot t = arange(0. 0, 2. 0, 0. 05) plot(t, sin(pi*t), ‘ro’) 25
Colorful background 26
Fill demo # data t = arange(0. 0, 1. 01, 0. 01) s = sin(2*2*np. pi*t) # graph fill(t, s*np. exp(-5*t), 'r') grid(True) 27
Fill demo 28
Subplot demo def f(t): s 1 = cos(2*pi*t); e 1 = exp(-t) return multiply(s 1, e 1) t 1 = arange(0. 0, 5. 0, 0. 1) t 2 = arange(0. 0, 5. 0, 0. 02) t 3 = arange(0. 0, 2. 0, 0. 01) subplot(211) plot(t 1, f(t 1), 'bo', t 2, f(t 2), 'k--', markerfacecolor='green') grid(True) title('A tale of 2 subplots') ylabel('Damped oscillation') subplot(212) plot(t 3, cos(2*pi*t 3), 'r. ') grid(True) xlabel('time (s)') ylabel('Undamped’) 29
Subplot demo 30
A basic 3 D plot example • Matplotlib can do polar plots, contours, …, and can even plot mathematical symbols using�La. Te. X • 3 D graphics? – not so great • Matplotlib has simple 3 D graphics but is limited relative to packages based on Open. GL like VTK. • Note: mplot 3 d module may not be loaded on your system. 31
3 D example from mpl_toolkits. mplot 3 d import Axes 3 D from matplotlib import cm import random fig = figure() ax = Axes 3 D(fig) X = arange(-5, 5, 0. 25) Y = arange(-5, 5, 0. 25) X, Y = meshgrid(X, Y) R = sqrt(X**2 + Y**2) Z = sin(R) ax. plot_surface(X, Y, Z, rstride=1, cmap=cm. jet) 32
3 D example 33
im = RGB im = 0 to • • Images in Python cv 2. imread(filename) # read image cv 2. cvt. Color(im, cv 2. COLOR_BGR 2 RGB) # order channels as im / 255 1 # values range from RGB image im is a H x W x 3 matrix (numpy. ndarray) im[0, 0, 0] = top-left pixel value in R-channel im[y, x, c] = y+1 pixels down, x+1 pixels to right in the cth channel im[H-1, W-1, 2] = bottom-right pixel in B-channel row column 0. 92 0. 95 0. 89 0. 96 0. 71 0. 49 0. 86 0. 96 0. 69 0. 79 0. 91 0. 93 0. 89 0. 72 0. 95 0. 81 0. 62 0. 84 0. 67 0. 49 0. 73 0. 94 0. 82 0. 51 0. 92 0. 88 0. 95 0. 81 0. 89 0. 60 0. 96 0. 74 0. 71 0. 54 0. 49 0. 56 0. 86 0. 90 0. 96 0. 89 0. 69 0. 79 0. 91 0. 97 0. 89 0. 55 0. 93 0. 94 0. 89 0. 87 0. 72 0. 58 0. 95 0. 58 0. 81 0. 85 0. 62 0. 66 0. 84 0. 67 0. 49 0. 73 0. 94 0. 62 0. 56 0. 51 0. 94 0. 56 0. 82 0. 57 0. 51 0. 92 0. 50 0. 88 0. 95 0. 51 0. 89 0. 48 0. 60 0. 96 0. 43 0. 74 0. 71 0. 33 0. 54 0. 49 0. 41 0. 56 0. 86 0. 90 0. 96 0. 89 0. 69 0. 79 0. 91 0. 37 0. 31 0. 42 0. 97 0. 46 0. 89 0. 37 0. 55 0. 93 0. 60 0. 94 0. 89 0. 39 0. 87 0. 72 0. 37 0. 58 0. 95 0. 42 0. 58 0. 81 0. 61 0. 85 0. 62 0. 78 0. 66 0. 84 0. 67 0. 49 0. 73 0. 94 0. 85 0. 75 0. 57 0. 62 0. 91 0. 56 0. 80 0. 51 0. 94 0. 58 0. 56 0. 82 0. 73 0. 57 0. 51 0. 88 0. 50 0. 88 0. 77 0. 51 0. 81 0. 69 0. 48 0. 60 0. 78 0. 43 0. 74 0. 33 0. 54 0. 41 0. 56 0. 90 0. 89 0. 97 0. 92 0. 41 0. 37 0. 87 0. 31 0. 88 0. 42 0. 97 0. 50 0. 46 0. 89 0. 92 0. 37 0. 55 0. 90 0. 60 0. 94 0. 73 0. 39 0. 87 0. 79 0. 37 0. 58 0. 77 0. 42 0. 58 0. 61 0. 85 0. 78 0. 66 0. 67 0. 49 0. 93 0. 81 0. 49 0. 85 0. 90 0. 75 0. 89 0. 57 0. 62 0. 61 0. 91 0. 56 0. 91 0. 80 0. 51 0. 94 0. 58 0. 56 0. 71 0. 73 0. 57 0. 73 0. 88 0. 50 0. 89 0. 77 0. 51 0. 69 0. 48 0. 78 0. 43 0. 33 0. 41 0. 92 0. 95 0. 91 0. 97 0. 92 0. 79 0. 41 0. 37 0. 45 0. 87 0. 31 0. 49 0. 88 0. 42 0. 82 0. 50 0. 46 0. 90 0. 92 0. 37 0. 93 0. 90 0. 60 0. 99 0. 73 0. 39 0. 79 0. 37 0. 77 0. 42 0. 61 0. 78 0. 99 0. 91 0. 92 0. 93 0. 95 0. 81 0. 85 0. 49 0. 85 0. 33 0. 90 0. 75 0. 74 0. 89 0. 57 0. 93 0. 61 0. 99 0. 91 0. 80 0. 97 0. 94 0. 58 0. 93 0. 71 0. 73 0. 88 0. 89 0. 77 0. 69 0. 78 R 0. 92 0. 95 0. 91 0. 97 0. 92 0. 79 0. 41 0. 45 0. 87 0. 49 0. 88 0. 82 0. 50 0. 92 0. 93 0. 90 0. 99 0. 73 0. 79 0. 77 0. 99 0. 91 0. 92 0. 93 0. 95 0. 81 0. 85 0. 49 0. 33 0. 90 0. 74 0. 89 0. 93 0. 61 0. 99 0. 91 0. 97 0. 94 0. 93 0. 71 0. 73 0. 89 G B 0. 92 0. 95 0. 91 0. 97 0. 79 0. 45 0. 49 0. 82 0. 90 0. 93 0. 99 0. 91 0. 92 0. 95 0. 85 0. 33 0. 74 0. 93 0. 99 0. 97 0. 93
Image filtering • Image filtering: compute function of local neighborhood at each position • Really important! – Enhance images • Denoise, resize, increase contrast, etc. – Extract information from images • Texture, edges, distinctive points, etc. – Detect patterns • Template matching
Example: box filter 1 1 1 1 1 Slide credit: David Lowe (UBC)
Image filtering 0 0 0 0 0 0 90 90 90 0 0 90 90 90 0 0 0 0 90 90 90 0 0 90 90 90 0 0 0 0 0 0 0 90 90 0 0 0 0 0 0 0 0 1 1 1 1 1 0 Credit: S. Seitz
Image filtering 0 0 0 0 0 0 90 90 90 0 0 90 90 90 0 90 90 90 0 0 0 0 0 0 0 0 0 1 1 10 Credit: S. Seitz
Image filtering 0 0 0 0 0 0 90 90 90 0 0 90 90 90 0 90 90 90 0 0 0 0 0 0 0 0 0 10 1 1 1 1 1 20 Credit: S. Seitz
Image filtering 0 0 0 0 0 0 90 90 90 0 0 90 90 90 0 90 90 90 0 0 0 0 0 0 0 0 0 10 20 1 1 1 1 1 30 Credit: S. Seitz
Image filtering 0 0 0 0 0 0 90 90 90 0 0 90 90 90 0 90 90 90 0 0 0 0 0 0 0 0 0 10 20 30 1 1 1 1 1 30 Credit: S. Seitz 41
Image filtering 0 0 0 0 0 0 90 90 90 0 0 90 90 90 0 90 90 90 0 0 0 0 0 0 0 0 0 10 20 30 1 1 1 1 1 30 ? Credit: S. Seitz 42
Image filtering 0 0 0 0 0 0 90 90 90 0 0 90 90 90 0 90 90 90 0 0 0 0 0 0 0 0 0 10 20 30 1 1 1 1 1 30 ? 50 Credit: S. Seitz 43
Image filtering 0 0 0 0 0 0 10 20 30 30 30 20 10 0 90 90 90 0 20 40 60 60 60 40 20 0 90 90 90 0 30 60 90 90 90 60 30 0 90 90 90 0 30 50 80 80 90 60 30 0 90 90 90 0 20 30 50 50 60 40 20 0 0 10 20 30 30 20 10 0 0 90 0 0 0 10 10 10 0 0 0 1 1 1 1 1 Credit: S. Seitz 44
Image filtering 0 0 0 0 0 0 10 20 30 30 30 20 10 0 90 90 90 0 20 40 60 60 60 40 20 Informally, what does the filter do? 0 0 0 90 90 90 0 30 60 90 90 90 60 30 0 0 90 90 90 0 30 50 80 80 90 60 30 0 90 90 90 0 20 30 50 50 60 40 20 0 0 10 20 30 30 20 10 0 0 90 0 0 0 10 10 10 0 0 0 0 1 1 1 1 1 Credit: S. Seitz 45
Box Filter What does it do? • Replaces each pixel with an average of its neighborhood • Achieve smoothing effect (remove sharp features) 1 1 1 1 1 Slide credit: David Lowe (UBC)
Example: Tracking cap = cv. Video. Capture ('slow. flv' ) # take first frame of the video ret, frame = cap. read() # setup initial location of window r, h, c, w = 250, 90, 400, 125 # simply hardcoded the values track_window = (c, r, w, h) # set up the ROI for tracking roi = frame[r: r+h, c: c+w] hsv_roi = cv. cvt. Color(roi, cv. COLOR_BGR 2 HSV) mask = cv. in. Range(hsv_roi, np. array((0. , 60. , 32. )), np. array((180. , 255. ))) roi_hist = cv. calc. Hist([hsv_roi], [0], mask, [180], [0, 180]) cv. normalize(roi_hist, 0, 255, cv. NORM_MINMAX) # Setup the termination criteria, either 10 iteration or move by atleast 1 pt term_crit = ( cv. TERM_CRITERIA_EPS | cv. TERM_CRITERIA_COUNT, 10, 1 ) while(1): ret , frame = cap. read() if ret == True: hsv = cv. cvt. Color(frame, cv. COLOR_BGR 2 HSV) dst = cv. calc. Back. Project([hsv], [0], roi_hist, [0, 180], 1) # apply meanshift to get the new location ret, track_window = cv. mean. Shift(dst, track_window, term_crit) x, y, w, h = track_window img 2 = cv. rectangle(frame, (x, y), (x+w, y+h), 255, 2) cv. imshow ('img 2', img 2) k = cv. wait. Key(60) & 0 xff if k == 27: break else: cv. imwrite(chr(k)+". jpg", img 2) else: break cv. destroy. All. Windows() cap. release()
Example: Face Detection import numpy as np import cv 2 as cv # Load classifiers face_cascade = cv. Cascade. Classifier ('haarcascade_frontalface_default. xml' ) eye_cascade = cv. Cascade. Classifier ('haarcascade_eye. xml' ) # Read Image img = cv. imread ('sachin. jpg' ) gray = cv. cvt. Color(img, cv. COLOR_BGR 2 GRAY) # Detect faces = face_cascade. detect. Multi. Scale(gray, 1. 3, 5) for (x, y, w, h) in faces: cv. rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2) roi_gray = gray[y: y+h, x: x+w] roi_color = img[y: y+h, x: x+w] eyes = eye_cascade. detect. Multi. Scale(roi_gray) for (ex, ey, ew, eh) in eyes: cv. rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2) cv. imshow ('img', img) cv. wait. Key(0) cv. destroy. All. Windows()
import matplotlib. pyplot as plt import matplotlib. image as mpimg = mpimg. imread('resim 1. png') print(img) lum_img = img[: , 0] imgplot = plt. imshow(lum_img) plt. show()
import numpy as np import cv 2 import matplotlib. pyplot as plt import matplotlib. image as mpimg im = cv 2. imread('resim 1. png', 0) scale_percent = 20 width = int(im. shape[1] * scale_percent / 100) height = int(im. shape[0] * scale_percent / 100) # dsize = (width, height) # resize image im = cv 2. resize(im, dsize) kernel_sharpening = np. array([[-1, -1], [-1, 9, -1], [-1, -1]]) # applying the sharpening kernel to the input image & displaying it. sharpened = cv 2. filter 2 D(im, -1, kernel_sharpening) #cv 2. imshow('image', sharpened); plt. subplot(121), plt. imshow(im, 'gray'), plt. title('ORIGINAL') plt. subplot(122), plt. imshow(sharpened, 'gray'), plt. title('SHARPENED') #cv 2. waitkey(0); #cv 2. destroy. All. Windows() plt. show()
- Slides: 50