warch1LD
warch1LD

Reputation: 61

Image Segmentation and Masking

Need assistance with the simple task. I’m playing around with the LISC dataset that contains hematological images taken from peripheral blood and segmentation masks of manual ground truth for these graphical samples. The task is the following:

  1. Segment isolated leukocytes by removing/cropping irrelevant background elements using the segmentation masks given in the dataset. Try this on one sample only.
  2. Once accomplished, go through the whole folder, and segment/crop the rest of the samples.

Results should be like this (these were obtained via a combination of Mask R-CNN, GrabCut, and OpenCV — but not suitable for the current project I’m working on):

Original Image Mask & Segmented Image

Here is the code that I’ve got so far (from jupyter notebook):

%matplotlib inline
from sklearn.datasets import load_sample_images
from matplotlib import pyplot as plt
from matplotlib.pyplot import imread
import cv2

original = imread(r'D:\mask\1_original.bmp')
plt.imshow(original)

enter image description here

segmented = imread(r'D:\mask\1_mask.bmp', cv2.IMREAD_GRAYSCALE)
_, mask = cv2.threshold(segmented, thresh=180, maxval=255, type=cv2.THRESH_BINARY)

plt.imshow(segmented)

enter image description here

I don't know why the image changed its colors since the original mask looks like this (just black and gray):

enter image description here

Here is the code that I'm using for the final result, but colors are distorted and different from the original once the mask is applied, and this is what I'm trying to fix now but to no avail:

%matplotlib inline

import matplotlib.pyplot as plt
from matplotlib.pyplot import imread
import cv2

img_org = cv2.imread(r'D:\mask\1_original.bmp')
img_mask = cv2.imread(r'D:\mask\1_mask.bmp')

##Resizing images
img_org = cv2.resize(img_org, (400,400),  interpolation = cv2.INTER_AREA)
img_mask = cv2.resize(img_mask, (400,400), interpolation = cv2.INTER_AREA)

for h in range(len(img_mask)):
    for w in range(len(img_mask)):
        if img_mask[h][w][0] == 0:
            for i in range(3):
                img_org[h][w][i] = 0
        else:
            continue
                      
plt.imshow(img_org)

enter image description here

Thanks in advance!

Upvotes: 1

Views: 5000

Answers (1)

DerekG
DerekG

Reputation: 3958

The change in colors is the result of the specified heatmap (viridis instead of binary) as noted above in comments.

The output image has different coloration than the input image because OpenCV uses BGR rather than RGB for colors, so it's likely your red and blue channels are swapped. If you read an image with OpenCV and plot with Matplotlib or vice versa. There are two easy solutions:

1.) Both read and plot images with OpenCV. You can replace plt.imshow(im_orig) with:

cv2.imshow("window name", im_orig)
cv2.waitKey(0) # waits until a key is pressed before continuing
cv2.destroyAllWindows()

2.) Swap the R and B channels before plotting with plt.imshow():

im_orig = cv2.cvtColor(im_orig, cv2.COLOR_BGR2RGB)
plt.imshow(im_orig)

Upvotes: 1

Related Questions