如何从一幅图像制作遮罩,然后再将其转移到另一幅图像? [英] How do I make a mask from one image and then transfer it to another?

查看:141
本文介绍了如何从一幅图像制作遮罩,然后再将其转移到另一幅图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决一个家庭作业问题,我需要从一个图像(DAPI)获取遮罩,然后将其应用于单元格的第二个图像(NPM1)(它们在完全相同的位置是相同的单元格)

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)

我已经绕圈跑了大约4个小时,试图使用真/假"方法来应用蒙版,但是它似乎不起作用.我尝试了很多其他方法,但均以失败告终,但只是粘贴了我认为最有可能使用的方法(我对编码来说是超级新手)

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)

DAPI图像:

NPM1图像:

我正在尝试仅获取原始图像上强度为25或更大的区域,以便在尝试获取的直方图时,不将遮罩中的所有黑色空间都计入遮罩. NPM1图像中细胞的强度.

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.

推荐答案

我将解决方案限制为使用OpenCV,numpy和matplotlib.

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

一般方法如下:

  1. 将两个图像都加载为灰度图像,请参见 cv2.imread .
  2. 在强度值25处使用二进制阈值从DAPI图像创建二进制掩码,请参见 cv2.threshold .
  3. 进行一些形态上的处理以消除可能的小瑕疵,请参见 cv2.getStructuringElement .
  4. 仅包含掩盖的像素,计算NPM1图像的直方图,请参见这是完整的代码:

    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()
    
    

    mask然后看起来像这样:

    而且,直方图可能看起来像这样:

    And, the histogram might look like this:

    希望有帮助!

    P.S.下次,最好使用opencvpython标签,而不是仅使用cv2标签.您将达到更多人的方式.

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

    这篇关于如何从一幅图像制作遮罩,然后再将其转移到另一幅图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆