如何从图像中获取像素并将其存储在数组中?

时间:2019-02-27 19:55:29

标签: java android android-imageview

我正在尝试在我的主要活动中实现一个功能,该功能获取由 TensorFlow图像分类相机应用程序捕获的图像的像素并将其存储到数组中。当按下“检测”按钮时,识别功能将使用这些像素。我在如何设置功能以及如何将图像尺寸设置为224X224方面遇到了一些问题。请帮忙!

请注意,INPUT_SIZE = 224

获取像素的功能:

function`public float [] getPixels(){

    bitmap=bitmap.createScaledBitmap(bitmap, INPUT_SIZE, INPUT_SIZE, false);
    myPhto.setImageBitmap(bitmap);
    if (bitmap == null) {
        return null;
    }

    //int width = bitmap.getWidth();
    //int height = bitmap.getHeight();

    // Get 28x28 pixel data from bitmap
    int[] pixels = new int[width * heigth];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

    float[] retPixels = new float[pixels.length];
    for (int i = 0; i < pixels.length; ++i) {

        int pix = pixels[i];
        int b = pix & 0xff;
        retPixels[i] = (float)((0xff - b)/255.0);
    }
    return retPixels;
}

` 这是单击按钮时的代码:

 Detect.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            float pixels[] = getPixels();//how to get pixel data of the taken image

            //init an empty string to fill with the classification output

            String text = "";
            //for each classifier in our array
            for (Classifier classifier : mClassifiers) {
                //perform classification on the image
                final Classification res = classifier.recognize(pixels);
                //if it can't classify, output a question mark
                if (res.getLabel() == null) {
                    text += classifier.name() + ": ?\n";

                }
            }
        }
    });
}

0 个答案:

没有答案