title: Image Segmentation and Masking
body: <p>Need assistance with the simple task. I’m playing around with the <a href="http://users.cecs.anu.edu.au/%7Ehrezatofighi/Data/Leukocyte%20Data.htm" rel="nofollow noreferrer">LISC dataset</a> that contains hematological images taken from peripheral blood and segmentation masks of manual ground truth for these graphical samples. The task is the following:</p>
  <ol>
  <li>Segment isolated leukocytes by removing/cropping irrelevant background elements using the segmentation masks given in the dataset. Try this on one sample only.</li>
  <li>Once accomplished, go through the whole folder, and segment/crop the rest of the samples.</li>
  </ol>
  <p>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):</p>
  <p><a href="https://i.stack.imgur.com/6nuPC.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6nuPC.jpg" alt="Original Image" /></a>
  <a href="https://i.stack.imgur.com/xbgKI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xbgKI.png" alt="Mask &amp; Segmented Image" /></a></p>
  <p>Here is the code that I’ve got so far (from jupyter notebook):</p>
  <pre><code>%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)
  </code></pre>
  <p><a href="https://i.stack.imgur.com/Jsr7q.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Jsr7q.png" alt="enter image description here" /></a></p>
  <pre><code>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)
  </code></pre>
  <p><a href="https://i.stack.imgur.com/JwbQA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JwbQA.png" alt="enter image description here" /></a></p>
  <p>I don't know why the image changed its colors since the original mask looks like this (just black and gray):</p>
  <p><a href="https://i.stack.imgur.com/UT6ui.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UT6ui.png" alt="enter image description here" /></a></p>
  <p>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:</p>
  <pre><code>%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)
  </code></pre>
  <p><a href="https://i.stack.imgur.com/8JAzm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8JAzm.png" alt="enter image description here" /></a></p>
  <p>Thanks in advance!</p>