我正在尝试使用 OpenCV 读取和图像,在读取该图像后,我得到了一些数据,我必须使用 numpy 将这些数据保存在 CSV 文件中。这是程序: -
import cv2 as cv
import numpy as np
import os
img1 = cv.imread('C:/Users/sbans/Pictures/bird.jpg')
dataA1 = os.path.basename('C:/Users/sbans/Pictures/bird.jpg')
height, width, channels = img1.shape
dataA2 = height
dataA3 = width
dataA4 = channels
a = int(height/2)
b = int(width/2)
px1 = img1[a,b]
dataA5 = px1[0]
dataA6 = px1[1]
dataA7 = px1[2]
a = np.array([dataA1, dataA2, dataA3, dataA4, dataA5, dataA6, dataA7])
img2 = cv.imread('C:/Users/sbans/Pictures/cat.jpg')
dataB1 = os.path.basename('C:/Users/sbans/Pictures/cat.jpg')
height, width, channels = img2.shape
dataB2 = height
dataB3 = width
dataB4 = channels
a = int(height/2)
b = int(width/2)
px2 = img2[a,b]
dataB5 = px2[0]
dataB6 = px2[1]
dataB7 = px2[2]
b = np.array([dataB1, dataB2, dataB3, dataB4, dataB5, dataB6, dataB7])
np.savetxt("stats.csv", np.stack((a,b)), delimiter=",", fmt='%s')
这个错误来了: -
回溯(最近一次通话最后):
文件“C:\Users\sbans\Documents\demo_opencv.py”,第 32 行,在 np.savetxt("stats.csv", np.stack((a,b)), delimiter=",", fmt=' %s')
文件“< array_function internals>”,第 6 行,在堆栈中
文件“C:\Users\sbans\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\core\shape_base.py”,第 425 行,在堆栈中
raise ValueError('all input array must have the same shape') ValueError: all input array must have the same shape