简体   繁体   English

如何使用opencv python将红色通道与灰度图像合并?

[英]How to merge red channel with gray scale image with opencv python?

I have two microscope images.我有两个显微镜图像。 One in gray scale, one in red.一种是灰度,一种是红色。 I can merge these in photoshop, imagej, etc. I want to merge these two in opencv so I can perform this operation on many samples.我可以在 photoshop、imagej 等中合并这些。我想在 opencv 中合并这两个,以便我可以对许多样本执行此操作。

So far I've done the following (where dia=grayscale and epi=red).到目前为止,我已经完成了以下操作(其中 dia=grayscale 和 epi=red)。

# Load images
img_dia = cv2.imread(input_dia)
img_epi = cv2.imread(input_epi)

# Slice red channel from fluorescence image
b_epi, g_epi, r_epi = cv2.split(img_epi)

# Now I want to merge the grey scale and red image

No error messages.没有错误信息。 I could not find any documentation or other stack exchange pages to resolve this issue.我找不到任何文档或其他堆栈交换页面来解决此问题。 Any help would be appreciated!任何帮助,将不胜感激!

There are many ways of overlaying and blending images, but I think the nearest result to yours is probably screen -mode blending.有多种叠加和混合图像的方法,但我认为最接近您的结果可能是屏幕模式混合。

You can get what you want quite simply with ImageMagick which is included in most Linux distros and is available for macOS and Windows.您可以使用ImageMagick非常简单地获得您想要的东西,它包含在大多数 Linux 发行版中,可用于 macOS 和 Windows。 So, just in Terminal (or Command Prompt on Windows), you can run the following without needing to write any code:因此,只需在终端(或 Windows 上的命令提示符)中,您无需编写任何代码即可运行以下命令:

magick grey.tif red.tif -compose screen -composite  result.png

在此处输入图片说明

If that is not the exact blend-mode you want, there 69 modes available in ImageMagick and you can see them all listed if you run:如果这不是您想要的确切混合模式, ImageMagick 中有 69 种模式可用,如果您运行,您可以看到它们全部列出:

magick -list compose

So, if you want Hard Light blending, use:因此,如果您想要强光混合,请使用:

magick grey.tif red.tif -compose HardLight -composite  result.png

You alluded to having lots of images to do, if so, you can get thousands done in parallel just by using GNU Parallel .你提到有很多图像要做,如果是这样,你可以通过使用GNU Parallel 并行完成数千个。 You can search for answers on Stack Overflow that use ImageMagick and GNU Parallel by putting the following in the Stack Overflow Search box, including the square brackets:您可以在 Stack Overflow搜索框中输入以下内容(包括方括号),在使用 ImageMagick 和 GNU Parallel 的 Stack Overflow 上搜索答案:

[imagemagick] [gnu-parallel]

Or, provide some more detail on how your files are named and stored and I can help you further.或者,提供有关如何命名和存储文件的更多详细信息,我可以进一步帮助您。

considering input_epi is an RGB image(3 channels)考虑到input_epi是一个 RGB 图像(3 个通道)

when you load an image into opencv it is loaded as BGR by default当您将图像加载到 opencv 时,默认情况下它会加载为 BGR

# colour image
img_epi = cv2.imread('input_epi.jpg')

# red channel
red_epi = img_epi[:,:,2]

# gray image
img_dia = cv2.imread('input_dia',0)

# creating a resultant image for combining only two channels
resultant_image = np.ones((img_dia.shape[0],img_dia.shape[1],2),np.uint8)

# merge two channels into a single two channel array
# first channel red
resultant_image[:,:,0] = red_epi
# second channel gray
resultant_image[:,:,1] = img_dia

Splitting and Merging Image Channels 拆分和合并图像通道

In OpenCV Documentation在 OpenCV 文档中

Warning cv2.split() is a costly operation (in terms of time).警告 cv2.split() 是一个代价高昂的操作(就时间而言)。 So do it only if you need it.所以只有在你需要的时候才这样做。 Otherwise go for Numpy indexing.否则去 Numpy 索引。

Actual red colour is tricky to pin point from a RGB image better to use HSV to extract particular range of colour from an image实际的红色很难从RGB图像中HSV以更好地使用HSV从图像中提取特定范围的颜色

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM