【发布时间】:2025-12-07 01:35:01
【问题描述】:
我正在学习 python,并且正在尝试学习如何处理图像。我想将 2D 灰度图像重新缩放(缩小)为 1D 向量(单行/列数组)。在我的测试代码中,当我重新缩放图像时,数组中的输出值是十进制(浮点)格式。但我想重新缩放并将一维数组中的值保留为整数。有人可以帮助/指导我吗?
这是我的代码:
#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')
我得到的输出的 sn-p 是这样的(由于循环,它走得更远):
[[ 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
【问题讨论】:
-
x的值是什么样的?
-
When I do print (x), it is [[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]]
标签: python arrays image vector rescale