Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Oh, I see, I didn't check the type and mis-understood what you were trying to do.

Ok, you are doing the same set of operations to each pixel, so you should instead do that to the entire Mat.

You can replace all of this with a few lines of code.

 for (;;) 
{
    Mat frame;
    cap >> frame;

    Mat src = frame.clone();

    vector<Mat> spl;
    split(src, spl);

    Mat tempBGR[3];
    Mat L, M, S;
    Mat LMSShift[3];

    //Converts to float matrices and divides by 255
    spl[0].convertTo(tempBGR[0], CV_32F, 1.0 / 255.0);
    spl[1].convertTo(tempBGR[1], CV_32F, 1.0 / 255.0);
    spl[2].convertTo(tempBGR[2], CV_32F, 1.0 / 255.0);


    //L = 17.8824 * tempBGR[2] + 43.5161 * tempBGR[1] + 4.1193 * tempBGR[0];
    //M = 3.45570 * tempBGR[2] + 27.1554 * tempBGR[1] + 3.8671 * tempBGR[0];
    //S = 0.02996 * tempBGR[2] + 0.18431 * tempBGR[1] + 1.4670 * tempBGR[0];
    //L
    addWeighted(tempBGR[0], 4.1193, tempBGR[1], 43.5161, 0, L);
    addWeighted(L, 1, tempBGR[2], 17.8824, 0, L);
    //M
    addWeighted(tempBGR[0], 3.8671, tempBGR[1], 27.1554, 0, M);
    addWeighted(M, 1, tempBGR[2], 3.45570, 0, M);
    //S (not used here)
    addWeighted(tempBGR[0], 1.4670, tempBGR[1], 0.18431, 0, S);
    addWeighted(M, 1, tempBGR[2], 0.02996, 0, S);

    // LMS to L'M'S' {Tritanopia}
    //LShift = 1 * L + 0 * M + 0 * S;
    //MShift = 0 * L + 1 * M + 0 * S;
    //SShift = -0.395913 * L + 0.801109 * M + 0 * S;
    LMSShift[0] = L;
    LMSShift[1] = M;
    addWeighted(L, 0.395913, M, 0.801109, 0, LMSShift[2]);

    merge(LMSShift, 3, src);
    src.convertTo(src, CV_8UC3);

    imshow("conversion", src);

    if (waitKey(30) >= 0) break;
}

Does this make sense to you? Why it's done this way instead of looping over every pixel?

Oh, I see, I didn't check the type and mis-understood what you were trying to do.

Ok, you are doing the same set of operations to each pixel, so you should instead do that to the entire Mat.

You can replace all of this with a few lines of code.

 for (;;) 
{
    Mat frame;
    cap >> frame;

    Mat src = frame.clone();

    vector<Mat> spl;
    split(src, spl);

    Mat tempBGR[3];
    Mat L, M, S;
    Mat LMSShift[3];

    //Converts to float matrices and divides by 255
    spl[0].convertTo(tempBGR[0], CV_32F, 1.0 / 255.0);
    spl[1].convertTo(tempBGR[1], CV_32F, 1.0 / 255.0);
    spl[2].convertTo(tempBGR[2], CV_32F, 1.0 / 255.0);


    //L = 17.8824 * tempBGR[2] + 43.5161 * tempBGR[1] + 4.1193 * tempBGR[0];
    //M = 3.45570 * tempBGR[2] + 27.1554 * tempBGR[1] + 3.8671 * tempBGR[0];
    //S = 0.02996 * tempBGR[2] + 0.18431 * tempBGR[1] + 1.4670 * tempBGR[0];
    //L
    addWeighted(tempBGR[0], 4.1193, tempBGR[1], 43.5161, 0, L);
    addWeighted(L, 1, tempBGR[2], 17.8824, 0, L);
    //M
    addWeighted(tempBGR[0], 3.8671, tempBGR[1], 27.1554, 0, M);
    addWeighted(M, 1, tempBGR[2], 3.45570, 0, M);
    //S (not used here)
    addWeighted(tempBGR[0], 1.4670, tempBGR[1], 0.18431, 0, S);
    addWeighted(M, addWeighted(S, 1, tempBGR[2], 0.02996, 0, S);

    // LMS to L'M'S' {Tritanopia}
    //LShift = 1 * L + 0 * M + 0 * S;
    //MShift = 0 * L + 1 * M + 0 * S;
    //SShift = -0.395913 * L + 0.801109 * M + 0 * S;
    LMSShift[0] = L;
    LMSShift[1] = M;
    addWeighted(L, 0.395913, -0.395913, M, 0.801109, 0, LMSShift[2]);

    merge(LMSShift, // l'M'S' to BGR
    //tempBGR[2] = 0.080944942 * LShift - 0.130505254 * MShift + 0.116728267 * SShift;
    //tempBGR[1] = -0.010248719 * LShift + 0.05401967 * MShift - 0.11362094 * SShift;
    //tempBGR[0] = -0.000365487 * LShift - 0.004121628 * MShift + 0.693554394 * SShift;
    //tempBGR[0]
    addWeighted(LMSShift[0], 0.080944942, LMSShift[1], -0.130505254, 0, tempBGR[0]);
    addWeighted(tempBGR[0], 1, LMSShift[2], 0.116728267, 0, tempBGR[0]);
    //tempBGR[1]
    addWeighted(LMSShift[0], -0.010248719, LMSShift[1], 0.05401967, 0, tempBGR[1]);
    addWeighted(tempBGR[1], 1, LMSShift[2], -0.11362094, 0, tempBGR[1]);
    //tempBGR[2]
    addWeighted(LMSShift[0], -0.000365487, LMSShift[1], -0.004121628, 0, tempBGR[2]);
    addWeighted(tempBGR[2], 1, LMSShift[2], 0.693554394, 0, tempBGR[2]);

    merge(tempBGR, 3, src);
    src.convertTo(src, CV_8UC3);
image);
    image.convertTo(image, CV_8UC3, 255);

    imshow("conversion", src);

    if (waitKey(30) >= 0) break;
}

Does this make sense to you? Why it's done this way instead of looping over every pixel?

Oh, I see, I didn't check the type and mis-understood what you were trying to do.

Ok, you are doing the same set of operations to each pixel, so you should instead do that to the entire Mat.

You can replace all of this with a few lines of code.

 for (;;) 
{
    Mat frame;
    cap >> frame;

    Mat src = frame.clone();

    vector<Mat> spl;
    split(src, spl);

    Mat tempBGR[3];
    Mat L, M, S;
    Mat LMSShift[3];

    //Converts to float matrices and divides by 255
    spl[0].convertTo(tempBGR[0], CV_32F, 1.0 / 255.0);
    spl[1].convertTo(tempBGR[1], CV_32F, 1.0 / 255.0);
    spl[2].convertTo(tempBGR[2], CV_32F, 1.0 / 255.0);


    //L = 17.8824 * tempBGR[2] + 43.5161 * tempBGR[1] + 4.1193 * tempBGR[0];
    //M = 3.45570 * tempBGR[2] + 27.1554 * tempBGR[1] + 3.8671 * tempBGR[0];
    //S = 0.02996 * tempBGR[2] + 0.18431 * tempBGR[1] + 1.4670 * tempBGR[0];
    //L
    addWeighted(tempBGR[0], 4.1193, tempBGR[1], 43.5161, 0, L);
    addWeighted(L, 1, tempBGR[2], 17.8824, 0, L);
    //M
    addWeighted(tempBGR[0], 3.8671, tempBGR[1], 27.1554, 0, M);
    addWeighted(M, 1, tempBGR[2], 3.45570, 0, M);
    //S (not used here)
    addWeighted(tempBGR[0], 1.4670, tempBGR[1], 0.18431, 0, S);
    addWeighted(S, 1, tempBGR[2], 0.02996, 0, S);

    // LMS to L'M'S' {Tritanopia}
    //LShift = 1 * L + 0 * M + 0 * S;
    //MShift = 0 * L + 1 * M + 0 * S;
    //SShift = -0.395913 * L + 0.801109 * M + 0 * S;
    LMSShift[0] = L;
    LMSShift[1] = M;
    addWeighted(L, -0.395913, M, 0.801109, 0, LMSShift[2]);

    // l'M'S' to BGR
    //tempBGR[2] = 0.080944942 * LShift - 0.130505254 * MShift + 0.116728267 * SShift;
    //tempBGR[1] = -0.010248719 * LShift + 0.05401967 * MShift - 0.11362094 * SShift;
    //tempBGR[0] = -0.000365487 * LShift - 0.004121628 * MShift + 0.693554394 * SShift;
    //tempBGR[0]
    addWeighted(LMSShift[0], 0.080944942, LMSShift[1], -0.130505254, 0, tempBGR[0]);
    addWeighted(tempBGR[0], 1, LMSShift[2], 0.116728267, 0, tempBGR[0]);
    //tempBGR[1]
    addWeighted(LMSShift[0], -0.010248719, LMSShift[1], 0.05401967, 0, tempBGR[1]);
    addWeighted(tempBGR[1], 1, LMSShift[2], -0.11362094, 0, tempBGR[1]);
    //tempBGR[2]
    addWeighted(LMSShift[0], -0.000365487, LMSShift[1], -0.004121628, 0, tempBGR[2]);
    addWeighted(tempBGR[2], 1, LMSShift[2], 0.693554394, 0, tempBGR[2]);

    merge(tempBGR, 3, image);
    image.convertTo(image, src);
    src.convertTo(src, CV_8UC3, 255);

    imshow("conversion", src);

    if (waitKey(30) >= 0) break;
}

Does this make sense to you? Why it's done this way instead of looping over every pixel?