繁体   English   中英

Python - 如何将 2D 灰度图像转换为 1D 矢量

[英]Python - How can I convert a 2D grayscale image to a 1D vector

我正在学习 python 并正在尝试学习如何处理图像。 我想将 2D 灰度图像重新缩放(缩小)为 1D 向量(单行/列数组)。 在我的测试代码中,当我重新缩放图像时,数组中的 output 值采用十进制(浮点)格式。 但我想重新缩放并将一维数组中的值保留为整数。 有人可以帮助/指导我吗?

这是我的代码:

#Testing Image to vector

#Importing required functionality
import skimage.io as io
import numpy as np
from skimage.transform import rescale


#read image
image=io.imread("https://www.usna.edu/Users/cs/wcbrown/courses/F14IC210/lab/l09/cat.jpg")
#print image
print (image)

#rescale to 50%
small_im = rescale(image,0.5)
#print the rescaled image
print(small_im)

#manipulate the array
x=np.array(small_im)
#convert to 1D vector
y=np.concatenate(x)
print (y)


#print each value in the 1D vector in a new line. Just to see how far it would go
for i in y:
    print (i, end='\n')

我得到的 output 的片段是这样的(由于循环,它走得更远):

[[ 8  8  9 ... 12 11 11]
 [ 8  8  9 ... 12 11 11]
 [ 7  7  8 ... 12 11 11]
 ...
 [ 5  5  5 ... 98 97 96]
 [ 5  5  5 ... 98 97 97]
 [ 5  5  5 ... 99 98 97]]
[[0.02745098 0.02941176 0.02941176 ... 0.04509804 0.04313725 0.04313725]
 [0.0254902  0.0254902  0.0254902  ... 0.04509804 0.04313725 0.04313725]
 [0.0254902  0.0254902  0.0254902  ... 0.04509804 0.04313725 0.04313725]
 ...
 [0.01960784 0.01960784 0.01960784 ... 0.38039216 0.37843137 0.37647059]
 [0.01960784 0.01960784 0.01960784 ... 0.38039216 0.37843137 0.37647059]
 [0.01960784 0.01960784 0.01960784 ... 0.38039216 0.38039216 0.37843137]]
[0.02745098 0.02941176 0.02941176 ... 0.38039216 0.38039216 0.37843137]
0.027450980392156862
0.029411764705882575
0.029411764705882575
0.027450980392156862
0.03137254901960784
0.03529411764705882
0.03529411764705882
0.032352941176470695
0.03039215686274498
0.02941176470588213
0.030392156862744994
0.03431372549019597
0.03529411764705882
0.0392156862745098
0.0392156862745098
0.0392156862745098
0.0392156862745098
0.0392156862745098
0.043137254901960784

在尝试和谷歌搜索之后,我找到了答案。 至少,在我的上下文中,这是我想要实现的。

解决方案代码:

#solution to converting to 1D vector

#Importing required functionality
import numpy as np
from PIL import Image

#Opening Image and resizing to 10X10 for easy viewing
image_test = np.array(Image.open('1.png').resize((10,10)))  #note: I used a local image
#print image
print (image_test)

#manipulate the array
x=np.array(image_test)
#convert to 1D vector
y=np.concatenate(x)
print (y)


#print each value in the 1D vector in a new line. Just to see how far it would go
for i in y:
    print (i, end='\n')

所需样品 output (由于循环它走得更远):

[[ 48  52  72  96  96  99  81  71  68  47]
 [ 52  85 133 149 168 175 157 116  70  46]
 [ 54 129 170 174 185 179 177 169  92  42]
 [ 55 142 165 171 187 175 162 167  97  40]
 [112 150 144 134 172 157 128 143 129 113]
 [162 166 166 158 166 164 154 163 157 155]
 [105 166 185 174 170 165 175 179 140  81]
 [ 35 113 199 170 147 145 174 181  83  32]
 [ 46  65 179 183 160 153 166 155  71  37]
 [ 47  58 169 178 170 159 148 158  74  39]]
[ 48  52  72  96  96  99  81  71  68  47  52  85 133 149 168 175 157 116
  70  46  54 129 170 174 185 179 177 169  92  42  55 142 165 171 187 175
 162 167  97  40 112 150 144 134 172 157 128 143 129 113 162 166 166 158
 166 164 154 163 157 155 105 166 185 174 170 165 175 179 140  81  35 113
 199 170 147 145 174 181  83  32  46  65 179 183 160 153 166 155  71  37
  47  58 169 178 170 159 148 158  74  39]
48
52
72
96
96
99
81
71
68
47
52
85
133
149
168
175
157
116
70
46

暂无
暂无

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

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