简体   繁体   English

图像的可选颜色

[英]Selective Color of image

I have more then 1 week reading about selective color change of an image.我有超过 1 周的时间阅读有关图像选择性颜色变化的信息。 It meand selcting a color from a color picker and then select a part of image in which I want to change the color and apply the changing of color form original color to color of color picker.这意味着从颜色选择器中选择一种颜色,然后选择我想要更改颜色的图像的一部分,并将颜色从原始颜色的更改应用于颜色选择器的颜色。

Eg if I select a blue color in color picker and I also select a red part in the image I should be able to change red color to blue color in all the image.例如,如果我在颜色选择器中选择了一种蓝色并且我还选择了图像中的红色部分,我应该能够将所有图像中的红色更改为蓝色。

Another example.另一个例子。 If I have an image with red apples and oranges and if I select an apple on the image and a blue color in the color picket, then all apples should be changing the color from red to blue.如果我有一个带有红苹果和橙子的图像,并且如果我在图像上选择了一个苹果,并在色标中选择了一个蓝色,那么所有的苹果都应该将颜色从红色变为蓝色。

I have some ideas but of course I need something more concrete on how to do this我有一些想法,但当然我需要一些关于如何做到这一点的更具体的东西

Thank you for reading感谢您阅读

As a starting point, consider clustering the colors of your image.作为起点,请考虑对图像的颜色进行聚类。 If you don't know how many clusters you want, then you will need methods to determine whether to merge or not two given clusters.如果您不知道您想要多少个集群,那么您将需要确定是否合并两个给定集群的方法。 For the moment, let us suppose that we know that number.现在,让我们假设我们知道这个数字。 For example, given the following image at left, I mapped its colors to 3 clusters, which have the mean colors as shown in the middle, and representing each cluster by its mean color gives the figure at right.例如,给定左边的下图,我将其颜色映射到 3 个簇,它们的平均颜色如中间所示,并用每个簇的平均颜色表示右图。

在此处输入图片说明在此处输入图片说明在此处输入图片说明

With the output at right, now what you need is a method to replace colors.输出正确,现在您需要的是一种替换颜色的方法。 Suppose the user clicks (a single point) somewhere in your image, then you know the positions in the original image that you will need to modify.假设用户单击(单个点)图像中的某个位置,那么您就知道原始图像中需要修改的位置。 For the next image, the user (me) clicked on a point that is contained by the "orange" cluster.对于下一个图像,用户(我)单击了“橙色”集群包含的一个点。 Then he clicked on some blue hue.然后他点击了一些蓝色调。 From that, you make a mask representing the points in the "orange" cluster and play with that.从中,您可以制作一个表示“橙色”集群中点的掩码并使用它。 I considered a simple gaussian filter followed by a flat dilation 3x5.我考虑了一个简单的高斯滤波器,然后是一个 3x5 的平面扩张。 Then you replace the hues in the original image according to the produced mask (after the low pass filtering, the values on it are also considered as a alpha value for compositing the images).然后根据生成的掩码替换原始图像中的色调(低通滤波后,其上的值也被视为合成图像的 alpha 值)。

在此处输入图片说明

Not perfect at all, but you could have a better clustering than me and also a much-less-primitive color replacement method.一点也不完美,但你可以有一个比我更好的聚类,还有一个不那么原始的颜色替换方法。 I intentionally skipped the details about clustering method, color space, and others, because I used only basic k-means on RGB without any pre-processing of the input.我故意跳过了关于聚类方法、颜色空间等的细节,因为我只在 RGB 上使用了基本的 k 均值,而没有对输入进行任何预处理。 So you can consider the results above as a baseline for anything else you can do.因此,您可以将上述结果视为您可以做的任何其他事情的基准。

Given the image, a selected color, and a target new color - you can't do much that isn't ugly.给定图像、选定的颜色和目标新颜色 - 您不能做很多不难看的事情。 You also need a range, some amount of variation in color, so you can say one pixel's color is "close enough" while another is clearly "different".您还需要一个范围,一些颜色的变化量,因此您可以说一个像素的颜色“足够接近”,而另一个则明显“不同”。

First step of processing: You create a mask image, which is grayscale and varying from 0.0 to 1.0 (or from zero to some maximum value we'll treat as 1.0), and the same size as the input image.处理的第一步:您创建一个蒙版图像,它是灰度的,从 0.0 到 1.0(或从零到某个最大值,我们将其视为 1.0)变化,并且与输入图像的大小相同。 For each input pixel, test if its color is sufficiently near the selected color.对于每个输入像素,测试其颜色是否足够接近所选颜色。 If it's "the same" or "close enough" put 1.0 in the mask.如果它“相同”或“足够接近”,则将 1.0 放入掩码中。 If it's different, put 0.0.如果不同,则输入 0.0。 If is sorta borderline, put an in-between value.如果是边界线,则放置一个中间值。 Exactly how to do this depends on the details of the image.究竟如何做到这一点取决于图像的细节。

This might work best in LAB space, and testing for sameness according to the angle of the A,B coordinates relative to their origin.这可能在 LAB 空间中效果最好,并根据 A、B 坐标相对于其原点的角度来测试相同性。

Once you have the mask, put it aside.拿到口罩后,把它放在一边。 Now color-transform the whole image.现在对整个图像进行颜色变换。 This might be best done in HSV space.这可能最好在 HSV 空间中完成。 Don't touch the V channel.请勿触摸 V 通道。 Add a constant to S, modulo 360deg (or mod 256, if S is stored as bytes) and multiply S by a constant chosen so that the coordinates in HSV corresponding to the selected color is moved to the HSV coordinates for the target color.向 S 添加一个常数,以 360deg 为模(或 mod 256,如果 S 存储为字节)并将 S 乘以选定的常数,以便将 HSV 中对应于所选颜色的坐标移动到目标颜色的 HSV 坐标。 Convert the transformed S and H, with the unchanged L, back to RGB.将变换后的 S 和 H(L 不变)转换回 RGB。

Finally, use the mask to blend the original image with the color-transformed one.最后,使用蒙版将原始图像与颜色转换后的图像混合。 Apply this to each channel - red, green, blue:将此应用于每个通道 - 红色、绿色、蓝色:

output = (1-mask)*original + mask*transformed

If you're doing it all in byte arrays, 0 is 0.0 and 255 is 1.0, and be careful of overflow and signed/unsigned problems.如果您在字节数组中进行所有操作,则 0 为 0.0,255 为 1.0,并注意溢出和有符号/无符号问题。

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

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