4

I've just started with Computer Vision and also with OpenCV.

I'm working with MRI brain images and I wondering if there is a filter or something that let me segment the areas that I have marked with red on this image:

enter image description here

This is the original image:

Yes, I know that there are a lot of deep learning solutions to segment this kind of networks, but I'm wondering, because I'm newbie, if there is a filter to highlight these areas that I have surrounded.

The question is:

Is there a filter or something to highlight that areas that I have surrounded?

This is what I want to get:

enter image description here

nathancy
  • 42,661
  • 14
  • 115
  • 137
VansFannel
  • 45,055
  • 107
  • 359
  • 626
  • How are you hoping the result will look please? – Mark Setchell Feb 10 '20 at 14:53
  • @MarkSetchell I don't know. Maybe I'm going to delete this question. Sorry, I'm new in this field and I haven't found anything useful about it (maybe because I don't know how to search for it). I have added a new picture showing what I want to get. – VansFannel Feb 10 '20 at 14:59
  • You can do that with a *"flood-fill"*. Have a look here for an example... https://stackoverflow.com/a/60044007/2836621 – Mark Setchell Feb 10 '20 at 16:01

3 Answers3

2

With the observation that the sections you want to extract have a specific color, we can use color thresholding to isolate the objects. You didn't specify a language so I'll use Python to demonstrate. The idea is to determine a lower/upper color range then color threshold using cv2.inRange(). We convert the image to HSV format then use this range to generate a binary segmented mask

lower = np.array([0, 0, 118])
upper = np.array([179, 255, 202])

import numpy as np
import cv2

# Color threshold
image = cv2.imread('1.png')
original = image.copy()
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
lower = np.array([0, 0, 118])
upper = np.array([179, 255, 202])
mask = cv2.inRange(hsv, lower, upper)
result = cv2.bitwise_and(original,original,mask=mask)

cv2.imshow('mask', mask)
cv2.imshow('result', result)
cv2.imshow('original', original)
cv2.waitKey()

You can use this color thresholder script with trackbars to fine tune the lower and upper color ranges

import cv2
import numpy as np

def nothing(x):
    pass

# Load image
image = cv2.imread('1.png')

# Create a window
cv2.namedWindow('image')

# Create trackbars for color change
# Hue is from 0-179 for Opencv
cv2.createTrackbar('HMin', 'image', 0, 179, nothing)
cv2.createTrackbar('SMin', 'image', 0, 255, nothing)
cv2.createTrackbar('VMin', 'image', 0, 255, nothing)
cv2.createTrackbar('HMax', 'image', 0, 179, nothing)
cv2.createTrackbar('SMax', 'image', 0, 255, nothing)
cv2.createTrackbar('VMax', 'image', 0, 255, nothing)

# Set default value for Max HSV trackbars
cv2.setTrackbarPos('HMax', 'image', 179)
cv2.setTrackbarPos('SMax', 'image', 255)
cv2.setTrackbarPos('VMax', 'image', 255)

# Initialize HSV min/max values
hMin = sMin = vMin = hMax = sMax = vMax = 0
phMin = psMin = pvMin = phMax = psMax = pvMax = 0

while(1):
    # Get current positions of all trackbars
    hMin = cv2.getTrackbarPos('HMin', 'image')
    sMin = cv2.getTrackbarPos('SMin', 'image')
    vMin = cv2.getTrackbarPos('VMin', 'image')
    hMax = cv2.getTrackbarPos('HMax', 'image')
    sMax = cv2.getTrackbarPos('SMax', 'image')
    vMax = cv2.getTrackbarPos('VMax', 'image')

    # Set minimum and maximum HSV values to display
    lower = np.array([hMin, sMin, vMin])
    upper = np.array([hMax, sMax, vMax])

    # Convert to HSV format and color threshold
    hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv, lower, upper)
    result = cv2.bitwise_and(image, image, mask=mask)

    # Print if there is a change in HSV value
    if((phMin != hMin) | (psMin != sMin) | (pvMin != vMin) | (phMax != hMax) | (psMax != sMax) | (pvMax != vMax) ):
        print("(hMin = %d , sMin = %d, vMin = %d), (hMax = %d , sMax = %d, vMax = %d)" % (hMin , sMin , vMin, hMax, sMax , vMax))
        phMin = hMin
        psMin = sMin
        pvMin = vMin
        phMax = hMax
        psMax = sMax
        pvMax = vMax

    # Display result image
    cv2.imshow('image', result)
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()
nathancy
  • 42,661
  • 14
  • 115
  • 137
1

This is my simple approach but it may be helpful. There is a color difference in the part you want to get at the end. I tried to use it. I just focused on the center of image. I chose the crop area by manually but you can try to get that by taking reference of two black area in the middle.

Here is my simple code:

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
    Mat img = imread("/ur/image/directory/image.png",CV_LOAD_IMAGE_GRAYSCALE);
    imshow("Source",img);

    for(int i=160;i<460;i++)
    {
        for(int j=130;j<460;j++)
        {
            if(img.at<uchar>(Point(j,i))<120)
                img.at<uchar>(Point(j,i)) = 0;
        }
    }

    imshow("Result",img);
    waitKey(0);
    return 0;
}

Here is result image:

enter image description here

Yunus Temurlenk
  • 4,085
  • 4
  • 18
  • 39
1

Doing some simple threshold you can achieve some good result I would say. You can then select area of interest to focus on the area of interest more closely and remove the scull artefacts.

Result

Here is the code I used for this:

/// Global Variables
const int alpha_slider_max = 255;
int alpha_slider;
int alpha_slider2;

cv::Mat image, gray;

void on_trackbar(int, void*)
{
    cv::Mat dst1, dst2, result;
    cv::cvtColor(image, gray, cv::COLOR_BGR2GRAY);
    cv::threshold(gray, dst1, alpha_slider, 255, cv::THRESH_BINARY_INV);
    gray.copyTo(dst2, dst1);
    cv::imshow("partial1", dst2);
    cv::threshold(dst2, dst2, alpha_slider2, 255, cv::THRESH_BINARY);
    cv::imshow("threshold", dst2);
}
int main()
{ 
    image = cv::imread("Brain.png");
    cv::namedWindow("threshold", 1);
    cv::createTrackbar("trakbar", "threshold", &alpha_slider, alpha_slider_max, on_trackbar);
    cv::createTrackbar("trakbar2", "threshold", &alpha_slider2, alpha_slider_max, on_trackbar);
    cv::imshow("original", image);
    while (cv::waitKey(10) != 'q');
}
guivi
  • 382
  • 1
  • 5