3. Opencv-python image processing basics (3)

Table of contents

1. Basic operations of images

1. Obtain and modify pixels in the image

2. Get the attributes of the image

 3. Splitting and merging image channels

4. Changes in color space

2. Arithmetic operations on images

1. Addition of images

2. Read data

Summarize:




1. Basic operations of images

1. Obtain and modify pixels in the image

(1) The more important image binarization later is achieved based on modifying the pixels of the image. We can obtain the pixel value of the pixel through the coordinate values ​​of the row and column. For BGR images, it returns an array of blue, green, and red values. For grayscale images, only the corresponding intensity value is returned. Use the same method to modify pixel values.

(2) Code example:

import cv2
import matplotlib.pyplot as plt
img = cv2.imread('123.png')
cv2.putText(img, "I am a pig", (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 2,  [0, 0, 255] ,3, cv2.LINE_AA)# 获取某个像素点的值
px = img[100,100]
# 仅获取蓝色通道的强度值
blue = img[100,100,0]
# 修改某个位置的像素值
img[100,100] = [255,255,255]
cv2.imshow('img',img)
cv2.waitKey(0)

 This code changes one pixel in the image. The visualization effect is not obvious, so the execution effect is not shown.

2. Get the attributes of the image

Image properties include number of rows, columns, and channels, image data type, number of pixels, etc.

Code example:

import cv2
import matplotlib.pyplot as plt
img = cv2.imread('123.png')
cv2.putText(img, "I am a pig", (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 2,  [0, 0, 255] ,3, cv2.LINE_AA)# 获取某个像素点的值
print(img.shape)
print(img.size)
print(img.dtype)
cv2.waitKey(0)

 Results of the:

 3. Splitting and merging image channels

Sometimes it is necessary to work individually on B, G, R channel images. In this case, the BGR image needs to be split into individual channels. Or in other cases, it may be necessary to merge these individual channels into a BGR image. You can do it in the following ways.

Code example:

# 通道拆分
b,g,r = cv.split(img)
# 通道合并
img = cv.merge((b,g,r))

4. Changes in color space

There are more than 150 color space conversion methods in OpenCV. There are two most widely used conversion methods, BGR↔Gray and BGR↔HSV.

API:

cv.cvtColor(input_image,flag)

parameter:

  • input_image: image for color space conversion
  • flag: conversion type
    • cv.COLOR_BGR2GRAY : BGR↔Gray
    • cv.COLOR_BGR2HSV: BGR→HSV




2. Arithmetic operations on images

This part will be used in the target segmentation of post-image processing, and it is also a part that must be mastered.




1. Addition of images

You can add the two images using OpenCV's cv.add() function, or you can simply add the two images through a numpy operation, such as res = img1 + img2. Both images should be of the same size and type, or the second image can be a scalar value.

NOTE: There is a difference between OpenCV addition and Numpy addition. OpenCV's addition is a saturation operation, while Numpy's addition is a modular operation.
Refer to the following code:

>>> x = np.uint8([250])
>>> y = np.uint8([10])
>>> print( cv.add(x,y) ) # 250+10 = 260 => 255
[[255]]
>>> print( x+y )          # 250+10 = 260 % 256 = 4
[4]

This difference becomes even more apparent when you add two images. OpenCV results are a bit better. So we try to use functions in OpenCV. 

We will take the following two images:

 Code:

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

# 1 读取图像
img1 = cv.imread("view.jpg")
img2 = cv.imread("rain.jpg")

# 2 加法操作
img3 = cv.add(img1,img2) # cv中的加法
img4 = img1+img2 # 直接相加

# 3 图像显示
fig,axes=plt.subplots(nrows=1,ncols=2,figsize=(10,8),dpi=100)
axes[0].imshow(img3[:,:,::-1])
axes[0].set_title("cv中的加法")
axes[1].imshow(img4[:,:,::-1])
axes[1].set_title("直接相加")
plt.show()

 The result looks like this:



The results are stunning!


2. Mixing of images

This is actually addition, but the difference is that the weights of the two images are different, which will give people a mixed or transparent feeling. The calculation formula for image blending is as follows:

g(x) = (1−α)f0(x) + αf1(x)

By modifying the value of α (0 → 1), very cool blending can be achieved.

Now we blend the two images together. The weight of the first image is 0.7 and the weight of the second image is 0.3. The function cv2.addWeighted() can perform blending operations on images according to the following formula.

dst = α⋅img1 + β⋅img2 + c

Here γ is taken to be zero.

Refer to the following code:

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

# 1 读取图像
img1 = cv.imread("view.jpg")
img2 = cv.imread("rain.jpg")

# 2 图像混合
img3 = cv.addWeighted(img1,0.7,img2,0.3,0)

# 3 图像显示
plt.figure(figsize=(8,8))
plt.imshow(img3[:,:,::-1])
plt.show()

The window will appear as shown below:





Summarize:

The previous paragraph talks about the end of basic image operations, and the latter paragraph talks about arithmetic operations on images. Next, I will write about some operations related to image processing, including geometric transformation, morphological operations, image smoothing, histogram, edge detection, template matching and Hough transform, etc. Friends who want to know more can continue to follow the idea ^_^.

Guess you like

Origin blog.csdn.net/weixin_44463519/article/details/121166938