[Python]Open CV basic knowledge learning

Open CV is widely used in image processing and target detection, so let’s learn the basics.

OpenCV installation:

Find opencv in anaconda search, and then anaconda will automatically install opencv and related libraries.

Open CV basic operations:

Note that the package name for importing opencv in python is cv2

  • Read pictures:

imread has two parameters:

  • file_name: file path
  • flag: IMREAD_UNCHANGE Do not make changes, read the original image

                  IMREAD_GRAYSCALE reads images as grayscale images

                  IMREAD_COLOR reads images in RGB mode

import cv2

# 显示一张图片

image_name="test_image.jpg"

image = cv2.imread(image_name, 1)      #读取图片
# cv2.imshow('image', image)                    #显示图片
# cv2.waitKey(0)    
  •  Pixel operations
import cv2
image = cv2.imread('test_image.jpg',1)
(b,g,r)=image[100,100]
print(b,g,r)
i=j=0
for i in range(1,1200):
    image[i,j]=(255,255,255)
    for i in range(1,500):
        image[i,j]=(255,255,255)
cv2.imshow('image',image)
cv2.waitKey(0)
  • Write image operation

Syntax:  cv2.imwrite(filename, image)

参数:
filename: A string representing the file name. The filename must include image format like .jpg, .png, etc.
image: It is the image that is to be saved.

Return Value: It returns true if image is saved successfully.

import cv2

# 显示一张图片

image_name="test_image.jpg"

image = cv2.imread(image_name, 1)      #读取图片


image_name_w="test_image1.jpg"         #写入图片的路径名称
cv2.imwrite(image_name_w, image)       #写入图片,参数image是待写入的图片数据

Guess you like

Origin blog.csdn.net/x1987200567/article/details/133089601