将范围内的RGB像素值转换为连续数字

时间:2022-03-18 13:20:47

Guys I'm new to python and I'm stuck on this. Would really appreciate it if you could help me out.

伙计我是python的新手,我坚持这个。如果你能帮助我,真的很感激。

I've got an image that has a number of colours in each pixel. They all denote a number.

我有一个图像,每个像素都有多种颜色。它们都表示一个数字。

将范围内的RGB像素值转换为连续数字

The image was constructed using this range from 0 to 15625 pixels. Each of the pixels in the range from 0 to 15625 has a different colour and the above image was constructed using that.

使用0到15625像素的范围构建图像。 0到15625范围内的每个像素具有不同的颜色,并且使用它构造上述图像。

image range (it's massive so you might need to download it to see the image)

图像范围(它很大,所以你可能需要下载它才能看到图像)

What I'm trying to do is convert the RGB values from the range such as the first pixel value (5,5,5) to 1 and the next pixel value in the range to 2 and so on. Therefore, each pixel in the image above could correspond to a value.

我要做的是将RGB值从第一个像素值(5,5,5)的范围转换为1,将范围中的下一个像素值转换为2,依此类推。因此,上图中的每个像素可以对应一个值。

It's similar to this question but I don't think it does what I want to do. How to Convert all pixel values of an image to a certain range -python

它与这个问题类似,但我不认为它能做我想做的事情。如何将图像的所有像素值转换为特定范围-python

This is the code I used to create the range

这是我用来创建范围的代码

#!/usr/local/bin/python3
from PIL import Image
import numpy as np

# Create array to hold output image
result=np.zeros([1,25*25*25,3],dtype=np.uint8)

#change the values above i.e. 51*51*51 done by (upperbound-lowerbound)/step i.e (255-0)/5
j=0
for r in range(5,255,10):
    for g in range(5,255,10):
        for b in range(5,255,10):
            result[0,j]= (r,g,b)
            j+=1

# Convert output array to image and save
im=Image.fromarray(result)
im.save("perfect1.png")

This is the code to find the RGB values of each pixel in the range

这是用于查找范围中每个像素的RGB值的代码

from PIL import Image

i = Image.open('perfect1.png')
pixels = i.load() # this is not a list, nor is it list()'able
width, height = i.size

all_pixels = []
for x in range(width):
    for y in range(height):
        cpixel = pixels[x, y]
        all_pixels.append(cpixel)

print all_pixels

This is the code for creating a sub array with no extra pixel values as each "pixel" value in the image has a number of pixels enclosed. a = array of the image values

这是用于创建没有额外像素值的子阵列的代码,因为图像中的每个“像素”值都包含多个像素。 a =图像值的数组

rows_mask = np.insert(np.diff(a[:, 0]).astype(np.bool), 0, True)
columns_mask = np.insert(np.diff(a[0]).astype(np.bool), 0, True)
b = a[np.ix_(rows_mask, columns_mask)]

1 个解决方案

#1


0  

Here's some idea.

这是一些想法。

Let's load your images

让我们加载你的图像

import numpy as np
from scipy.misc import imread

img = imread("img.png")[..., :3]   # drop alpha channel
lut = imread("lut.png").squeeze()  # squeeze 1D first axis

print(img.shape)
print(lut.shape)

Which outputs

(589, 612, 3)
(15625, 3)

Now let's say we want to look up the first pixel in the image

现在让我们说我们要查找图像中的第一个像素

print(img[0, 0])
[245 245  95]

You can find all pixels in the look up table with the same value (axis=1 to compare row by row)

您可以在查找表中找到具有相同值的所有像素(轴= 1以逐行比较)

np.all(img[0, 0] == lut, axis=1)

Which gives you a mask for all the pixels which is True for matches and False otherwise.

这为您提供了所有像素的掩码,其中匹配为True,否则为False。

Now you can convert that to a list of indices (which in your case we can assume will have length 1) with np.where

现在,您可以使用np.where将其转换为索引列表(在您的情况下,我们可以假设其长度为1)

idx = np.where(np.all(img[0, 0] == lut, axis=1))

And, assuming each pixel, has a unique mapping you will get

而且,假设每个像素都有一个独特的映射,你会得到

(array([15609]),)

Now this method is really slow and inefficient, you'll have to repeat it for each pixel of the image. There's probably some way to speed it up but at the moment I'm not seeing it, let's see if anyone else has some better input.

现在这种方法非常慢且效率低,你必须为图像的每个像素重复它。可能有一些方法可以加快速度,但目前我还没有看到它,让我们看看是否有其他人有更好的输入。

#1


0  

Here's some idea.

这是一些想法。

Let's load your images

让我们加载你的图像

import numpy as np
from scipy.misc import imread

img = imread("img.png")[..., :3]   # drop alpha channel
lut = imread("lut.png").squeeze()  # squeeze 1D first axis

print(img.shape)
print(lut.shape)

Which outputs

(589, 612, 3)
(15625, 3)

Now let's say we want to look up the first pixel in the image

现在让我们说我们要查找图像中的第一个像素

print(img[0, 0])
[245 245  95]

You can find all pixels in the look up table with the same value (axis=1 to compare row by row)

您可以在查找表中找到具有相同值的所有像素(轴= 1以逐行比较)

np.all(img[0, 0] == lut, axis=1)

Which gives you a mask for all the pixels which is True for matches and False otherwise.

这为您提供了所有像素的掩码,其中匹配为True,否则为False。

Now you can convert that to a list of indices (which in your case we can assume will have length 1) with np.where

现在,您可以使用np.where将其转换为索引列表(在您的情况下,我们可以假设其长度为1)

idx = np.where(np.all(img[0, 0] == lut, axis=1))

And, assuming each pixel, has a unique mapping you will get

而且,假设每个像素都有一个独特的映射,你会得到

(array([15609]),)

Now this method is really slow and inefficient, you'll have to repeat it for each pixel of the image. There's probably some way to speed it up but at the moment I'm not seeing it, let's see if anyone else has some better input.

现在这种方法非常慢且效率低,你必须为图像的每个像素重复它。可能有一些方法可以加快速度,但目前我还没有看到它,让我们看看是否有其他人有更好的输入。