How do I make a mask from one image and then transfer it to another?

☆樱花仙子☆ 提交于 2020-01-15 04:14:08

问题


I'm trying to solve a homework problem where I need to get a mask from one image (DAPI) and then apply it to the second image (NPM1) of cells (they are the same cells in the exact same location)

I've been running in circles for about 4 hours trying to get the mask applied using a True/False approach but it doesn't seem to work. I've tried and failed with a bunch of other approaches but just pasting the one that I thought would most likely work (I'm super new to coding)

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
from skimage.color import rgb2gray
import cv2

#Load the images

image = np.array(Image.open("NOTREATDAPI.jpg"))
image1 = np.array(Image.open("NOTREATNPM1.jpg"))
No_Treat_DAPI = rgb2gray(image)
No_Treat_NPM1 = rgb2gray(image1)
plt.imshow(image)

#Create a mask using the DAPI image 

arr = np.array(No_Treat_DAPI)
DAPI_stain = arr[:,0] > 25

plt.imshow(arr)

The DAPI image:

The NPM1 image:

I'm trying to only get the regions on the original image that have an intensity of 25 or greater so that all of the black space in the isn't counted towards the mask as I'm trying to get a histogram of intensity of the cells in the NPM1 image.


回答1:


I limited my solution to the use of OpenCV, numpy, and matplotlib.

The general approach is the following:

  1. Load both images as grayscale images, see cv2.imread.
  2. Create a binary mask from the DAPI image using binary thresholding at intensity value 25, see cv2.threshold.
  3. Do some morphological opening to get rid of possible small artifacts, see cv2.morphologyEx and cv2.getStructuringElement.
  4. Calculate the histogram of the NPM1 image, only incorporating the masked pixels, see cv2.calcHist.

Here's the complete code:

import cv2
import matplotlib.pyplot as plt
import numpy as np

# Load images as grayscale
dapi = cv2.imread('images/NOTREATDAPI.jpg', cv2.IMREAD_GRAYSCALE)
npm1 = cv2.imread('images/NOTREATNPM1.jpg', cv2.IMREAD_GRAYSCALE)

# Create a mask using the DAPI image and binary thresholding at 25
_, mask = cv2.threshold(dapi, 25, 255, cv2.THRESH_BINARY)

# Do some morphological opening to get rid of small artifacts
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15, 15)))

# Calculate the histogram using the NPM1 image and the obtained binary mask
hist = cv2.calcHist([npm1], [0], mask, [256], [0, 256])

# Show bar plot of calculated histogram
plt.bar(np.arange(256), np.squeeze(hist))
plt.show()

# Show mask image
cv2.imshow('Mask', mask)
cv2.waitKey(0)
cv2.destroyAllWindows()

The mask then looks like this:

And, the histogram might look like this:

Hope that helps!

P.S. Next time, better use the opencv and python tags instead of only using the cv2 tag. You'll reach way more people.



来源:https://stackoverflow.com/questions/56412418/how-do-i-make-a-mask-from-one-image-and-then-transfer-it-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!