从图像中删除特定颜色 [英] remove a particular color from an image

查看:137
本文介绍了从图像中删除特定颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从图像中删除特定的rbg颜色?例如,我想创建一个函数,以传递图像和颜色作为参数,并返回相同的图像,但没有该颜色。

Is it possible to remove a particular rbg color from an image? For example, I want to create a function such that I pass an image and color as a parameter and it returns the same image but without that color.

例如,该函数

功能(图像, R)

应该给我一个没有R阴影的图像。我该怎么办?

should give me an image that has no R shades. How can I do so?

当前,类似的方法起作用:

Currently, something like this works:

def exclusionWithPIL(image, channel):
    out = None
    image = Image.open(image)
    image_data = image.load()
    height,width = image.size
    for loop1 in range(height):
        for loop2 in range(width):
            r,g,b = image_data[loop1,loop2]
            image_data[loop1,loop2] = 0,g,b
    return image

result = rgb_exclusion('./image.jpg', "G")
result.save('new.jpg')

但是在这里,我正在读取图像 Image.open(image)功能。相反,我想传递已经像这样读取的 image1

but here, I'm reading the image like this Image.open(image) inside the function. Instead, I want to pass in image1 which is already read like this:

image1 = load(image1_path)


def load(image_path):
    out = io.imread(image_path)
    out = out.astype(np.float64) / 255
    return out

如何相应地修改功能?
PS我使用skimage的io.imread读取图像。

How can I modify the function accordingly? P.S I use io.imread from skimage to read images.

编辑:
如果我将io.imread加载的图像直接传递到函数中,例如这似乎可行:

if I pass the image loaded by io.imread directly into the function, something like this seems to work:

    out = image.copy()
    if (channel == "R"):
        out[:, :, 0] = 0

但是我不太清楚索引 [:,:,0]

推荐答案

找到了在线教程,可以完全解决您的问题:
教程链接

found a tutorial online which solves exactly your problem: Tutorial Link

解决方案的主要思想是使用PIL打开图像,加载图像,然后遍历图像中的每个像素(使用2个循环)并设置选择颜色为零。

The main idea behind their solution is to use PIL in order to open the image, load it, and then iterate over every pixel in the image (using 2 loops) and setting the chosen color to zero.

编辑: 我认为您的问题是由于事实您试图将其作为参数传递给函数使用 sicikit-image 库( io.imread())加载的图像,然后在您要使用 PILLOW 库在同一张图片上的功能。

I think that your problem occurs because of the fact that you're trying to pass to the function as a parameter an image which was loaded using sicikit-image library (io.imread()), and then inside the function you're trying to use PILLOW library's features on the same image.

这就是为什么它不起作用的原因,因为您要合并两个不同的库。
如我所见,有2种可能的解决方案:

That's why it shouldn't work, you're merging between 2 different libraries. As I see it, there are 2 possible solution:


  1. 将函数的参数设置为图像的路径,而不是图像本身,然后保存

  2. 保持一致,并在将函数作为参数传递给图像之前,使用 PILLOW 库将其加载,并在返回时,仅以 PILLOW 库的形式引用返回的图像。

  1. Make the function's parameter the image's path instead of the image itself, and then save the image in the same directory and do not return anything.
  2. Stay consistent, and before passing the function an image as a parameter, load it using the PILLOW library, and when returning it, refer to the returned image in terms of the PILLOW library only.

这篇关于从图像中删除特定颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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