Skip to content
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

Closed
yeyupiaoling opened this issue Mar 8, 2018 · 5 comments
Closed

CAPI如加载图像数据 #8884

yeyupiaoling opened this issue Mar 8, 2018 · 5 comments
Assignees
Labels
User 用于标记用户问题

Comments

@yeyupiaoling
Copy link
Contributor

这创建了一个数组,这个是不是要预测的数据呢?

// Fill the matrix with a randomly generated test sample.
srand(time(0));
for (int i = 0; i < 784; ++i) {
array[i] = rand() / ((float)RAND_MAX);
}

如果是Python的话,是要把图像生成一个一维数组的

def load_image(file):
    im = Image.open(file).convert('L')
    im = im.resize((28, 28), Image.ANTIALIAS)
    im = np.array(im).astype(np.float32).flatten()
    im = im / 255.0 * 2.0 - 1.0
    return im

CPP又应该怎么处理呢?

@ranqiu92 ranqiu92 added the User 用于标记用户问题 label Mar 8, 2018
@wangkuiyi
Copy link
Collaborator

这个问题和 #8885 相关。也请 @Xreki @kexinzhao 看看吧。

@Xreki
Copy link
Contributor

Xreki commented Mar 9, 2018

可以借助于OpenCV将图像像素数据读出来。

@yeyupiaoling
Copy link
Contributor Author

yeyupiaoling commented Mar 17, 2018

@Xreki @kexinzhao
我这处理,使用Java代码把图像转成字节数组:

//把图像读取成一个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++;
        }
    }
}

这样程序是跑得同的,只是结果是不对的,应该是数据加载错了,不知道少了什么?

@yeyupiaoling
Copy link
Contributor Author

yeyupiaoling commented Mar 20, 2018

冒昧问一下,我使用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++;
            }
        }
    }

其中有很多的数据都是0.00000,这是有问题的吗?我预测好像不准确,您能为我解答吗?

我是在Android上把图像转成字节数组的,Java代码如下:

//把图像读取成一个Bitmap对象
Bitmap bitmap = BitmapFactory.decodeFile(img_path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
//把图像生成一个字节数组
byte[] pixels = baos.toByteArray();

@yeyupiaoling
Copy link
Contributor Author

上面的转换是没错的,不过有点要注意的是,一开始可以指定读取通道的顺序,如下:

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
Labels
User 用于标记用户问题
Projects
None yet
Development

No branches or pull requests

5 participants