提取圖像感興趣區域_從圖像中提取感興趣區域

提取圖像感興趣區域

Welcome to the second post in this series where we talk about extracting regions of interest (ROI) from images using OpenCV and Python.

歡迎來到本系列的第二篇文章,我們討論使用OpenCV和Python從圖像中提取感興趣區域(ROI)。

As a recap, in the first post of this series we went through the steps to extract balls and table edges from an image of a pool table. We used simple OpenCV functions like inRange, findContours, boundingRect, minAreaRect, minEnclosingCircle, circle, HoughLines, line etc to achieve our objective.

回顧一下,在本系列的第一篇文章中,我們完成了從臺球桌圖像中提取球和桌邊的步驟。 我們使用了簡單的OpenCV函數(例如inRange,findContours,boundingRect,minAreaRect, minEnclosingCircle,circle,HoughLines,line等)來實現我們的目標。

For beginners in OpenCV, I would recommend to go through that post in order to get familiar with the usage of the above functions.

對于OpenCV初學者,我建議您仔細閱讀該文章 ,以熟悉上述功能的用法。

In this post we will look at a somewhat more complex problem and explore some methods which we can use to obtain the desired results.

在這篇文章中,我們將研究一個更為復雜的問題,并探索一些可用于獲得所需結果的方法。

Our task today is to extract the desired segments from an image which contains a snapshot of a patients brain activity map. The extracted segments can then be used in numerous applications e.g. in a Machine Learning model which can diagnose any health anomalies.

今天的任務是從包含患者大腦活動圖快照的圖像中提取所需的片段。 然后,可以將提取的段用于多種應用程序,例如可以診斷任何健康異常的機器學習模型。

So let us start by looking at the input image itself. It is a typical report generated by medical instruments used in the field of Neurological Science which use sensors to detect signals from a patients brain and display them as colored maps. Typically there are four maps, all of which depict a certain feature and are analyzed together for diagnosis (further details are out of current scope).

因此,讓我們從查看輸入圖像本身開始。 這是由神經科學領域的醫療儀器生成的典型報告,該儀器使用傳感器檢測來自患者大腦的信號并將其顯示為彩色地圖。 通常,有四張地圖,所有地圖都描繪了某個特征并一起分析以進行診斷(更多詳細信息不在當前范圍內)。

From the above image, we want to extract only the regions corresponding to the four maps (head scans) leaving everything else out. So lets get going.

從上面的圖像中,我們只想提取與四個地圖(頭部掃描)相對應的區域,而將其他所有內容都排除在外。 因此,讓我們開始吧。

The first step is detecting the edges of the segments we want to extract. This is a multi step process as mentioned below:

第一步是檢測我們要提取的片段的邊緣。 這是一個多步驟過程,如下所述:

  1. Convert the RGB image to gray-scale using “cvtColor()”

    使用“ cvtColor()”將RGB圖像轉換為灰度

  2. Remove noise from the gray-scale image by applying a blurring function “GaussianBlur()”

    通過應用模糊函數“ GaussianBlur()”來消除灰度圖像中的噪聲

  3. Finally applying the “Canny()” function to the blurred image to obtain the edges

    最后將“ Canny()”函數應用于模糊圖像以獲得邊緣

The output of the edge detection process looks something like this:

邊緣檢測過程的輸出如下所示:

Image for post
Edge detection output using Canny algorithm (image source author)
使用Canny算法的邊緣檢測輸出(圖像源作者)

Notice that although the brain map segments are identified, there are a lot of unwanted edges which need to be eliminated and some of the edges have gaps in between which need to be closed.

請注意,盡管已識別出腦圖片段,但仍有許多不需要的邊緣需要消除,并且某些邊緣之間有間隙需要封閉。

A common method applied for such purpose is Morphological Transformation which involves using a succession of dilations and erosions on the image to remove unwanted edges and close gaps.

用于此目的的常用方法是形態轉換 ,它涉及在圖像上使用一系列的擴張和腐蝕來去除不需要的邊緣和閉合間隙。

We use OpenCV functions “dilate()” and “erode()” over multiple iterations to get an output as below.

我們通過多次迭代使用OpenCV函數“ dilate()”和“ erode()”來獲得如下輸出。

Image for post
Some enhancements in the edges using OpenCV (image source author)
使用OpenCV(圖像源作者)對邊緣進行了一些增強

As you can see, the edges are now complete and much smoother than before.

如您所見,邊緣現在已經完成并且比以前光滑得多。

Now we can extract the contours in this image using OpenCV function “findContours()” and select only those contours which have the following properties:

現在,我們可以使用OpenCV函數“ findContours()”提取該圖像中的輪廓,并僅選擇具有以下屬性的輪廓:

  1. Geometry is circle or oval shaped

    幾何形狀是圓形或橢圓形
  2. Area is above a certain threshold (the value 7000 works fine for this example).

    面積大于某個閾值(在此示例中,值7000可以正常工作)。

For the first part, we will detect the bounding rectangle of each contour using OpenCV “boundingRect()” and check whether the aspect ratio (height to width ratio) is close to 1.

對于第一部分,我們將使用OpenCV“ boundingRect()”檢測每個輪廓的邊界矩形,并檢查縱橫比(高寬比)是否接近1。

It may appear that our task is finished but there is a little bit of fine tuning required.

看來我們的任務已經完成,但需要進行一些微調。

It is often the case that multiple overlapping contours are detected over a segment whereas we are interested in only one.

通常情況是在一個片段上檢測到多個重疊的輪廓,而我們只對一個感興趣。

This problem is solved using Non Maxima Suppression, i.e. we look at all overlapping contours and select the one with the maximum area as the final candidate. The logic is pretty straightforward hence we do not need any inbuilt OpenCV or Python functions.

使用非最大抑制可以解決此問題,即,我們查看所有重疊的輪廓,然后選擇面積最大的輪廓作為最終候選輪廓。 邏輯非常簡單,因此我們不需要任何內置的OpenCV或Python函數。

Another important logic is to identify the four segments separately i.e. Top-Left, Top-Right, Bottom-Left and Bottom-Right.

另一個重要的邏輯是分別識別四個段,即左上,右上,左下和右下。

This is also pretty straightforward and involves identifying the image center coordinates as well as the centroid of each of our detected segments. Centroid detection of a segment contour requires applying the OpenCV “moments()” function on the contour and then calculating the center X, Y coordinates using the formula below: center_x, center_y = (int(M[“m10”] / M[“m00”]), int(M[“m01”] / M[“m00”]))

這也非常簡單,涉及識別圖像中心坐標以及每個檢測到的片段的質心。 對段輪廓進行質心檢測需要在輪廓上應用OpenCV “ moments()”函數,然后使用以下公式計算中心 X,Y坐標: center_x,center_y =(int(M [“ m10”] / M [” m00”]),int(M [“ m01”] / M [“ m00”]))

Comparing the segment centroid coordinates with the image center coordinates lets us place the four segments in their respective positions.

將線段質心坐標與圖像中心坐標進行比較,可以將四個線段放置在各自的位置。

Now that we have the four segments identified, we need to build the image mask which will allow us to pull out the desired features from the original image.

現在我們已經確定了四個部分,我們需要構建圖像蒙版,這將使我們能夠從原始圖像中提取所需的特征。

We will use the OpenCV function “drawContours()” using color as White (R,G,B=255,2555,255) and thickness as FILLED (-1) to draw all four segment contours over a black background. The result looks like below:

我們將使用OpenCV函數“ drawContours()”,將顏色用作白色(R,G,B = 255,2555,255),將厚度用作FILLED(-1)在黑色背景上繪制所有四個線段輪廓。 結果如下所示:

Image for post
Mask for extracting our ROIs (image source author)
用于提取我們的ROI的蒙版(圖像來源作者)

Applying this mask on the original image gets us the desired segments over a background of our choice (e.g. Black or White).

在原始圖像上應用此蒙版可以在我們選擇的背景(例如黑色或白色)上為我們提供所需的分段。

For a black background we create a black canvas and then draw upon it using the OpenCV function “bitwise_and()” with the previously obtained mask.

對于黑色背景,我們創建一個黑色畫布,然后使用OpenCV函數“ bitwise_and()”以及先前獲得的蒙版在其上進行繪制。

Image for post
Extracted ROIs over a black background (image source author)
在黑色背景上提取的ROI(圖像源作者)

For a white background we first create a white canvas and then create a color inverted mask as below by drawing contours with OpenCV function “drawContours()” in black color (R,G,B = 0,0,0) and thickness as FILLED (-1).

對于白色背景,我們首先創建一個白色畫布,然后通過使用OpenCV函數“ drawContours()”繪制輪廓為黑色(R,G,B = 0,0,0)并且厚度為FILLED的顏色,如下所示創建顏色反轉的蒙版(-1)。

Image for post
An alternative Inverted mask for ROI extraction (image source author)
用于ROI提取的備用倒置掩模(圖像源作者)

We then add this inverted mask with the previously obtained black background using OpenCV “add()” function and achieve the same result but with white background.

然后,我們使用OpenCV “ add()”函數將此反向蒙版添加到先前獲得的黑色背景中,并獲得相同的結果,但使用白色背景。

Image for post
Extracted ROIs over a white background (image source author)
在白色背景上提取的ROI(圖像源作者)

This concludes the current post in which we looked at few methods using which we can easily extract regions of interest from images.

到此為止,我們在當前文章中總結了幾種方法,可以輕松地從圖像中提取感興趣區域。

It should be noted that the methods used above may undergo modifications in case of other images with varying complexity. However the basics discussed above would lay the groundwork for any advanced techniques that may be required to solve such problems.

應當注意,在具有變化的復雜度的其他圖像的情況下,上面使用的方法可以進行修改。 但是,以上討論的基礎將為解決此類問題所需的任何高級技術奠定基礎。

翻譯自: https://towardsdatascience.com/extracting-regions-of-interest-from-images-dacfd05a41ba

提取圖像感興趣區域

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/388735.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388735.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388735.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

解決java compiler level does not match the version of the installed java project facet

ava compiler level does not match the version of the installed java project facet錯誤的解決 因工作的關系,Eclipse開發的Java項目拷來拷去,有時候會報一個很奇怪的錯誤。明明源碼一模一樣,為什么項目復制到另一臺機器上,就會…

php模板如何使用,ThinkPHP如何使用模板

到目前為止,我們只是使用了控制器和模型,還沒有接觸視圖,下面來給上面的應用添加視圖模板。首先我們修改下 Action 的 index 操作方法,添加模板賦值和渲染模板操作。PHP代碼classIndexActionextendsAction{publicfunctionindex(){…

理解Windows窗體和WPF中的跨線程調用

你曾開發過Windows窗體程序,可能會注意到有時事件處理程序將拋出InvalidOperationException異常,信息為“ 跨線程調用非法:在非創建控件的線程上訪問該控件”。這種Windows窗體應用程序中 跨線程調用時的一個最為奇怪的行為就是,有…

什么是嵌入式系統

在我們的日常生活中,我們經常使用許多使用嵌入式系統技術設計的電氣和電子電路和套件。計算機,手機,平板,筆記本電腦,數字電子系統以及其他電子和電子設備都是使用嵌入式系統設計的。 什么是嵌入式系統?將硬…

面向數據科學家的實用統計學_數據科學家必知的統計數據

面向數據科學家的實用統計學Beginners usually ignore most foundational statistical knowledge. To understand different models, and various techniques better, these concepts are essential. These work as baseline knowledge for various concepts involved in data …

字符串、指針、引用、數組基礎

1.字符串:字符是由單引號所括住的單個字母、數字或符號。若將單引號改為雙引號,該字符就會變成字符串。它們之間主要的差別是:雙引號的字符串“A”會比單引號的字符串’A’在字符串的最后補上一個結束符’\0’(Null字符&#xff0…

suse安裝php,SUSE下安裝LAMP

安裝Apache可以看到編譯安裝Apache出錯,rpm包安裝gcc (首先要安裝GCC)makemake install修改apache端口cd /home/sxit/apache2vi conf/httpd.confListen 8000啟動 apache/home/root/apache2/bin/apachectl start(stop restart)http://localhost:8000安裝一下PHP開發…

自己動手寫事件總線(EventBus)

2019獨角獸企業重金招聘Python工程師標準>>> 本文由云社區發表 事件總線核心邏輯的實現。 <!--more--> EventBus的作用 Android中存在各種通信場景&#xff0c;如Activity之間的跳轉&#xff0c;Activity與Fragment以及其他組件之間的交互&#xff0c;以及在某…

viz::viz3d報錯_我可以在Excel中獲得該Viz嗎?

viz::viz3d報錯Have you ever found yourself in the following situation?您是否遇到以下情況&#xff1f; Your team has been preparing and working tireless hours to create and showcase the end product — an interactive visual dashboard. It’s a culmination of…

php 數組合并字符,PHP將字符串或數組合并到一個數組內方法

本文主要和大家分享PHP將字符串或數組合并到一個數組內方法&#xff0c;有兩種方法&#xff0c;希望希望能幫助到大家。一般寫法&#xff1a;<?php /*** add a string or an array to another array** param array|string $val* param array $array*/function add_val_to_a…

xcode 4 最低的要求是 10.6.6的版本,如果你是 10.6.3的版本,又不想升級的話。可以考慮通過修改版本號的方法進行安裝

xcode 4 最低的要求是 10.6.6的版本&#xff0c;如果你是 10.6.3的版本&#xff0c;又不想升級的話。可以考慮通過修改版本號的方法進行安裝。 一、打開控制臺&#xff1b; 二、使用root用戶&#xff1b; 命令&#xff1a;sudo -s 之后輸入密碼即可 三、編輯 /System/Library/C…

android 調試技巧

1.查看當前堆棧 Call tree new Exception(“print trace”).printStackTrace(); &#xff08;在logcat中打印當前函數調用關系&#xff09; 2.MethodTracing 性能分析與優&#xff08; 函數占用CPU時間&#xff0c; 調用次數&#xff0c; 函數調用關系&#xff09; a) 在程序…

Xml序列化

xml序列化 實現思路 通過程序生成一個xml文件來備份手機短信. 先獲取手機短信的內容 —>通過xml備份.StringBuffer 代碼如下public void click(View view) {StringBuffer sb new StringBuffer();sb.append("<?xml version\"1.0\" encoding\"UTF-8\…

java 添加用戶 數據庫,跟屌絲學DB2 第二課 建立數據庫以及添加用戶

在安裝DB2 之后&#xff0c;就可以在 DB2 環境中創建自己的數據庫。首先考慮數據庫應該使用哪個實例。實例(instance) 提供一個由數據庫管理配置(DBM CFG)文件控制的邏輯層&#xff0c;可以在這里將多個數據庫分組在一起。DBM CFG 文件包含一組 DBM CFG 參數&#xff0c;可以使…

iphone視頻教程

公開課介紹 本課程共28集 翻譯至第15集 網易正在翻譯16-28集 敬請關注 返回公開課首頁 一鍵分享&#xff1a;  網易微博開心網豆瓣網新浪微博搜狐微博騰訊微博郵件 講師介紹 名稱&#xff1a;Alan Cannistraro 課程介紹 如果你對iPhone Development有興趣&#xff0c;以下是入…

在Python中有效使用JSON的4個技巧

Python has two data types that, together, form the perfect tool for working with JSON: dictionaries and lists. Lets explore how to:Python有兩種數據類型&#xff0c;它們一起構成了使用JSON的理想工具&#xff1a; 字典和列表 。 讓我們探索如何&#xff1a; load a…

Vlan中Trunk接口配置

Vlan中Trunk接口配置 參考文獻&#xff1a;HCNA網絡技術實驗指南 模擬器&#xff1a;eNSP 實驗環境&#xff1a; 實驗目的&#xff1a;掌握Trunk端口配置 掌握Trunk端口允許所有Vlan配置方法 掌握Trunk端口允許特定Vlan配置方法 實驗拓撲&#xff1a; 實驗IP地址 &#xff1a;…

django中的admin組件

Admin簡介&#xff1a; Admin:是django的后臺 管理的wed版本 我們現在models.py文件里面建幾張表&#xff1a; class Author(models.Model):nid models.AutoField(primary_keyTrue)namemodels.CharField( max_length32)agemodels.IntegerField()# 與AuthorDetail建立一對一的關…

虛擬主機創建虛擬lan_創建虛擬背景應用

虛擬主機創建虛擬lanThis is the Part 2 of the MediaPipe Series I am writing.這是我正在編寫的MediaPipe系列的第2部分。 Previously, we saw how to get started with MediaPipe and use it with your own tflite model. If you haven’t read it yet, check it out here.…

.net程序員安全注意代碼及服務器配置

概述 本人.net架構師&#xff0c;軟件行業為金融資訊以及股票交易類的軟件產品設計開發。由于長時間被黑客攻擊以及騷擾。從事高量客戶訪問的服務器解決架構設計以及程序員編寫指導工作。特此總結一些.net程序員在代碼編寫安全以及服務器設置安全常用到的知識。希望能給對大家…