2013-09-23 62 views
1

我创建了一个在Android中调整亮度的示例图像。我使用位图来调整亮度,但运行需要很长时间。相反,我想使用OpenCV在Android中设置图像亮度。如何使用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) 

请查看我想要做的事情的例子here

+0

Imgproc.cvtColor(mRgba,mRay,Imgproc.CV_MEDIAN,4); //这很可能是错误的。你应该在这里提供Imgproc.COLOR_ *值,纯粹的机会Imgproc.CV_MEDIAN是3,它被解释为COLOR_BGRA2RGB。不是你想要的,对吧? – berak

+0

好的,这是我的错误,但这不是我的问题。我想设置亮度,我不需要改变颜色。请查看链接示例。 –

回答

1

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

请参见本

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; 
    } 
相关问题