Python Pandas 旋转图像数据 table

Python Pandas Pivot image data table

我有一个使用 Pandas 的数据框 我创建了一个 table,它是 3000000x3(像素 x 抢劫带),现在我试图旋转它,所以我有一个 table 这是3x3000000 然后 运行 针对新数据的 PCA。

我正在使用 pandas 枢轴函数,但无法弄清楚我做错了什么。

import pandas as pd 
import numpy as np 
import random as rd
from sklearn.decomposition import PCA
from sklearn import preprocessing
import matplotlib.pyplot as plt
import cv2 

#read in image
img = cv2.imread('/Volumes/EXTERNAL/Stitched-Photos-for-Chris/p7_0015_20161005-949am-75m-pass-1.jpg.png',1)
row,col = img.shape[:2]
#print(row , col)

#get a unique pixel ID for each pixel
pixel = [i for i in range(0,row*col)]
data = pd.DataFrame(columns=['bBand','gBand','rBand'],index = pixel)

#populate data for each band
b,g,r = cv2.split(img)
data.loc[pixel,'bBand'] = b.flat[:]
data.loc[pixel,'gBand'] = g.flat[:]
data.loc[pixel,'rBand'] = r.flat[:]

datapivoted = data.pivot(index=['bBand','gBand','rBand'], columns=pixel, values=[data.loc[pixel,'bBand'],data.loc[pixel,'bBand'],data.loc[pixel,'bBand']])

print(data.head())
print(data.shape)

更新

使用以下代码重新创建数据框,我认为它是正确且更有效的,但仍然无法转换

img = cv2.imread('/Volumes/EXTERNAL/Stitched-Photos-for-Chris/p7_0015_20161005-949am-75m-pass-1.jpg.png',1)
row,col = img.shape[:2]
#print(row , col)
b,g,r = cv2.split(img)

data = pd.DataFrame({'bBnad':b.flat[:],'gBnad':g.flat[:],'rBnad':r.flat[:]})

严重想多了这个问题。因为我需要完全交换轴,所以使用 .T

进行转置很简单
data = pd.DataFrame({'bBnad':b.flat[:],'gBnad':g.flat[:],'rBnad':r.flat[:]})
datapivoted = data.T
print(datapivoted.head())
print(datapivoted.shape)