通过映射缩小图像尺寸 [英] Reduce image dimension with mapping

查看:69
本文介绍了通过映射缩小图像尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有四种颜色的.png图像.如果将图像转换为numpy数组,则会得到具有以下尺寸的数组:[length X height X 3],带有length == height. 如何通过映射颜色来缩小尺寸?

I have a .png image with four colors in it. If I convert the image to a numpy array I get an array with the following dimensions: [length X height X 3], with length == height. How can I reduce the dimension with mapping the colors?

这是当前结构:

array([[[  0,  65, 101],
    [  0,  65, 101],
    [  0,  65, 101],
    ...,
    [  0,  65, 101],
    [  0,  65, 101],
    [  0,  65, 101]],

   [[  0,  65, 101],
    [163, 219, 232],
    [163, 219, 232],
    ...,
    [  0,  65, 101],
    [163, 219, 232],
    [  0,  65, 101]],

   [[  0,  65, 101],
    [163, 219, 232],
    [  0,  65, 101],
    ...,
    [  0,  65, 101],
    [163, 219, 232],
    [  0,  65, 101]],

   ...,

   [[  0,  65, 101],
    [163, 219, 232],
    [  0,  65, 101],
    ...,
    [  0,  65, 101],
    [  0,  65, 101],
    [  0,  65, 101]],

   [[  0,  65, 101],
    [163, 219, 232],
    [163, 219, 232],
    ...,
    [163, 219, 232],
    [163, 219, 232],
    [  0,  65, 101]],

   [[  0,  65, 101],
    [  0,  65, 101],
    [  0,  65, 101],
    ...,
    [  0,  65, 101],
    [  0,  65, 101],
    [  0,  65, 101]]], dtype=uint8)  

我想要一个具有二维的数组,并且i'th行和j'th列中的每个值都将对应于三维中的颜色.因此,如果原始图像的尺寸7 X 7 X 3具有四种颜色,则输出将如下所示:

And I would like an array with two dimensions, and every value in the i'th row and j'th column would correspond to the color it had in the third dimension. So if the original image had 7 X 7 X 3 dimension with four colors, the output would be something like this:

array([[0, 1, 1, 3, 3, 3, 0],
   [0, 2, 1, 1, 1, 1, 0],
   [0, 2, 0, 1, 2, 1, 0],
   [0, 3, 1, 1, 3, 1, 0],
   [0, 1, 0, 0, 3, 0, 0],
   [0, 1, 1, 1, 1, 1, 0],
   [0, 0, 0, 0, 0, 0, 0]])

前面提到的数组中的值都是伪造的,所以它们彼此不对应,我只是尝试表示这个概念.

The values in the forementioned arrays are all made up, so they don't correspond to each other, I have just tried to represent the concept.

我将图片读取为:

from PIL import Image
import numpy as np

img = Image.open('image.png')
imgarray = np.asarray(img)

print(imgarray)

推荐答案

您可以为此使用numpy.unique.例如,这是一个只有三种颜色的3x5图像:

You can use numpy.unique for this. For example, here's a 3x5 image that has just three colors:

In [105]: img
Out[105]: 
array([[[10, 20, 30],
        [ 5,  5,  0],
        [ 5,  5,  0],
        [ 5,  5,  0],
        [ 0,  0,  0]],

       [[ 5,  5,  0],
        [ 5,  5,  0],
        [ 0,  0,  0],
        [ 0,  0,  0],
        [ 0,  0,  0]],

       [[10, 20, 30],
        [10, 20, 30],
        [10, 20, 30],
        [10, 20, 30],
        [ 5,  5,  0]]])

在调整后的图像上调用numpy.unique.将前两个维展平为一个维,然后使用axis=0以便获得唯一的颜色. inv将保存逆"数组,即原始值colors的索引.

Call numpy.unique on the reshaped image. The first two dimensions are flattened into a single dimension, and then axis=0 is used so we get the unique colors. inv will holds the array of "inverses", i.e. the indices into colors of the original values.

In [106]: colors, inv = np.unique(img.reshape(-1, 3), axis=0, return_inverse=True)

In [107]: colors
Out[107]: 
array([[ 0,  0,  0],
       [ 5,  5,  0],
       [10, 20, 30]])

In [108]: inv
Out[108]: array([2, 1, 1, 1, 0, 1, 1, 0, 0, 0, 2, 2, 2, 2, 1])

重塑inv形状,使索引数组进入colors并具有与原始图像相同的形状:

Reshape inv to get the array of indices into colors with the same shape as the original image:

In [109]: inv.reshape(img.shape[:2])
Out[109]: 
array([[2, 1, 1, 1, 0],
       [1, 1, 0, 0, 0],
       [2, 2, 2, 2, 1]])

这篇关于通过映射缩小图像尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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