gpt4 book ai didi

python - 如何在openCV中将灰度图像加载为numpy数组

转载 作者:太空宇宙 更新时间:2023-11-03 21:53:37 26 4
gpt4 key购买 nike

我将 96x96 像素图像作为 numpy 数组。问题是我不知道如何让 opencv 加载这个 ndarray 并检测人脸。

img = cv2.imread(X)

这条线不起作用。我得到这个错误

TypeError: expected string or Unicode object, numpy.ndarray found

X为输入图像数组(灰度)

最佳答案

好的,现在可以使用它了

img = X[k].reshape(96,96)

但是i opencv输出时显示黑色图像。根据要求,这是一段代码。

import numpy as np
import cv2, cv

import numpy as np
import pandas as pd
import pylab as pl
from skimage import transform
from numpy import ravel

import pylab as pl

from sklearn import linear_model, cross_validation
from sklearn.svm import SVR
from sklearn.decomposition import PCA
from sklearn.neural_network import BernoulliRBM
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor

df = pd.read_csv('/users/prabhubalakrishnan/Desktop/training.csv', header=0)

x = df['Image'][:5].values


face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

X = []

for k in xrange(len(x)):
img = np.fromstring(x[k], dtype = np.uint8, sep=' ', count=96*96)
X.append ( ravel(transform.resize (img.reshape(96,96) , (96,96))) )

'''
for k in xrange(len(X)):
pl.imshow(X[k].reshape(96,96),cmap=pl.cm.gray)
pl.show()
'''

X = np.array(X)
X = X.astype('uint8')

print 'X:', X.shape, X.dtype

pl.ion()


for k in xrange(len(X)):

img = X[k].reshape(96,96)

faces = face_cascade.detectMultiScale(img, 1.03, 5)

for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_color = img[y:y+h, x:x+w]

print 'Image',img
print 'Faces',faces
cv2.namedWindow("img", cv2.CV_WINDOW_AUTOSIZE)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

关于python - 如何在openCV中将灰度图像加载为numpy数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27708831/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com