python-opencv study II: related operations (read, display, save) the image

Copyright: Reprinted please contact: [email protected] https://blog.csdn.net/weixin_40928253/article/details/89786423

I, on the OpenCV Introduction

       OpenCV is based on a BSD license (open source) issue of cross-platform computer vision library that can run on Linux, Windows, Android and Mac OS operating systems. It is lightweight and efficient - consists of a series of C functions and a small amount of C ++ classes, while providing an interface Python, Ruby, MATLAB language, etc., many common algorithms implemented image processing and computer vision. OpenCV written in C ++ language, its main interface is also C ++ language, but still retains a large number of C language interface.

       In the development of computer vision project, OpenCV as the more popular open source database, with a wealth of common image processing library, using C / C ++ language, runs on Linux / Windows / Mac operating systems, can quickly achieve Some image processing and recognition tasks. In addition, OpenCV also provides Java, python, cuda, etc. using the interface, basic machine learning algorithm calls, so that the image processing and image analysis becomes more easy to use, allowing developers to spend more effort on algorithm design.

Two, OpenCV applications

1, the direction of the field of computer vision

    1, human-computer interaction
    2, object recognition
    3, the image segmentation
    4, face
    5, the motion recognition
    6, motion tracking
    7, the robot
    8, motion analysis
    9, machine vision
    10, Structure
    11, automotive safe driving

2, the underlying computer operating technology

    Operation of the image data: the allocation, release, replication, and conversion is provided. The video images are input and output I / O, file and camera input, output image and video files).
     Linear algebra algorithms and the operation of the vector and matrix: matrix product, solving the equation, and the characteristic values of the singular values and the like.
    Various dynamic data structures: lists, queues, sets, trees, etc. FIG.
    The basic digital image processing: filtering, edge detection, corner detection, sampling the difference, color conversion, morphological operations, histogram, image pyramids.
    Structure Analysis: connecting member, the outline processing, distance transform, from each calculation, template matching, Hough transform, polygonal approximation, linear fitting, ellipse fitting, the Delaunay triangulation and the like.
    Camera Calibration: Discovery and Tracking calibration mode, calibration, estimated fundamental matrix, homogeneous matrix estimation, stereo correspondence.
    Motion analysis: optical flow, motion segmentation, tracing.
    Target Recognition: wherein method, Hidden Markov Model: HMM.
    Basic GUI: image and video display, keyboard and mouse event handlers, scroll bar.
    Image annotation: lines, quadratic, polygonal, character painting.

Three, opencv operation of the picture

1, reads the image:

 cv2.imread()

 #读取图像 IMREAD_COLOR:读入一副彩色图像。IMREAD_GRAYSCALE:以灰度模式读入图像
    img = cv2.imread("1.jpg",cv2.IMREAD_COLOR)

2, the display image:

 cv2.imshow()

#显示图像(窗口名字,要显示的图片)
  cv2.imshow("picture",img)

3, save the image:

 cv2.imwrite ()

 cv2.imwrite('messigray.png',img)

Complete code:

'''
使用opencv 显示图像
'''
import cv2

if __name__ == "__main__":
    #读取图像 IMREAD_COLOR:读入一副彩色图像。IMREAD_GRAYSCALE:以灰度模式读入图像
    img = cv2.imread("1.jpg",cv2.IMREAD_COLOR)
    #显示图像(窗口名字,要显示的图片)
    cv2.imshow("picture",img)
    k = cv2.waitKey(0)
    # 按下esc不保存退出
    if k == 27:
        cv2.destroyAllWindows()
    # 按下ctrl+s 保存退出
    elif k == ord('s'):
        cv2.imwrite('messigray.png', img)
        cv2.destroyAllWindows()

The results show:

4, display pictures using Matplotlib

plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')

Note here matplotlib need to use the library, remember to use pip install matplotlib download and install. (If you are using pip download is slow, the proposed change pip mirror source.)

Does not change the mirror source, please refer to: https://blog.csdn.net/weixin_40928253/article/details/89786547

code show as below:

'''
使用 Matplotlib显示图片
'''
import cv2
from matplotlib import pyplot as plt


if __name__ == "__main__":
    img = cv2.imread('1.jpg',0)
    plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
    plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
    plt.show()

5, using Matplotlib display pictures and some operations

import cv2
from matplotlib import pyplot as plt

if __name__ == "__main__":
    img = cv2.imread('2.jpg')
    edges = cv2.Canny(img,100,200)

    plt.subplot(121),plt.imshow(img,cmap = 'gray')
    plt.title('Original Image'), plt.xticks([]), plt.yticks([])
    plt.subplot(122),plt.imshow(edges,cmap = 'gray')
    plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
    plt.show()

Results are as follows:

 

About opencv operation of the picture being only learn so much. :)

Guess you like

Origin blog.csdn.net/weixin_40928253/article/details/89786423