OpenCV-installation, image processing

test environment

Install the library

We need to install several libraries about python using OpenCV

  • opencv-python
  • opencv-contrib-python
  • pytesseract

Then the normal installation method is to enter the pip install library name in the cmd command line,
but the download speed is very slow, so we need to use the mirror to download
python -m pip

python -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple 库名

Copy all to cmd, change the library name to the library you choose, and download it

test environment

import cv2 as cv

src = cv.imread("C:/Users/acer/Desktop/Other/img/01.jpg")
cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
cv.imshow("input image",src)
cv.waitKey(0)
cv.destroyAllWindows()

There are 2 points to note here:
1. Be sure to import the cv2 library first
2. Read the address of a picture of the machine in cv.imread, use / instead of \

At this point, a picture will appear when you click Run, which means that the environment is successfully built

If the Chinese comment is wrong, please add at the top # coding:utf-8

Get full picture

# coding:utf-8
import cv2 as cv

src = cv.imread("01.jpg")
src2 = cv.imread("01.jpg",cv.IMREAD_GRAYSCALE)#读取灰度图像
cv.imshow("image",src) #给窗口指定一个名字
cv.waitKey(0) #等待时间,毫秒,0表示任意键终止
cv.destroyAllWindows() #触发关闭操作,关闭窗口
  • coding: utf-8: set the encoding format to utf-8
  • import cv2 as cv import opencv library we named cv
  • Read a picture through cv.imread, this picture is in my project, so just write the name directly, if you write the full path elsewhere
  • cv.imread ("01.jpg", cv.IMREAD_GRAYSCALE) The parameter behind is to set to read grayscale image
  • The first parameter of imshow is the name, the second parameter is the picture
  • waitkey wait time
  • destroyAlliwindows operation to close the window

shape can represent the number of image rows, columns, and color channels in opencv in image coordinates


Take some pictures

src = cv.imread("01.jpg")
demo = src[0:50,0:200]
cv.imshow("image",demo) #给窗口指定一个名字
cv.waitKey(0) #等待时间,毫秒,0表示任意键终止
cv.destroyAllWindows() #触发关闭操作,关闭窗口
  • Read pictures through imread
  • By picture name [0: 50,0: 200] height 50 width 200
  • The rest of the operation is similar to the full picture

Read video

# coding:utf-8
import cv2 as cv

vc = cv.VideoCapture("test.mp4");
if vc.isOpened():
    open, frame = vc.read() # open 是布尔值,frame是当前这一帧的图像
else:
    open = False

while open: #如果open是true就是能打开 进入while循环
    ret, frame = vc.read() #读一下
    if frame is None: #如果这个帧为空,就是没有下一帧了,就结束
        break
    if ret == True: #读这一帧数没错误
        gray = cv.cvtColor(frame,cv.COLOR_BGR2GRAY)
        #转换成灰度图 frame:传递当前这一帧,COLOR_BGR2GRAY:生成灰度图

        cv.imshow('result',gray) #展示结果,名字result 传进来gray

        if cv.waitKey(10) & 0xFF == 27: #0xFF == 27 代表执行过程中按一个键就退出
            break
vc.release()
cv.destroyAllWindows()
  • cv.VideoCapture ("test.mp4") Get video address path
  • open is a Boolean value that represents whether to open the video; frame is the image of the current frame
  • cv.cvtColor (frame, cv.COLOR_BGR2GRAY) Convert to grayscale image, frame current frame, COLOR_BGR2GRAY: generate grayscale image
  • 0xFF == 27 means press a key during execution to exit

BGR

Only R

# coding:utf-8
import cv2 as cv

src = cv.imread("01.jpg")
cur_img = src.copy()
cur_img[:,:,0] = 0
cur_img[:,:,1] = 0
cv.imshow('R',cur_img)

cv.waitKey(0) #等待时间,毫秒,0表示任意键终止
cv.destroyAllWindows() #触发关闭操作,关闭窗口

By setting BGR to only keep R BG to 0

image-20200421150812625

Keep only G

# coding:utf-8
import cv2 as cv

src = cv.imread("01.jpg")
cur_img = src.copy()
cur_img[:,:,0] = 0
cur_img[:,:,2] = 0
cv.imshow('G',cur_img)

cv.waitKey(0) #等待时间,毫秒,0表示任意键终止
cv.destroyAllWindows() #触发关闭操作,关闭窗口

By setting BGR only keep G BR to 0

image-20200421150913866

Keep only B

# coding:utf-8
import cv2 as cv

src = cv.imread("01.jpg")
cur_img = src.copy()
cur_img[:,:,1] = 0
cur_img[:,:,2] = 0
cv.imshow('B',cur_img)

cv.waitKey(0) #等待时间,毫秒,0表示任意键终止
cv.destroyAllWindows() #触发关闭操作,关闭窗口

By setting BGR to keep only B GR to 0

image-20200421151012653

Boundary filling

top_size,bottom_size,left_size,right_size = (50,50,50,50)

replicate = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_REPLICATE)
reflect = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size,cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_REFLECT_101)
wrap = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_WRAP)
constant = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size,cv2.BORDER_CONSTANT, value=0)

Drawing through matplot

import matplotlib.pyplot as plt
plt.subplot(231), plt.imshow(img, 'gray'), plt.title('ORIGINAL')
plt.subplot(232), plt.imshow(replicate, 'gray'), plt.title('REPLICATE')
plt.subplot(233), plt.imshow(reflect, 'gray'), plt.title('REFLECT')
plt.subplot(234), plt.imshow(reflect101, 'gray'), plt.title('REFLECT_101')
plt.subplot(235), plt.imshow(wrap, 'gray'), plt.title('WRAP')
plt.subplot(236), plt.imshow(constant, 'gray'), plt.title('CONSTANT')

plt.show()

image-20200421152730651

  • BORDER_REPLICATE: Copy method, that is, copy the edge pixels.
  • BORDER_REFLECT: reflection method, copy the pixels in the image of interest on both sides. For example: fedcba | abcdefgh | hgfedcb
  • BORDER_REFLECT_101: Reflectance method, that is, with the edge pixel as the axis, symmetry, gfedcb | abcdefgh | gfedcba
  • BORDER_WRAP: outer packaging method cdefgh | abcdefgh | abcdefg
  • BORDER_CONSTANT: Constant method, constant value filling.

Numeral Calculations

Get cats and dogs, then dogs = cats + 10, and get the first 5 lines of the final result

img_cat=cv2.imread('cat.jpg')
img_dog=cv2.imread('dog.jpg')
img_cat2= img_cat +10 
img_cat[:5,:,0]

array([[142, 146, 151, ..., 156, 155, 154],
       [107, 112, 117, ..., 155, 154, 153],
       [108, 112, 118, ..., 154, 153, 152],
       [139, 143, 148, ..., 156, 155, 154],
       [153, 158, 163, ..., 160, 159, 158]], dtype=uint8)

View the value of cat 2 at this time

img_cat2[:5,:,0] #查看此时猫2的值

array([[152, 156, 161, ..., 166, 165, 164],
       [117, 122, 127, ..., 165, 164, 163],
       [118, 122, 128, ..., 164, 163, 162],
       [149, 153, 158, ..., 166, 165, 164],
       [163, 168, 173, ..., 170, 169, 168]], dtype=uint8)

Cat 1 + Cat 2 is 142 + 152 = 294 reasonably, the first line is 38
, because unit8 only has 0-255 bits, so the result needs to be% 256 to take the remainder, that is 294-255 = 38

#相当于% 256
(img_cat + img_cat2)[:5,:,0] 

array([[ 38,  46,  56, ...,  66,  64,  62],
       [224, 234, 244, ...,  64,  62,  60],
       [226, 234, 246, ...,  62,  60,  58],
       [ 32,  40,  50, ...,  66,  64,  62],
       [ 60,  70,  80, ...,  74,  72,  70]], dtype=uint8)

Adding this add, it will output% 255 instead of% 255 after crossing the boundary

cv2.add(img_cat,img_cat2)[:5,:,0]	

array([[255, 255, 255, ..., 255, 255, 255],
       [224, 234, 244, ..., 255, 255, 255],
       [226, 234, 246, ..., 255, 255, 255],
       [255, 255, 255, ..., 255, 255, 255],
       [255, 255, 255, ..., 255, 255, 255]], dtype=uint8)

Image fusion

When added directly

img_cat + img_dog


ValueError: operands could not be broadcast together with shapes (414,500,3) (429,499,3) 

An error will be reported, saying that the two values ​​are not the same and there is no way to add them.

Then you need to perform the resize operation to make it the same.
Through resize (picture object, the value that needs to be changed)

img_dog = cv2.resize(img_dog, (500, 414))
img_dog.shape

(414, 500, 3)

addWeighted

res = cv2.addWeighted(img_cat, 0.4, img_dog, 0.6, 0)
plt.imshow(res)

image-20200421154354644

res = cv2.resize(img, (0, 0), fx=4, fy=4)
plt.imshow(res)

Here you set 0, 0 but set the multiple by fx and fy to change

image-20200421154438676

res = cv2.resize(img, (0, 0), fx=1, fy=3)
plt.imshow(res)

image-20200421154454242

Image threshold

ret, dst = cv2.threshold(src, thresh, maxval, type)
  • src: input image, only single channel image can be input, usually grayscale image
  • dst: output graph
  • thresh: threshold
  • maxval: when the pixel value exceeds the threshold (or is less than the threshold, determined by type), the value assigned
  • type: The type of binary operation, including the following 5 types: cv2.THRESH_BINARY; cv2.THRESH_BINARY_INV; cv2.THRESH_TRUNC; cv2.THRESH_TOZERO; cv2.THRESH_TOZERO_INV
  • cv2.THRESH_BINARY exceeds the threshold to take maxval (maximum value), otherwise it takes 0
  • cv2.THRESH_BINARY_INV Inversion of THRESH_BINARY
  • cv2.THRESH_TRUNC greater than the threshold is set as the threshold, otherwise unchanged
  • cv2.THRESH_TOZERO greater than the threshold does not change, otherwise set to 0
  • cv2.THRESH_TOZERO_INV Inversion of THRESH_TOZERO
ret, thresh1 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)
ret, thresh2 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY_INV)
ret, thresh3 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TRUNC)
ret, thresh4 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TOZERO)
ret, thresh5 = cv2.threshold(img_gray, 127, 255, cv2.THRESH_TOZERO_INV)

titles = ['Original Image', 'BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO', 'TOZERO_INV']
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]

for i in range(6):
    plt.subplot(2, 3, i + 1), plt.imshow(images[i], 'gray')
    plt.title(titles[i])
    plt.xticks([]), plt.yticks([])
plt.show()

image-20200421155227547

Image smoothing

img = cv2.imread('lenaNoise.png')

cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

# 均值滤波
# 简单的平均卷积操作
blur = cv2.blur(img, (3, 3))

cv2.imshow('blur', blur)
cv2.waitKey(0)
cv2.destroyAllWindows()


# 方框滤波
# 基本和均值一样,可以选择归一化
box = cv2.boxFilter(img,-1,(3,3), normalize=True)  

cv2.imshow('box', box)
cv2.waitKey(0)
cv2.destroyAllWindows()


# 方框滤波
# 基本和均值一样,可以选择归一化,容易越界
box = cv2.boxFilter(img,-1,(3,3), normalize=False)  

cv2.imshow('box', box)
cv2.waitKey(0)
cv2.destroyAllWindows()



# 高斯滤波
# 高斯模糊的卷积核里的数值是满足高斯分布,相当于更重视中间的
aussian = cv2.GaussianBlur(img, (5, 5), 1)  

cv2.imshow('aussian', aussian)
cv2.waitKey(0)
cv2.destroyAllWindows()


# 中值滤波
# 相当于用中值代替
median = cv2.medianBlur(img, 5)  # 中值滤波

cv2.imshow('median', median)
cv2.waitKey(0)
cv2.destroyAllWindows()


# 展示所有的
res = np.hstack((blur,aussian,median))
#print (res)
cv2.imshow('median vs average', res)
cv2.waitKey(0)
cv2.destroyAllWindows()

Guess you like

Origin www.cnblogs.com/pengcode/p/12747408.html