2

我创建了一个在 Android 中调整了亮度的示例图像。我使用Bitmap来调整亮度,但运行时间很长。相反,我想使用 OpenCV 在 Android 中设置图像亮度。

这是我的示例代码,但它只会改变图像的颜色:

Bitmap bmp = BitmapFactory.decodeResource(getResources(),
            R.drawable.a001);
int width = bmp.getWidth();
int height = bmp.getHeight();
Mat mRgba = new Mat(width, height, CvType.CV_8UC1);
Utils.bitmapToMat(bmp, mRgba);
Mat mRay = new Mat();
Imgproc.cvtColor(mRgba, mRay, Imgproc.COLOR_BGRA2RGB, 4);
Utils.matToBitmap(mRay, bmp);
mImageview_01.setImageBitmap(bmp);

[更新] 我尝试添加代码,但它错误

Bitmap bmp = BitmapFactory.decodeResource(getResources(),
            R.drawable.a001);
int width = bmp.getWidth();
int height = bmp.getHeight();
Mat mRgba = new Mat(width, height, CvType.CV_8UC1);
Utils.bitmapToMat(bmp, mRgba);
Mat mRay = new Mat();
Imgproc.cvtColor(mRgba, mRay, Imgproc.COLOR_BGRA2RGB, 4);
/*
* Use Adaptive Thresholding on the grayscaled Mats crop -> threshed Mat
* src, Mat dst, double maxValue, int adaptiveMethod, int thresholdType,
* int blockSize, double C
*/
Imgproc.adaptiveThreshold(threshed, threshed, 255,
            Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C, Imgproc.THRESH_BINARY_INV, 15, 8);
Utils.matToBitmap(mRay, bmp);
mImageview_01.setImageBitmap(bmp);

[错误]

CvException [org.opencv.core.CvException: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/thresh.cpp:796: error: (-215) src.type() == CV_8UC1 in function void cv::adaptiveThreshold(cv::InputArray, cv::OutputArray, double, int, int, int, double)

请查看我在这里尝试做的示例。

4

2 回答 2

1

有许多快速简便的方法可以调整位图亮度,例如ColorMatrixRenderScript。但是,如果您仍然考虑使用OpenCV这种方式:

private Bitmap changeBrightness(Bitmap bmp, int value){
    Mat src = new Mat(bmap.getHeight(),bmap.getWidth(), CvType.CV_8UC1);
    Utils.bitmapToMat(bmap,src);
    src.convertTo(src,-1,1,value);
    Bitmap result = Bitmap.createBitmap(src.cols(),src.rows(), Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(src,result);
    return result;
}
于 2021-06-08T12:39:15.930 回答
-3

检查在增加/减少图像亮度中给出的示例

请看这个

    int brightness;
 SeekBar seekBarBrightness=(SeekBar)findViewById(R.id.seekBar1);
 seekBarBrightness.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

     @Override
     public void onStopTrackingTouch(SeekBar arg0) {


     }

     @Override
     public void onStartTrackingTouch(SeekBar arg0) {


     }

     @Override
     public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {

       Bitmap newBitMap  =   doBrightness(bitMap,progress);

       imageView.setImageBitmap(newBitMap);

     }
    });




    public static Bitmap doBrightness(Bitmap src, int value)
    {
        // image size
        int width = src.getWidth();
        int height = src.getHeight();
        // create output bitmap
        Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
        // color information
        int A, R, G, B;
        int pixel;

        // scan through all pixels
        for (int x = 0; x < width; ++x)
            {
                for (int y = 0; y < height; ++y)
                    {
                        // get pixel color
                        pixel = src.getPixel(x, y);
                        A = Color.alpha(pixel);
                        R = Color.red(pixel);
                        G = Color.green(pixel);
                        B = Color.blue(pixel);

                        // increase/decrease each channel
                        R += value;
                        if (R > 255)
                            {
                                R = 255;
                            } else if (R < 0)
                            {
                                R = 0;
                            }

                        G += value;
                        if (G > 255)
                            {
                                G = 255;
                            } else if (G < 0)
                            {
                                G = 0;
                            }

                        B += value;
                        if (B > 255)
                            {
                                B = 255;
                            } else if (B < 0)
                            {
                                B = 0;
                            }

                        // apply new pixel color to output bitmap
                        bmOut.setPixel(x, y, Color.argb(A, R, G, B));
                    }
            }

        // return final image
        return bmOut;
    }
于 2014-02-06T08:57:19.723 回答