Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can save quite a bit. First, get rid of the vector. Then, outside the loop, create a Mat for the V channel only.

Then use extractChannel and insertChannel to access the channel you're using. It only accesses the one channel, instead of all three like split does.

The reason you put the Mat outside the loop is to avoid reallocating it every pass through the loop. Right now you're allocating and deallocating three Mats every pass.

You can save quite a bit. First, get rid of the vector. Then, outside the loop, create a Mat for the V channel only.

Then use extractChannel and insertChannel to access the channel you're using. It only accesses the one channel, instead of all three like split does.

The reason you put the Mat outside the loop is to avoid reallocating it every pass through the loop. Right now you're allocating and deallocating three Mats every pass.

test code:

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>

using namespace std;
using namespace cv;

int main()
{
    TickMeter tm;
    Ptr<CLAHE> clahe = createCLAHE();
    clahe->setClipLimit(4);

    vector<Mat> hsvChannels;
    Mat img, hsv1, hsv2, hsvChannels2, diff;
    img = imread("lena.jpg");

    cvtColor(img, hsv1, COLOR_BGR2HSV);
    cvtColor(img, hsv2, COLOR_BGR2HSV);

    tm.start();
    for (int i = 0; i < 1000; i++)
    {
        split(hsv2, hsvChannels);
        clahe->apply(hsvChannels[2], hsvChannels[2]);
        merge(hsvChannels, hsv2);
    }
    tm.stop();
    cout << tm << endl;

    tm.reset();
    tm.start();
    for (int i = 0; i < 1000; i++)
    {
        extractChannel(hsv1, hsvChannels2, 2);
        clahe->apply(hsvChannels2, hsvChannels2);
        insertChannel(hsvChannels2, hsv1, 2);
    }
    tm.stop();
    cout << tm;

    absdiff(hsv1, hsv2, diff);
    imshow("diff", diff*255);
    waitKey();
}