当前位置:网站首页>Study on the basis of opencv
Study on the basis of opencv
2022-07-26 10:11:00 【SingleDog_ seven】
Follow these days b The teacher on the station did opencv The most basic operation of learning , Next, write down what you have learned .
( This is b Link to learn on the website , If you are interested, you can have a look at )https://www.bilibili.com/video/BV1PV411774y?p=9&share_source=copy_web
This is my photo
def cv_show(name,img):
cv2.imshow(name, img)
cv2.waitKey(0) # 0 Means press any key to exit Other numbers represent how many milliseconds to wait
cv2.destroyAllWindows() # Destroy all windows First, define the function, and then display the image ( Don't knock again and again )
It's used here imshow To show. , If it is to use print Words , The values of each channel of the picture will be output with a matrix . Here's an example .
img = cv2.imread('cat.jpg', cv2.IMREAD_GRAYSCALE) # Grey diagram cv2.IMREAD_GRAYSCALE The second parameter indicates the reading type
print(img)
cv_show('img2', img)Take gray-scale image as an example , The result is :( The value range is 0--255 Between )
[[10 10 10 ... 13 13 13]
[10 10 10 ... 13 13 13]
[10 10 10 ... 13 13 13]
...
[14 7 6 ... 16 9 22]
[40 18 3 ... 15 30 52]
[63 28 0 ... 15 19 47]]
Next is shape The use of , It shows the picture high , wide , And there are several color channels
img = cv2.imread('cat.jpg')
print(img.shape) # h,w,c( Several color channels )This is the running result of the color chart : (296, 474, 3), This is the result of converting into a grayscale image (296, 474)
Save the picture :
# cv2.inwrite(' name ',img) preservation Next is the partial interception of the image :
# Capture part of the image data
img=cv2.imread('cat.jpg')
cat=img[0:200,0:200] # Coordinate range
cv_show('cat',cat)This is the image obtained , The pixel we selected is high :0-200, wide :0-200

Operation of color channel :
The first is the extraction of color channels :
# Color channel extraction
b,g,r=cv2.split(img) # Separate
print(b)
[[12 12 12 ... 11 11 11]
[12 12 12 ... 11 11 11]
[12 12 12 ... 11 11 11]
...
[14 7 6 ... 7 2 15]
[40 18 3 ... 6 23 45]
[63 28 0 ... 6 12 40]]Below the code snippet is the result B Parameters of the channel .
Then there is the combination of color channels :
img=cv2.merge((b,g,r)) # Combine Only images of monochrome channels are preserved : B---0,G---1,R---2
# Only keep R
cur_img =img.copy()
cur_img[:,:,0]=0 #B---0
cur_img[:,:,1]=0 #G---1
cv_show('R',cur_img)
# Only keep B
cur_img =img.copy()
cur_img[:,:,1]=0 #G---1
cur_img[:,:,2]=0 #R---2
cv_show('B',cur_img)
# Only keep R
cur_img =img.copy()
cur_img[:,:,0]=0 #B---0
cur_img[:,:,1]=0 #G---1
cv_show('R',cur_img) Let's show you : This is B Image of the channel 
Fill the image boundary :
# Border filling
top_size,bottom_size,left_size,right_size =(50,50,50,50) # Specify the fill size up, down, left and right
replicate = cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv2.BORDER_REPLICATE) # Replication
We use the replication method here ( Copy the most edge pixel ) give an example , There are also the following methods :
#BORDER_REFLECT Reflection , Copy the pixels in the image of interest on both sides dcba|abcd|dcba
#BORDER_REFLECT_101 Reflection , Take the most edge pixel as the axis , symmetry dcb|abcd|cba
#BORDER_WRAP The outer packing method abcd|abcd|abcd
#BORDER_CONSTANT Constant method
All the above methods use cv2.copyMakeBorder This is for operation
We can also operate on the values in the matrix :
img_cat = cv2.imread('cat.jpg')
img_cat2 = img_cat + 10shape If the value is the same, the two pictures can also be added , exceed 255 Words , The value obtained will be correct 256 Remainder .
cv2.add(img_cat,img_cat2)
# If you don't cross the line, take yourself , If you cross the line, take 255This line of code is also additive , The difference is that if the number after adding is greater than 255, Its value will show 255.

If shape The value is different , You can put their shape Values are converted to the same , And then we'll do the operation :
# Change height and width
img_dog = cv2.resize(img_dog,(474,296))
#shape The output is height and width ,resize What is needed is width and height
We need to pay attention here .shape The values are the output height and width ,resize What is needed is width and height .
Their width and height can also be listed in a certain ratio :
res1 = cv2.resize(img,(0,0),fx=3,fy=1)
# Height and width are in proportion Here are two pictures to fuse :
img_cat = cv2.imread('cat.jpg')
img_dog = cv2.imread('dog.jpg')
res = cv2.addWeighted(img_cat,0.4,img_dog,0.6,0)
# The selected photos and the weight of each photo , The last number represents how much brightness is increased This is the result of fusion

Finally, I want to talk about the operation of video :
vc =cv2.VideoCapture('cat.mp4') # Capture the video
if vc.isOpened(): # Check whether it can be opened correctly
open,frame = vc.read()
else:
open =False
while open:
ret,frame =vc.read()
if frame is None:
break
if ret == True:
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) # Convert to black and white ?
cv2.imshow('result',gray)
if cv2.waitKey(10) & 0xFF ==27: #0xFF==27 Means press the exit key to exit ,waitKey Indicates how long to wait to exit
break
vc.release()
cv2.destroyAllWindows() # Close all windows if Statement to determine whether it can open , If it can't be opened , that while Directly terminate in the statement , If it can be opened , We are here to convert the video into black and white , The video will be played as we have done ,waitkey() The number in represents the number of seconds to wait before closing the video ,0xFF==27 Yes means press esc Key to exit ( According to the key ASCII Modify the code value ).
The above is the whole content of this blog , If there is anything wrong, you are welcome to correct .

边栏推荐
- Use of pclint in vs2013
- Flask framework beginner-03-template
- Common errors when starting projects in uniapp ---appid
- Set view dynamic picture
- 数通基础-二层交换原理
- Interview shock 68: why does TCP need three handshakes?
- 新增市场竞争激烈,中国移动被迫推出限制性超低价5G套餐
- Flutter Event 派发
- Wechat H5 payment on WAP, for non wechat browsers
- C language course design Tetris (Part 1)
猜你喜欢

AirTest

2022 zhongkepan cloud - server internal information acquisition and analysis flag

Necessary for beginners: debug breakpoint debugging skills in idea and common breakpoint skills

Mysql5.7.25 master-slave replication (one-way)

protobuf的基本用法

Use of tabbarcontroller

Unstoppable, pure domestic PCs have been in place, and the monopoly of the U.S. software and hardware system has been officially broken

Server memory failure prediction can actually do this!

Node memory overflow and V8 garbage collection mechanism

The diagram of user login verification process is well written!
随机推荐
Sublime install plug-ins
Replay the snake game with C language (II) end
A new paradigm of distributed deep learning programming: Global tensor
Use of selectors
Vs2019 configuring opencv
Opencv image processing
SSG框架Gatsby访问数据库,并显示到页面上
苹果独占鳌头,三星大举复兴,国产手机在高端市场颗粒无收
Transform between tree and array in JS (hide the children field if the child node of the tree is empty)
Apple dominates, Samsung revives, and domestic mobile phones fail in the high-end market
MySQL 5.7.25 source code installation record
Interpretation of the standard of software programming level examination for teenagers_ second level
Interview shock 68: why does TCP need three handshakes?
Session based recommendations with recurrent neural networks
WARNING: [pool www] server reached pm. max_ children setting (5), consider raising it
Mysql5.7.25 master-slave replication (one-way)
Sqoop【环境搭建 01】CentOS Linux release 7.5 安装配置 sqoop-1.4.7 解决警告并验证(附Sqoop1+Sqoop2最新版安装包+MySQL驱动包资源)
Azkaban [basic knowledge 01] core concepts + features +web interface + Architecture +job type (you can get started with Azkaban workflow scheduling system in one article)
Solve proxyerror: CONDA cannot proceed due to an error in your proxy configuration
汉诺塔II|汉诺塔4柱