Python for OPENCV development----simple picture manipulation __python

Source: Internet
Author: User
[Python]   View plain copy #!/usr/bin/python2   # coding: utf-8      import  cv2   import numpy as np      #原始图片    image =  Cv2.imread ('./meinv2_original.jpg ',  cv2. Cv_load_image_color)    cv2.imshow (' Original image ',  image)       #截取图片的一部分 , that is, ROI (region of interest)    #因为在python  cv2 the picture in the Ndarray format, so directly with the Ndarray    # Slicing is a very easy way to intercept ROI, just like the fragment usage of the list in Python, except for    #这个是在二维数组上分片    crop = image[0:201,  100:301]  #分片都是不包含后面一个参数的, so many 1   cv2.imshow (' Crop image ',  crop)    Cv2.imwrite ( './meinv2_crop.jpg ',  crop       #处理过的灰度图片    gray = cv2.cvtcolor ( Image, cv2. Color_bgr2gray)    cv2.imshow (' Gray image ',  gray)    cv2.imwrite ('./meinv2_gray.jpg ') ,  gray)       #在图片上Draw a Box    imagerect = image.copy ()    p1 =  (300, 200)    P2  =  (500, 300)    color =  (0, 0, 255)   #BGR的顺序, this color is red     Cv2.rectangle (Imagerect, p1, p2, color)    cv2.imshow (' rectangle an  Image ',  imagerect    cv2.imwrite ('./meinv2_rect.jpg ',  imagerect)       # Picture scaling    size =  (400, 300)    imageresize = cv2.resize (image,  Size)    #可以不指定缩放的图片大小, and specify the scaling ratio, such as below, to zoom to half the size    #即将缩放尺寸设为0, and then set the zoom ratio in the XY direction respectively    # Imageresize = cv2.resize (image,  (0,0),  fx=0.5, fy=0.5)    cv2.imshow (' Resize  an image ',  imageresize)    cv2.imwrite ('./meinv2_resize.jpg ',  imageresize)        #保留单一通道色彩, the channel order is bgr   b = image.copy ()    b[:,:,1] = 0    b[:,:,2] = 0   cv2.imshow (' Blue image ',  b)    cv2.imwrite ('./meinv2_blue.jpg ',  b       g = image.copy ()    g[:,:,0] = 0   g[:, :,2] = 0   cv2.imshow (' Green image ',  g)    cv2.imwrite ('./meinv2_ Green.jpg ',  g)       r = image.copy ()   
Related Article

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.