当前位置:网站首页>Practical puzzle solving | how to extract irregular ROI regions in opencv
Practical puzzle solving | how to extract irregular ROI regions in opencv
2022-07-04 14:26:00 【Xiaobai learns vision】
Click on the above “ Xiaobai studies vision ”, Optional plus " Star standard " or “ Roof placement ”
Heavy dry goods , First time delivery
What is? ROI
ROI It's English Region Of Interest Three acronyms of , Many times, our analysis of images is image specific ROI Analysis and understanding of , For cells and medical images ,ROI Only when the extraction is correct can subsequent analysis be carried out 、 measurement 、 Calculate density, etc , And these ROI The area is often not rectangular , Generally, it is an irregular polygon area , quite a lot OpenCV Beginners don't know how to extract these irregular ROI Area . Actually OpenCV There is a very convenient API Function can quickly extract all kinds of abnormal ROI Area .
extract ROI Area
Before doing this , First of all, let's know what is in image processing mask( Mask ),OpenCV It is defined in Mask Of : Eight bit single channel Mat object , Each pixel value is zero or non-zero area . When Mask When an object is added to the image area , Only non-zero regions are visible ,Mask All pixel values in the image are zero, and the area overlapped with the image will not be visible , in other words Mask The shape and size of the area directly determines the size and shape of the final image you see . A specific example is as follows :
It can be seen that ,mask The function of can Help us extract various irregular regions .OpenCV To complete the above steps, you only need to call API function bitwise_and that will do .
So another problem also follows , How can we generate such mask Area , The answer is OpenCV There are two ways to do it Mask Region generation .
Method 1 :
By manually selecting , Then you can do it through polygon filling , The code implementation is as follows :
import cv2 as cv
import numpy as np
src = cv.imread("D:/images/gc_test.png")
cv.imshow("input", src)
h, w, c = src.shape
# Hand drawn ROI Area
mask = np.zeros((h, w), dtype=np.uint8)
x_data = np.array([124, 169, 208, 285, 307, 260, 175])
y_data = np.array([205, 124, 135, 173, 216, 311, 309])
pts = np.vstack((x_data, y_data)).astype(np.int32).T
cv.fillPoly(mask, [pts], (255), 8, 0)
cv.imshow("mask", mask)
# according to mask, extract ROI Area
result = cv.bitwise_and(src, src, mask=mask)
cv.imshow("result", result)
cv.waitKey(0)
The operation effect is as follows :
Method 2 :
This is also OpenCV The most confused place for beginners , How to generate by program mask, It's really simple . Let's watch the code demonstration !
src = cv.imread("D:/images/gc_test.png")
cv.imshow("input", src)
# Generate mask Area
hsv = cv.cvtColor(src, cv.COLOR_BGR2HSV)
mask = cv.inRange(hsv, (156, 43, 46), (180, 255, 255))
cv.imshow("mask", mask)
# extract ROI Area , according to mask
result = cv.bitwise_and(src, src, mask=mask)
cv.imshow("result", result)
cv.waitKey(0)
The effect is as follows :
It is mainly divided into three steps
Extract outline ROI
Generate Mask Area
Extract the specified contour
One thing in particular to note -> Which produces Mask According to the outline 、 Analysis of binary connected components 、inRange Wait for the processing method to get . Here is based on inRange The way to get mask Area , Then extract .
Practical application demonstration
Finally, let's see two that will be used in actual processing mask Realization ROI Extract and then re fuse the background to generate a new image effect :
The good news !
Xiaobai learns visual knowledge about the planet
Open to the outside world
download 1:OpenCV-Contrib Chinese version of extension module
stay 「 Xiaobai studies vision 」 Official account back office reply : Extension module Chinese course , You can download the first copy of the whole network OpenCV Extension module tutorial Chinese version , Cover expansion module installation 、SFM Algorithm 、 Stereo vision 、 Target tracking 、 Biological vision 、 Super resolution processing and other more than 20 chapters .
download 2:Python Visual combat project 52 speak
stay 「 Xiaobai studies vision 」 Official account back office reply :Python Visual combat project , You can download, including image segmentation 、 Mask detection 、 Lane line detection 、 Vehicle count 、 Add Eyeliner 、 License plate recognition 、 Character recognition 、 Emotional tests 、 Text content extraction 、 Face recognition, etc 31 A visual combat project , Help fast school computer vision .
download 3:OpenCV Actual project 20 speak
stay 「 Xiaobai studies vision 」 Official account back office reply :OpenCV Actual project 20 speak , You can download the 20 Based on OpenCV Realization 20 A real project , Realization OpenCV Learn advanced .
Communication group
Welcome to join the official account reader group to communicate with your colleagues , There are SLAM、 3 d visual 、 sensor 、 Autopilot 、 Computational photography 、 testing 、 Division 、 distinguish 、 Medical imaging 、GAN、 Wechat groups such as algorithm competition ( It will be subdivided gradually in the future ), Please scan the following micro signal clustering , remarks :” nickname + School / company + Research direction “, for example :” Zhang San + Shanghai Jiaotong University + Vision SLAM“. Please note... According to the format , Otherwise, it will not pass . After successful addition, they will be invited to relevant wechat groups according to the research direction . Please do not send ads in the group , Or you'll be invited out , Thanks for your understanding ~
边栏推荐
- AI与生命科学
- 递增的三元子序列[贪心训练]
- 卷积神经网络经典论文集合(深度学习分类篇)
- NowCoder 反转链表
- Leetcode T47: 全排列II
- R language uses dplyr package group_ The by function and the summarize function calculate the mean and standard deviation of the target variables based on the grouped variables
- flink sql-client. SH tutorial
- gin集成支付宝支付
- Progress in architecture
- Test process arrangement (3)
猜你喜欢
docker-compose公网部署redis哨兵模式
一文概览2D人体姿态估计
flink sql-client. SH tutorial
Nowcoder reverse linked list
【信息检索】分类和聚类的实验
Talk about 10 tips to ensure thread safety
Supprimer les lettres dupliquées [avidité + pile monotone (maintenir la séquence monotone avec un tableau + Len)]
第十七章 进程内存
10.(地图数据篇)离线地形数据处理(供Cesium使用)
Learn kernel 3: use GDB to track the kernel call chain
随机推荐
R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布
Test process arrangement (2)
【信息检索】分类和聚类的实验
实战解惑 | OpenCV中如何提取不规则ROI区域
docker-compose公网部署redis哨兵模式
第十六章 字符串本地化和消息字典(二)
RK1126平台OSD的实现支持颜色半透明度多通道支持中文
Leetcode T49: 字母异位词分组
R语言使用lattice包中的bwplot函数可视化箱图(box plot)、par.settings参数自定义主题模式
Install and use MAC redis, connect to remote server redis
Matters needing attention in overseas game Investment Agency
The failure rate is as high as 80%. What are the challenges on the way of enterprise digital transformation?
Visual Studio调试方式详解
R language uses bwplot function in lattice package to visualize box plot and par Settings parameter custom theme mode
R language uses the DOTPLOT function of epidisplay package to visualize the frequency of data points in different intervals in the form of point graph, and uses the by parameter to specify the groupin
MySQL的触发器
[FAQ] Huawei Account Service Error Report 907135701 Common reasons Summary and Solutions
sql优化之查询优化器
MySQL stored procedure exercise
DDD application and practice of domestic hotel transactions -- Code