当前位置:网站首页>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 ~边栏推荐
猜你喜欢
随机推荐
【云原生】我怎么会和这个数据库杠上了?
R语言使用lattice包中的bwplot函数可视化箱图(box plot)、par.settings参数自定义主题模式
redis 日常笔记
第十六章 字符串本地化和消息字典(二)
Visual Studio调试方式详解
Redis daily notes
一文概览2D人体姿态估计
数据仓库面试问题准备
Ruiji takeout notes
Migration from go vendor project to mod project
按照功能对Boost库进行分类
(1)性能调优的标准和做好调优的正确姿势-有性能问题,上HeapDump性能社区!
LifeCycle
Popular framework: the use of glide
Abnormal value detection using shap value
Leetcode T47: 全排列II
迅为IMX6Q开发板QT系统移植tinyplay
R language dplyr package summary_ If function calculates the mean and median of all numerical data columns in dataframe data, and summarizes all numerical variables based on conditions
【FAQ】華為帳號服務報錯 907135701的常見原因總結和解决方法
scratch古堡历险记 电子学会图形化编程scratch等级考试三级真题和答案解析2022年6月









