当前位置:网站首页>Opencv's image processing. This one is enough (I)
Opencv's image processing. This one is enough (I)
2022-07-20 08:04:00 【Speed turtle】
Because recently, company projects need to use opencv Process the image , So I learned this piece , Now record what you have learned , For use .
One 、 Image reading and display
import cv2
import numpy as np
img = cv2.imread("soble.png")
cv2.imshow("soble",img)
cv2.waitKey(10000)
But there's a hole , If it is a Chinese path , You will find that the picture cannot be read , Need a different way
#path Path for picture
img_np = np.fromfile(path, dtype = np.uint8)
imgs = cv2.imdecode(img_np, -1)
Two 、 Basis of image processing
1. Pixel processing
Read pixel
When reading picture pixels , If the image is a grayscale image, the grayscale value is returned , If it is BGR Images , The return value is B,G,R Value .
import cv2
import numpy as np
img = cv2.imread("soble.png")
# If it is a grayscale image, there is only one return value , If BGR The image returns three values
p = img[11,11]
print(p)
Normally, the color picture returns BGR The value of three channels , If you want to get a channel value separately, this is in , Add a passage after the position
import cv2
import numpy as np
img = cv2.imread("soble.png")
# If it is a grayscale image, there is only one return value , If BGR The image returns three values , If the channel value is added, only the value of the current channel will be returned 【0,1,2】 It represents three channels of blue, green and red
blue = img[11,11,0]
# At this time, the blue channel value is returned
print(blue)
green = img[11,11,1]
# At this time, the green channel value is returned
print(green)
red = img[11,11,2]
# At this time, the red channel value is returned
print(red)
Change the pixel value
If it is a grayscale image, the modification method is :
img[11,11] = 255
If BGR The image is knee style :
img[11,11] = [255,255,255]
perhaps
img[11,11,0] = 255
img[11,11,1] = 255
img[11,11,2] = 255
2. Use numpy visit
Read pixel
This operation is basically the same as the above . It is also divided into grayscale pictures and BGR Two kinds of pictures , The return value is the same . If it is a grayscale image, only one value is returned , If it is BGR The picture returns three values
import cv2
import numpy as np
img = cv2.imread("soble.png")
blue = img.item(78,125,0)
green = img.item(78,125,1)
red = img.item(78,125,2)
print(blue)
print(green)
print(red)
Change the pixel value
Compared to not using numpy,numpy The setting of pixels is more convenient .
The operation mode is as follows :
import cv2
import numpy as np
img = cv2.imread("soble.png")
# Grayscale image : Receive two parameters , The first parameter is pixel position , The second position is the pixel value
img.itemset((88,99),255)
#BGR Images It also receives two parameters , The first parameter is pixel position , But there is one more channel , The second is the pixel value
img.itemset((88,99,0),255)
img.itemset((88,99,0),255)
img.itemset((88,99,0),255)
3. Get image properties
opencv The attributes of image acquisition include three aspects
- shape : That's ok 、 Column 、 The channel number
- Number of pixels
- The data type of the image
1. shape
shape You can get the shape of the image
Returns the number of containing rows , Number of columns , Tuple of the number of channels . If it is a grayscale image, return the number of rows and columns , If it is BGR The image returns the number of lines 、 Number of columns 、 The channel number
import cv2
import numpy as np
# Read images
img = cv2.imread("soble.png")
# Turn the image into a grayscale image
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
print(img.shape) # The output value is (428, 605, 3)
print(gray.shape) # The output value is (428, 605)
2. Number of pixels
size The number of pixels of the image can be obtained
If it is a grayscale image, return the number of lines * Number of columns , If it is BGR The image returns the number of lines * Number of columns * Number of channels
import cv2
import numpy as np
img = cv2.imread("soble.png")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
print(img.size) # The output value is 776820
print(gray.size) # Output value 258940
It can be seen that after turning the picture into a gray image , There is only one channel left in the picture , Compared with BGR The picture pixels are reduced a lot
3. Image type
dtype The data type of the image is returned
import cv2
import numpy as np
img = cv2.imread("soble.png")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
print(img.dtype) #uint8
print(gray.dtype) #uint8
4. Region of interest (ROI)
ROI(region of interest), Region of interest
From the processed image to the box 、 round 、 The ellipse 、 Irregular polygon and other ways to outline the area to be dealt with .
It can be done by various operators (operator) And function to find the region of interest ROI, And the next step of image processing .
import cv2
import numpy as np
img = cv2.imread("soble.png")
cv2.imshow('ori',img)
temp = img[100:300,100:300]
img[100:300,400:600] = temp
cv2.imshow('temp',img)
cv2.waitKey()
cv2.destroyAllWindows()
5. Splitting and merging of channels
1. Split channels
import cv2
import numpy as np
img = cv2.imread("soble.png")
cv2.imshow('ori',img)
b = img[:,:,0]
g = img[:,:,1]
r = img[:,:,2]
cv2.imshow('b',b)
cv2.imshow('g',g)
cv2.imshow('r',r)
cv2.waitKey()
cv2.destroyAllWindows()
You can also use opencv The provided tool class performs channel splitting
import cv2
import numpy as np
img = cv2.imread("soble.png")
cv2.imshow('ori',img)
b,g,r = cv2.split(img)
cv2.imshow('b',b)
cv2.imshow('g',g)
cv2.imshow('r',r)
cv2.waitKey()
cv2.destroyAllWindows()
The results of the two splitting methods are the same , It's shown as follows :
2. Merge channel
adopt opencv Provided tool classes for operation
import cv2
import numpy as np
img = cv2.imread("soble.png")
cv2.imshow('ori',img)
b,g,r = cv2.split(img)
result = cv2.merge([b,g,r])
cv2.imshow('result',result)
cv2.waitKey()
cv2.destroyAllWindows()
You can see that the images after merging are consistent with those before splitting .
3、 ... and 、 Image operation
1. Image addition
Numpy Add
numpy Addition adopts modular addition , If two pixels are added , When the pixel value is less than 255 When, the pixel value of the current addition is taken , If the added pixel value is greater than 255 If so, take the current pixel value pair 255 modulus
OpenCV Add
OpenCV Addition adopts saturation operation addition , How it works : result = cv2.add( Images 1, Images 2)
When adding with saturation operation , If the added pixel value is lower than 255 Then take the current added pixel value , If the added pixel value is greater than 255 Then take 255 As the current pixel value .
Something to be aware of ( The size of the image involved in the operation 、 The type must be the same )
The following code is used to operate :
import cv2
import numpy as np
img = cv2.imread("soble.png")
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow('ori',img)
a = img + img
b = cv2.add(img,img)
cv2.imshow('a',a)
cv2.imshow('b',b)
cv2.waitKey()
cv2.destroyAllWindows()
It can be seen that there is a big problem in the direct addition of this image , After adding, the picture is very different from the original picture . So the next step is to introduce the commonly used image fusion . Compared with this simple image addition effect will be better .
2. Image fusion
The resulting image = Images 1* coefficient 1 + Images 2* coefficient 2 + The amount of brightness adjustment
img = img1*0.3 + img2*0.7 +18
Image fusion function addWeighted
dst = cv.addWeighted(src1,alpha,src2,beta,gamma)
dst = src1*alpha + src2*beta + gamma;
It's important to note that the parameters gamma Don't omit
import cv2
import numpy as np
img = cv2.imread("soble.png")
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow('ori',img)
result = cv2.addWeighted(img,0.3,img,0.7,0)
cv2.imshow('result',result)
cv2.waitKey()
cv2.destroyAllWindows()
It was found by experiment that , You can find alpha and beta It is to control the display proportion of pictures , Which picture has a large coefficient , Then which picture is more obvious .
Four 、 Type conversion
Convert an image from one type to another
OpenCV Provides 200 Various image conversion methods , If necessary, please refer to the official documents
A simple example , take BGR turn gray Come on :
import cv2
import numpy as np
img = cv2.imread("soble.png")
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray',gray)
cv2.waitKey()
cv2.destroyAllWindows()
5、 ... and 、 Geometric transformation
1. Image zoom
dst = cv2.resize(src,dsize)
disze Zoom size
b = cv2.resize(a,(122,122))
dst = cv2.resize(src,dsize,fx,fy)
fx,fy Zoom size , If dsize If it is not empty, follow dsize Zoom , If it is empty, follow fx,fy Zoom
b = cv2.resize(a,None,fx=0.5,fy=0.7)
Code example :
import cv2
import numpy as np
img = cv2.imread("soble.png")
rows,cols = img.shape[:2]
b=cv2.resize(img,(round(cols*0.5),round(rows*1.2)))
cv2.imshow('ori',img)
cv2.imshow('resize',b)
cv2.waitKey()
cv2.destroyAllWindows()
import cv2
import numpy as np
img = cv2.imread("soble.png")
rows,cols = img.shape[:2]
b=cv2.resize(img,None,fx=1,fy=0.5)
cv2.imshow('ori',img)
cv2.imshow('resize',b)
cv2.waitKey()
cv2.destroyAllWindows()
2. Image reversal
dst = cv2.flip(src,flipCode)
flipCode = 0 With X Flip the axis of symmetry
flipCode > 0 With y The axis is the symmetry axis to flip
flipCode < 0 With y The axis is the symmetry axis to flip , And then to x The axis is the symmetry axis to flip
Code example :
import cv2
import numpy as np
img = cv2.imread("soble.png")
b= cv2.flip(img,0)
c= cv2.flip(img,1)
d= cv2.flip(img,-1)
cv2.imshow('ori',img)
cv2.imshow('b',b)
cv2.imshow('c',c)
cv2.imshow('d',d)
cv2.waitKey()
cv2.destroyAllWindows()
Due to time , I'll write here first today, and I'll continue to write the rest of the time .
If you have any questions, you can add wechat communication , Because I have just begun to contact , I hope you can have more exchanges .
边栏推荐
猜你喜欢
一篇文章让你彻底搞懂关于性能测试常见术语的定义
Don't know how to learn MySQL? It's enough to finish the 50 questions of Niuke! (Part II)
I'm confused after work. How to do a qualified test?
Flask框架——数据库操作命令(增删改查)
Flask框架——模型关系(多对多)
YOLOv5改进之二十一:CNN+Transformer——主干网络替换为又快又强的轻量化主干EfficientFormer
Alfred的常见设置
超超超超写实的数字人!让你24小时不停播
Rock paper will develop ar game for San Diego priest baseball team
无重叠区间[贪心练习]
随机推荐
Unityvr -- robot scene 2- robot
CSimpleArray
Whatsns_ Installation instructions for v6.03 mutual wireless SMS plug-in
SQL service 中如何使用IF
Flask send_ Absolute path traversal caused by file function
有关编码表的基础知识
TCP与UDP的区别
10 ppt pages, understand SaaS customer life cycle
Hc-sr04 ultrasonic module driven by 51 single chip microcomputer (LCD1602 display)
Ar beauty platform youcam supports real-time preview of male beard
21特征值和特征向量
Basic knowledge about coding table
Review in the fifth week
Unable to detect release version problem solving
Report on Market Research and investment prospects of China's yellow blood salt and potassium industry (2022 Edition)
读取douban_data.csv文件写入redis
编译器除法优化
js之数组,对象,类数组对象
Rllib学习[2] --env定义 + env rollout
新概念三册笔记-01