OpenCV---The use of numpy arrays and create pictures

Source: Internet
Author: User

One: access to all pixels of the avatar and the UI image for Pixel inversion (a) for loop inversion
Import Cv2 asCvimport NumPy asnpdef access_pixels (image): #对图像的所有像素进行访问Print (image.size) Height,width,channel=image.shape #每个像素3个通道, channel order b,g,r print ("height:%s\r\nwidth:%s\r\nchannel:%s\r\n"%(Height,width,channel))" "Height608Width:343Channel:3    " "     forRowinchRange (height): forColinchRange (width): forCinchRange (channel): #循环会变慢, after 625632 cycles PV=Image[row,col,c] image[row,col,c] = 255- PV #像素取反cv.imshow ("Pixels_demo", image) src= Cv.imread ("./1.png") #读取图片cv. Namedwindow ("Input Image", CV. Window_autosize) #创建GUI窗口 in the form of adaptive cv.imshow ("Input Image", SRC) #通过名字将图像和窗口联系t1= Cv.gettickcount () #获取时间 for precision timing, the number of milliseconds that the operating system started (elapsed) access_pixels (src) T2=cv.gettickcount ()print ((T2 -t1)/ cv.gettickfrequency ()) #getTickFrequency () is the number of seconds to get the result of a second Cv.waitkey (0#等待用户操作, the inside wait parameter is milliseconds, we fill in 0, stands for forever, waits for user Operation Cv.destroyallwindows () #销毁所有窗口
625632 Height: 608 Width: 343 Channel: 3 15.740029368334588 #经历了15秒, is a very time-consuming cycle, we can use the NumPy array access, more convenient and fast
(ii) using built-in methods to reverse (directly using C code execution, more efficient)
def inverse (image):     = Cv.bitwise_not (image)    cv.imshow ("inverse image", IMG)
T1 = Cv.gettickcount ()  = Cv.gettickcount ()
0.09940230583146789
Two: Create a picture using the NumPy array

(i) Creating pictures using multiple channels
def create_img ():     Np.zeros ([400,400,3],np.uint8)    #创建一个三维数组高400, width 400, signal Channel 3, the initial is 0, each channel accounted for 8 bits of   img[: ,:,0] = Np.ones ([400,400]) *255     #将0号通道下 [400,400] area using ones set to 1, then multiply by 255, set it to 255 , note: 3 channels are b,g,r so this is shown as blue    cv.imshow ("new Image", IMG) Create_img () cv.waitkey (0)   #等待用户操作, inside wait parameter is milliseconds, we fill 0, represent is forever, wait for user operation Cv.destroyallwindows ( )  #销毁所有窗口
(ii) Create an image using a single channel (grayscale image)

def create_img ():     = Np.zeros ([400,400,1], np.uint8) #创建一个只有一个信道的三维数组, initially 0    img[:,:,  0] = Np.ones ([400,400]) *127 #修改这个图像的信道为127, gray     cv.imshow ("New Image  ", IMG)
Orso the initial time to use ones will be more flexible)
def create_img ():     = Np.ones ([1, 1    ) #创建一个只有一个信道的三维数组,],np.uint8127 # initially Can be directly operated     cv.imshow ("new Image", IMG)
Cv.imwrite (". 3.png", IMG) #可以进行保存
Third: The use of supplementary numpy

(i) Use of two-dimensional arrays (choose the right type) 1.float type
def create_arr ():     = Np.ones ([3,3],np.float32) #float类型, allow fractional presence    Ml.fill (122.388)    Print (ML) Create_arr ()
[[122.388122.388122.388] [122.388122.388  122.388] [122.388122.388122.388]
2.int type
def create_arr ():     = Np.ones ([3,3],np.uint8) #不允许小数的存在 with a maximum of 255    Ml.fill (122.388)    Print (ML) Create_arr ()
[[122122122] [122122122] [ 122 122 122]]
def create_arr ():     = Np.ones ([3,3],np.uint8) #有位数限制, the high is truncated, and the low has left a    Ml.fill (256.388  )    print (ml) Create_arr ()
[[000] [000] [0  00]]
(ii) Dimension conversion reshape
def create_arr ():     = Np.ones ([3,3],np.uint8)    Ml.fill (122.388)    = ml.Reshape ([1,9]) #注意: Convert dimension, array size or consistent, or error    print (m2)
[[122122122122122122122122  122]]
(iii) using array to customize arrays
Def create_arr ():    = Np.array ([[2,2,3],[4,5,6 ],[7,8,9]],np.uint8)    print (m3)
[[223] [456] [7  89]]

OpenCV---The use of numpy arrays and create pictures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.