OpenCV journey ----- Numpy array

Nunmpy array contains:

  • Powerful N-dimensional array object

  • Sophisticated (broadcasting) functions

  • Integrated C / C ++ and Fortran code tools

  • Useful linear algebra, Fourier transform and the random number function

All traverse and modify pixel array

1  # loop over all the pixels 
2  DEF access_pixels (Image):
 . 3      Print (image.shape)
 . 4      height = image.shape [0]      # height 
. 5      width = image.shape [1]       # width 
. 6      channels image.shape = [ 2]    # channels 
. 7      Print ( " width:% S, height:% S, channels:% S " % (width, height, channels))
 . 8      for Row in (height) Range:                # cycle Get each pixel 
9          for COL in range(width):
10             for c in range(channels):
11                 pv = image[row, col, c]         #维度
12                 image[row, col, c] = 255 - pv
13     cv.imshow("pixels_demo",image)

Create a new image

Create a new image:

. 1 np.zeros ([400, 400,. 3], np.uint8)         # shape, type,

Code:

1  # assignment Numpy by a specified number of dimensions of the array 
2  DEF create_image ():
 . 3      '' ' 
. 4      # (Create a new three-channel image)
 5      # multichannel common to RGB
 . 6      # np.zeros IMG = ([400 , 400,. 3], np.uint8)
 . 7      # IMG [:,:, 0] = np.ones ([400, 400]) * 255 # 0 indicates a first passage Blue; 1 denotes the second channel Green; 3 represents a third passage the Read
 . 8  
. 9      # common single-channel grayscale image
 10      # np.zeros IMG = ([400, 400,. 1], np.uint8) # Create a new image
 . 11      # IMG [:,:, 0] = np.ones ([400, 400]) * 127
 12 is      IMG np.ones = ([400, 400,. 1], np.uint8)
 13 is      IMG IMG * 127 =
 14      cv.imshow ( "new new Image", IMG) # window
 15     cv.imwrite ( "C: /Users/shinelon/Desktop/DL/001.png") # save the image
 16      '' ' 
. 17  
18 is      # initialize a two-dimensional, the print pixel 
. 19      M1 = np.ones ([. 3,. 3 ], np.float32)
 20 is      m1.fill (222.388 )
 21 is      Print (M1)
 22 is  
23 is      M2 = m1.reshape ([1,9])       # changes its shape in the space 
24      Print (M2)

 

 Other knowledge

Get the current CPU clock:

. 1 T1 = cv.getTickCount ()                   # Get the current CPU time

 The complete code

. 1  Import CV2 AS CV
 2  Import numpy AS NP
 . 3  
. 4  
. 5  # loop over all pixels; interpreted slower 
. 6  DEF access_pixels (Image):
 . 7      Print (image.shape)
 . 8      height = image.shape [0]      # Height 
. 9      width = image.shape [. 1]       # width 
10      channels = image.shape [2]    # channels 
. 11      Print ( " width:% S, height:% S, channels:% S " % (width, height, channels) )
 12      for Row inRange (height):                # cyclic Get each pixel 
13 is          for COL in Range (width):
 14              for C in Range (channels):
 15                  PV = Image [Row, COL, C]          # dimension 
16                  Image [Row, COL, C] = 255 - PV
 . 17      cv.imshow ( " pixels_demo " , Image)
 18 is  
. 19  
20 is  DEF inverse (Image):
 21 is      dest = cv.bitwise_not (Image)     # pixels negated, relying on the code C 
22 is      cv.imshow ( " inverse ", dest)
 23 is  
24  
25  # assignment to the specified dimension of an array by Numpy 
26 is  DEF create_image ():
 27      '' ' 
28      # (Create a new three-channel image)
 29      # multichannel common to RGB
 30      # IMG = NP .zeros ([400, 400,. 3], np.uint8)
 31 is      # IMG [:,:, 0] = np.ones ([400, 400]) * 255 # 0 indicates a first passage Blue; 1 represents a second channel Green; 3 shows a third passage the Read
 32  
33 is      # a common single-channel gray image
 34 is      # np.zeros IMG = ([400, 400,. 1], np.uint8) to create a new image #
 35      # IMG [:, :, 0] = np.ones ([400, 400]) * 127
 36      IMG np.ones = ([400, 400,. 1], np.uint8)
 37 [      IMG IMG * 127 =
 38 is     cv.imshow ( "new image", img ) # window
 39      cv.imwrite ( "C: /Users/shinelon/Desktop/DL/001.png") to save the image #
 40      '' ' 
41 is  
42 is      # initialize a two-dimensional print pixel 
43 is      M1 = np.ones ([. 3,. 3 ], np.float32)
 44 is      m1.fill (222.388 )
 45      Print (M1)
 46 is  
47      M2 = m1.reshape ([1,9])       # change the shape of the space 
48      Print (M2)
 49  
50      M3 = np.array ([[2,. 3,. 4], [. 4,. 5,. 6], [. 7,. 8,. 9]], np.int32)       # convolution neural requires 
51 is      # m3.fill (. 9) 
52 is      Print (M3)
53 is  
54 is  
55  Print ( " ------ ----- the Python the OpenCV the Tutorial " )
 56 is the src = cv.imread ( " C: /Users/shinelon/Desktop/DL/12.png " )       # bracket class absolute image path 
57 cv.namedWindow ( " the INPUT Image " , cv.WINDOW_AUTOSIZE)
 58 cv.imshow ( " the INPUT Image " , src)             # the picture shows the window in Windows 
59 T1 = cv.getTickCount ()                   # get the current CPU time 
60  create_image ()
 61 is  # access_pixels (the src) # long time
62 is inverse (the src)             # optimization, shorter 
63 is T2 = cv.getTickCount ()
 64 Time = (T2 - T1) /cv.getTickFrequency ()       # Run time MS 
65  Print ( " Time:% S MS " % ( * 1000 Time ))
 66  cv.waitKey (0)
 67  
68  
69 cv.destroyAllWindows ()

 

 

Guess you like

Origin www.cnblogs.com/gghy/p/11761949.html