当前位置:网站首页>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 ~
边栏推荐
- Test dble split function execution + import time-consuming shell script reference
- 【目标跟踪】|stark配置 win otb
- VB. Net read / write NFC ntag tag source code
- Image migration and data migration synchronization of old and new servers with different Alibaba cloud accounts
- What is a SCM system? What are the advantages of a supply chain management system?
- [try to hack] cookies and sessions
- Spingmvc requests and responses
- Software testing - you may not understand the basic theoretical knowledge
- 2022春夏系列 KOREANO ESSENTIAL重塑时装生命力
- js两个二维数组合并并去除相同项(整理)
猜你喜欢

SRM供应商协同管理系统功能介绍

Walk with love, educate and run poor families, and promote public welfare undertakings

育润多维发力慈善领域,勇抗企业公益大旗

SRM supplier collaborative management system function introduction

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

Bloom filter:

Top 30 open source software

分布式 | 几步快速拥有读写分离

ABC253 D FizzBuzz Sum Hard(容斥定理)

2022 spring summer collection koreano essential reshapes the vitality of fashion
随机推荐
Automatic software test - read SMS verification code using SMS transponder and selenium
Serial port experiment based on stm32f103zet6 library function
Image migration and data migration synchronization of old and new servers with different Alibaba cloud accounts
Kubekey2.2.1 kubernetes1.23.7 offline package production +harbor Department summer and upload image
如何使用B/S开发工具DevExtreme的图表控件 - 自定义轴位置?
Visual studio plug-in coderush officially released v22.1 -- visual tool for optimizing debugging
SRM系统是什么系统?如何应用SRM系统?
VB.Net读写NFC Ntag标签源码
It's really easy to make money in foreign lead and build a website
ISO 32000-2 international standard 7.7
On adding and subtracting dates
从一个被应用商店坑了的BUG说起
Basic operations such as MySQL startup under Windows platform
Maidong Internet won the bid of Dajia Insurance Group
Web Scraping with Beautiful Soup for Data Scientist
Split palindrome string [dp + DFS combination]
牛客小Bai月赛52 D 环上食虫(尺取+st表)
力扣每日一题 06.29 两数相加
[webdriver] upload files using AutoIT
Selenium upload file