当前位置:网站首页>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 ~边栏推荐
- 实时数据仓库
- Innovation and development of independent industrial software
- Popular framework: the use of glide
- Ruiji takeout notes
- One architecture to complete all tasks - transformer architecture is unifying the AI Jianghu on its own
- How to package QT and share exe
- Matters needing attention in overseas game Investment Agency
- flink sql-client. SH tutorial
- 产业互联网则具备更大的发展潜能,具备更多的行业场景
- 电商系统中红包活动设计
猜你喜欢

为什么图片传输要使用base64编码

Data warehouse interview question preparation

第十七章 进程内存

C# wpf 实现截屏框实时截屏功能

数据湖(十三):Spark与Iceberg整合DDL操作

The failure rate is as high as 80%. What are the challenges on the way of enterprise digital transformation?

实时数据仓库

Map of mL: Based on Boston house price regression prediction data set, an interpretable case of xgboost model using map value

Xcode 异常图片导致ipa包增大问题

scratch古堡历险记 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月
随机推荐
Leetcode t49: grouping of alphabetic words
Gorm data insertion (transfer)
The implementation of OSD on rk1126 platform supports color translucency and multi-channel support for Chinese
R language uses the mutation function of dplyr package to standardize the specified data column (using mean function and SD function), and calculates the grouping mean of the standardized target varia
【MySQL从入门到精通】【高级篇】(四)MySQL权限管理与控制
Data warehouse interview question preparation
2022游戏出海实用发行策略
Count the running time of PHP program and set the maximum running time of PHP
Gorm read / write separation (rotation)
ML之shap:基于boston波士顿房价回归预测数据集利用shap值对XGBoost模型实现可解释性案例
Blob, text geometry or JSON column'xxx'can't have a default value query question
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
nowcoder重排链表
opencv3.2 和opencv2.4安装
测试流程整理(3)
MySQL triggers
数据埋点的一些问题和想法
LiveData
LifeCycle
How to operate and invest games on behalf of others at sea