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.
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).
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.
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:
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).
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.
Erik has been at Sun since 1989. He has spent most of this time on networkingcentered around IP such as IP Multicast, IPv6, Web performance, TCP/IPmultithreading, STREAMS, as well as significant technical work in the Internet Engineering Task Force. The IETF work has included working on many parts of IPv6 and serving for five years as an IETF Area Director and as such a member of the Internet Engineering Steering Group (IESG). Currently the IETF work includes Scalable IPv6 Multihoming (shim6), Detecting Network Attachment (DNA), and Transparent Interconnection of Lots of Links (TRILL).
Before he joined Sun, Erik worked at the Swedish Institute of Computer Science. He holds degrees from Linkoping University, Stanford University, and Uppsala University.