将二值图像重塑为一维向量 Python

Reshaping Binary images into 1D vector Python

我正在从事图像处理项目,我是 Python 的初学者。任何帮助都是 appreciated.I 我正在尝试将 matlab 代码转换为 python.Below 是我的 matlab 代码-

file_name='play.png';
message=double(imread(file_name));
Mm=size(message,1);                         %Height
Nm=size(message,2);                         %Width
message_vector=round(reshape(message,1200,1)./256);
disp(message_vector)

Output of matlab code

下面是python代码

water = cv2.imread('sam.png')
gray_image = cv2.cvtColor(water, cv2.COLOR_BGR2GRAY)
file_name=im2double(gray_image)
a=np.shape(gray_image)
b=a[0]*a[1]#getting resolution
images_rs = gray_image.reshape([b, 1])#reshaping array into 1D vector
print(images_rs)

Output of python

我想要输出像我用 1 和 0 得到的 matlab 代码。我怎么能得到它?我在 python 中犯错的地方因为 python 中的答案与 Matlab 不同?

您在整形后没有规范化 python 中的值。将 images_rs = gray_image.reshape([b, 1]) 更新为 images_rs = gray_image.reshape([b, 1])/256. 应该可以满足您的需求。