当前位置:网站首页>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 ~边栏推荐
- ML之shap:基于boston波士顿房价回归预测数据集利用Shap值对LiR线性回归模型实现可解释性案例
- 为什么图片传输要使用base64编码
- 如何游戏出海代运营、游戏出海代投
- Use of arouter
- 数据仓库面试问题准备
- Ruiji takeout notes
- 去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]
- What is the difference between Bi financial analysis in a narrow sense and financial analysis in a broad sense?
- 迅为IMX6Q开发板QT系统移植tinyplay
- opencv3.2 和opencv2.4安装
猜你喜欢

Leetcode 61: 旋转链表

【信息检索】链接分析

Nowcoder rearrange linked list

Leetcode T48: rotating images

Visual Studio调试方式详解

【MySQL从入门到精通】【高级篇】(五)MySQL的SQL语句执行流程

Excel quickly merges multiple rows of data

Chapter 17 process memory
![[MySQL from introduction to proficiency] [advanced chapter] (V) SQL statement execution process of MySQL](/img/58/a8dbed993921f372d04f7a1ee19679.png)
[MySQL from introduction to proficiency] [advanced chapter] (V) SQL statement execution process of MySQL

flink sql-client.sh 使用教程
随机推荐
ML:SHAP值的简介、原理、使用方法、经典案例之详细攻略
R语言ggplot2可视化:gganimate包创建动画图(gif)、使用anim_save函数保存gif可视化动图
基于51单片机的超声波测距仪
Digi重启XBee-Pro S2C生产,有些差别需要注意
One architecture to complete all tasks - transformer architecture is unifying the AI Jianghu on its own
【云原生】我怎么会和这个数据库杠上了?
sql优化之查询优化器
File creation, writing, reading, deletion (transfer) in go language
【MySQL从入门到精通】【高级篇】(五)MySQL的SQL语句执行流程
[matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions
R language ggplot2 visualization: gganimate package creates dynamic line graph animation (GIF) and uses transition_ The reveal function displays data step by step along a given dimension in the animat
gin集成支付宝支付
【算法leetcode】面试题 04.03. 特定深度节点链表(多语言实现)
【FAQ】華為帳號服務報錯 907135701的常見原因總結和解决方法
PyTorch的自动求导机制详细解析,PyTorch的核心魔法
The mouse wheel of xshell/bash/zsh and other terminals is garbled (turn)
Leetcode T48: rotating images
ViewModel 初体验
(1)性能调优的标准和做好调优的正确姿势-有性能问题,上HeapDump性能社区!
卷积神经网络经典论文集合(深度学习分类篇)