-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CAPI如加载图像数据 #8884
Labels
User
用于标记用户问题
Comments
这个问题和 #8885 相关。也请 @Xreki @kexinzhao 看看吧。 |
可以借助于OpenCV将图像像素数据读出来。 |
@Xreki @kexinzhao //把图像读取成一个Bitmap对象
Bitmap bitmap = BitmapFactory.decodeFile(img_path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
//把图像生成一个字节数组
byte[] pixels = baos.toByteArray(); 在C++中,把字节数组转换成float数组: //获取字节数组转换成浮点数组
unsigned char *pixels =
(unsigned char *) env->GetByteArrayElements(jpixels, 0);
size_t index = 0;
std::vector<float> means;
means.clear();
for (size_t i = 0; i < 3; ++i) {
means.push_back(0.0f);
}
for (size_t c = 0; c < 3; ++c) {
for (size_t h = 0; h < 32; ++h) {
for (size_t w = 0; w < 32; ++w) {
array[index] =
static_cast<float>(
pixels[(h * 32 + w) * 3 + c]) - means[c];
index++;
}
}
} 这样程序是跑得同的,只是结果是不对的,应该是数据加载错了,不知道少了什么? |
冒昧问一下,我使用Java把图像读取成了字节数组,然后想使用C++把字节数组转成float数组,使用的C++代码如下: //获取字节数组转换成浮点数组
unsigned char *pixels =
(unsigned char *) env->GetByteArrayElements(jpixels, 0);
size_t index = 0;
std::vector<float> means;
means.clear();
for (size_t i = 0; i < 3; ++i) {
means.push_back(0.0f);
}
for (size_t c = 0; c < 3; ++c) {
for (size_t h = 0; h < 32; ++h) {
for (size_t w = 0; w < 32; ++w) {
array[index] =
(static_cast<float>(
pixels[(h * 32 + w) * 3 + c]) - means[c])/255;
LOGI("array1:%f", array[index]);
index++;
}
}
} 其中有很多的数据都是 我是在Android上把图像转成字节数组的,Java代码如下: //把图像读取成一个Bitmap对象
Bitmap bitmap = BitmapFactory.decodeFile(img_path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
//把图像生成一个字节数组
byte[] pixels = baos.toByteArray(); |
Closed
Closed
上面的转换是没错的,不过有点要注意的是,一开始可以指定读取通道的顺序,如下: Bitmap mBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); 因为CIFAR训练是的通道是BGR的,所以要转一下: public byte[] getPixelsBGR(Bitmap bitmap) {
int bytes = bitmap.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
bitmap.copyPixelsToBuffer(buffer);
byte[] temp = buffer.array();
byte[] pixels = new byte[(temp.length/4) * 3];
for (int i = 0; i < temp.length/4; i++) {
pixels[i * 3] = temp[i * 4 + 2]; //B
pixels[i * 3 + 1] = temp[i * 4 + 1]; //G
pixels[i * 3 + 2] = temp[i * 4 ]; //R
}
return pixels;
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
这创建了一个数组,这个是不是要预测的数据呢?
Paddle/paddle/capi/examples/model_inference/dense/main.c
Lines 70 to 74 in ffda2c4
如果是Python的话,是要把图像生成一个一维数组的
CPP又应该怎么处理呢?
The text was updated successfully, but these errors were encountered: