Ask Your Question
0

How to implement CLAHE algorithm in android using opencv 3? [closed]

asked 2016-04-14 02:03:00 -0600

Hardik Patel gravatar image

updated 2016-04-15 02:00:55 -0600

hello, i am new in opencv, i trying to convert grayscale bitmap to CLAHE but i can't. i use this code

ImageView img;

Bitmap original, grayscale, histogram, resize, threshold;

int fixedwidth = 480; int fixedheight = 800; Button btn_original, btn_grayscale, btn_histogram, btn_resize, btn_threshold; Mat rgbMat; Mat grayMat; Mat CLAHEmat;

static { System.loadLibrary("opencv_java3"); }

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

img = (ImageView) findViewById(R.id.imageView);

btn_original = (Button) findViewById(R.id.btn_original);
btn_grayscale = (Button) findViewById(R.id.btn_grayscale);
btn_histogram = (Button) findViewById(R.id.btn_histogram);
btn_resize = (Button) findViewById(R.id.btn_resize);
btn_threshold = (Button) findViewById(R.id.btn_threshold);

original = BitmapFactory.decodeResource(getResources(), R.drawable.sample);

btn_original.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        img.setImageBitmap(original);
    }
});

btn_grayscale.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        /*grayscale = getGrayscale(original);
        img.setImageBitmap(grayscale);*/
        grayscale = BitmapFactory.decodeResource(getResources(), R.drawable.sample);

        rgbMat = new Mat();
        Utils.bitmapToMat(grayscale, rgbMat);

        grayMat = new Mat(grayscale.getHeight(), grayscale.getWidth(),
                CvType.CV_8U, new Scalar(1));
        Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY, 1);
        Utils.matToBitmap(grayMat, grayscale);
        img.setImageBitmap(grayscale);
        Log.e("Width & Height:-", grayscale.getWidth() + "-" + grayscale.getHeight());
    }
});

btn_histogram.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //histogram=HistogramEqualization.histogramEqualization(resize);

        int lnth = grayscale.getByteCount();

        ByteBuffer dst = ByteBuffer.allocate(lnth);
        grayscale.copyPixelsToBuffer(dst);
        byte[] byteArray = dst.array();

        Log.e("byte array", byteArray + "");
        Mat orgImage = new Mat();
        orgImage.put(0, 0, byteArray);

        Mat labImage = new Mat(grayscale.getHeight(), grayscale.getWidth(), CvType.CV_8UC1);
        Imgproc.cvtColor(orgImage, labImage, Imgproc.COLOR_BGR2Lab);

        CLAHE clahe = Imgproc.createCLAHE();
        CLAHEmat = new Mat(grayscale.getHeight(), grayscale.getWidth(), CvType.CV_8UC1);
        clahe.apply(labImage, CLAHEmat);

        Utils.matToBitmap(CLAHEmat, histogram);
        img.setImageBitmap(histogram);
    }
});

btn_threshold.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.sheet);

        Mat rgbMat = new Mat();
        Utils.bitmapToMat(bmp, rgbMat);

        Mat grayMat = new Mat(histogram.getHeight(), histogram.getWidth(),
                CvType.CV_8U, new Scalar(1));
        Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY, 1);
        Mat bwMat = new Mat();

        Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY);
        Imgproc.equalizeHist(grayMat, grayMat);

        Imgproc.threshold(grayMat, bwMat, 127.5, 255.0, Imgproc.THRESH_OTSU);
        Utils.matToBitmap(bwMat, bmp);
        Imgproc.createCLAHE();
        img.setImageBitmap(bmp);
    }
});

}}

but i got this error

CvException [org.opencv.core.CvException: cv::Exception: /builds/master_pack-android/opencv/modules/imgproc/src/clahe.cpp:354: error: (-215) _src.type() == CV_8UC1 || _src.type() == CV_16UC1 in function virtual void {anonymous}::CLAHE_Impl::apply(cv::InputArray, cv::OutputArray)]

please help me..

thanks in advance

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Hardik Patel
close date 2016-06-15 03:19:02.959140

1 answer

Sort by ยป oldest newest most voted
2

answered 2016-04-14 02:16:04 -0600

berak gravatar image

updated 2016-04-14 02:50:42 -0600

your src image must be CV_8UC1 || CV_16UC1, a single channel grayscale image, but is not. please check grayscale.type() or grayscale.channels()

also, your dst image will have the same type (and size), not CV_8UC4, you should not try to pre-allocate it at all (and later get confused, because it's not like you expected), but just pass a new Mat() . really, it's simple as this:

Mat gray = Imgcodecs.imread("4621239.jpg", Imgcodecs.IMREAD_GRAYSCALE);
CLAHE clahe = Imgproc.createCLAHE();
Mat res = new Mat();
clahe.apply(gray,res);
Imgcodecs.imwrite("clahe.jpg", res);
edit flag offensive delete link more

Comments

can you please share example code so its good for understanding.. currently i used

int lnth=grayscale.getByteCount();

            ByteBuffer dst= ByteBuffer.allocate(lnth);
            grayscale.copyPixelsToBuffer( dst);
            byte[] byteArray=dst.array();

            Log.e("byte array",byteArray+"");
            Mat orgImage = new Mat();
            orgImage.put(0, 0, byteArray);

            Mat labImage = new Mat(grayscale.getHeight(), grayscale.getWidth(), CvType.CV_8UC1);
            Imgproc.cvtColor(orgImage, labImage, Imgproc.COLOR_BGR2Lab);

            CLAHE clahe = Imgproc.createCLAHE();
            CLAHEmat = new Mat(grayscale.getHeight(),grayscale.getWidth(), CvType.CV_8UC1);
            clahe.apply(labImage, CLAHEmat);
Hardik Patel gravatar imageHardik Patel ( 2016-04-14 02:42:06 -0600 )edit

i'm sure, your grayscale image is not grayscale. please check again.

(and then, 80% of the code you show, are unrelated to the CLAHE operation.)

berak gravatar imageberak ( 2016-04-14 02:53:24 -0600 )edit

hello,Sorry but bitmap to grayscale works perfectly but when try to apply CLAHE its shows error..please find full code here..http://answers.opencv.org/question/92747/how-to-use-clahe-in-android/

thanks

Hardik Patel gravatar imageHardik Patel ( 2016-04-14 07:59:38 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-04-14 02:03:00 -0600

Seen: 3,154 times

Last updated: Apr 15 '16