当前位置:网站首页>3H proficient in opencv (VI) - image stacking
3H proficient in opencv (VI) - image stacking
2022-06-29 17:58:00 【Hua Weiyun】
| 0、 preparation |
|---|
Right click the new project , choice Python File, Create a new one Python file , Then at the beginning import cv2 Import cv2 library ,import numpy And rename to np.
import cv2import numpy as np
We also need to know where OpenCV in , The direction of the coordinate axis is x Shaft to the right ,y Axis down , The coordinate origin is in the upper left corner , For example, the following one is 640 Pixels , Wide for 480 Pixel image .OK, Let's begin with this section .
| 1、 Stack horizontally |
|---|
call np Of hstack() Horizontal stack method , The parameter is the image we want to stack , The number of parameters is the number we want to stack .
img=cv2.imread("Resources/lena.png")imgHor=np.hstack((img,img))cv2.imshow("Horizontal",imgHor)cv2.waitKey(0) Run to see the effect , Because the parameter we entered is (img,img), So it's the horizontal splicing of two original drawings .
Let's change it to a horizontal three :imgHor=np.hstack((img,img,img))
| 2、 Stack vertically |
|---|
call np Of vstack() Vertical stack method , The parameter is the image we want to stack , The number of parameters is the number we want to stack .
img=cv2.imread("Resources/lena.png")imgVer=np.vstack((img,img))cv2.imshow("Vertical",imgVer)cv2.waitKey(0) Run to see the effect , Because the parameter we entered is (img,img), So it's vertical splicing of two original drawings .
Empathy , We can change it to three vertical :imgVer=np.vstack((img,img,img))
| 3、 Image stack stack |
|---|
If you just use numpy Stack functions , Images of different sizes and channels cannot be stacked , So we need to implement a stacking method ourselves , Here is the template , It can achieve the effect we want , We don't need to understand this code , Because if you understand it, you can't get any useful information , Just know how to use it .
def stackImages(scale,imgArray): rows = len(imgArray) cols = len(imgArray[0]) rowsAvailable = isinstance(imgArray[0], list) width = imgArray[0][0].shape[1] height = imgArray[0][0].shape[0] if rowsAvailable: for x in range ( 0, rows): for y in range(0, cols): if imgArray[x][y].shape[:2] == imgArray[0][0].shape [:2]: imgArray[x][y] = cv2.resize(imgArray[x][y], (0, 0), None, scale, scale) else: imgArray[x][y] = cv2.resize(imgArray[x][y], (imgArray[0][0].shape[1], imgArray[0][0].shape[0]), None, scale, scale) if len(imgArray[x][y].shape) == 2: imgArray[x][y]= cv2.cvtColor( imgArray[x][y], cv2.COLOR_GRAY2BGR) imageBlank = np.zeros((height, width, 3), np.uint8) hor = [imageBlank]*rows hor_con = [imageBlank]*rows for x in range(0, rows): hor[x] = np.hstack(imgArray[x]) ver = np.vstack(hor) else: for x in range(0, rows): if imgArray[x].shape[:2] == imgArray[0].shape[:2]: imgArray[x] = cv2.resize(imgArray[x], (0, 0), None, scale, scale) else: imgArray[x] = cv2.resize(imgArray[x], (imgArray[0].shape[1], imgArray[0].shape[0]), None,scale, scale) if len(imgArray[x].shape) == 2: imgArray[x] = cv2.cvtColor(imgArray[x], cv2.COLOR_GRAY2BGR) hor= np.hstack(imgArray) ver = hor return verHorizontal and vertical stack
- This method has two parameters , The first is the scaling of the original image ,0.5 Is to shrink 1 times ,2 Just zoom in 1 times , It's easy to understand ;
- The second parameter is the matrix of the image stack , You can write any row and any column , The number of columns must be the same , Otherwise, it cannot form a matrix .
- In the following code , We just built 2 Row one-dimensional matrix , Each line is three images .
img=cv2.imread("Resources/lena.png")imgStcak=stackImages(0.5,([img,img,img],[img,img,img]))cv2.imshow("ImgStack",imgStcak)cv2.waitKey(0) Let's run and see the effect :
Since this method can combine images of different channels , That is, grayscale images and color images can be stacked together . Then let's try :
img=cv2.imread("Resources/lena.png")imgGray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)imgStcak=stackImages(0.5,([img,imgGray,img],[img,img,img]))cv2.imshow("ImgStack",imgStcak)cv2.waitKey(0)cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) Is to convert the original color image into gray image , from 3 The channel becomes 1 passageway . Then we replace one of them in the parameter matrix img. Let's run and see the effect :
The content of section 6 , It's simple and interesting , There are many applications in life . A thousand eyes is better than a thousand hands , Go and knock it again ~
边栏推荐
- SRM系统是什么系统?如何应用SRM系统?
- 让 Google 搜索到自己的博客
- 如何使用B/S开发工具DevExtreme的图表控件 - 自定义轴位置?
- SRM系统可以为企业带来什么价值?
- selenium 文件上传方法
- ISO 32000-2 international standard 7.7
- 小白月赛51 补题 E G F
- [Oracle] basic knowledge interview questions
- Premature end of script headers 或 End of script output before headers
- Uploading files using AutoIT
猜你喜欢

Niuke Xiaobai monthly race 52 E group logarithmic sum (inclusion exclusion theorem + dichotomy)

Opencv+yolo-v3 for target tracking

牛客小Bai月赛52 D 环上食虫(尺取+st表)

The soft youth under the blessing of devcloud makes education "smart" in the cloud

Automatic software test - read SMS verification code using SMS transponder and selenium

小白月赛51 补题 E G F

【WebDriver】使用AutoIt上传文件

如何使用B/S开发工具DevExtreme的图表控件 - 自定义轴位置?

布隆过滤器:

自动化软件测试 - 利用短信转发器结合Selenium读取短信验证码
随机推荐
Opencv+yolo-v3 for target tracking
Maidong Internet won the bid of Dajia Insurance Group
selenium 组合键操作
字典树(随学)
JS merge two 2D arrays and remove the same items (collation)
What value can SRM systems bring to the enterprise?
Xiaobai yuesai 51 supplement e g f
Segment tree and tree array template (copy and paste are really easy to use)
How to use the chart control of the b/s development tool devextreme - customize the axis position?
ABC253 D FizzBuzz Sum Hard(容斥定理)
分布式 | 几步快速拥有读写分离
js两个二维数组合并并去除相同项(整理)
与爱同行,育润走进贫困家庭,助推公益事业
Function independent watchdog (iwdg) experiment based on stm32f103zet6 Library
Software testing - you may not understand the basic theoretical knowledge
3h精通OpenCV(八)-形状检测
Graduation season | Huawei experts teach interview tips: how to get a high salary offer from a large factory?
Prevent form resubmission based on annotations and interceptors
从一个被应用商店坑了的BUG说起
小迈科技 X Hologres:高可用的百亿级广告实时数仓建设