霓虹灯效果

源码:

void neon(Mat& src,Mat& dst)
{
    for (int i = 1; i < src.rows - 1; i++) {
        for (int j = 1; j < src.cols - 1; j++){
            int r1, r2, r3, g1, g2, g3, b1, b2, b3;

            r1 = src.at(i, j)[2];
            r2 = src.at(i + 1, j)[2];
            r3 = src.at(i, j + 1)[2];

            g1 = src.at(i, j)[1];
            g2 = src.at(i + 1, j)[1];
            g3 = src.at(i, j + 1)[1];

            b1 = src.at(i, j)[0];
            b2 = src.at(i, j + 1)[0];
            b3 = src.at(i, j + 1)[0];

            double R1, R2, G1, G2, B1, B2;
            R1 = pow(r1 - r2, 2);
            R2 = pow(r1 - r3, 2);
            G1 = pow(g1 - g2, 2);
            G2 = pow(g1 - g3, 2);
            B1 = pow(b1 - b2, 2);
            B2 = pow(b1 - b3, 2);

            double Red, Green, Blue;
            
            //灰度
            //Red = 4 * sqrt(R1 + R2) + (r1 + b1 + g1) / 3;
            //Green = 4 * sqrt(G1 + G2) + (r1 + b1 + g1) / 3;
            //Blue = 4 * sqrt(B1 + B2) + (r1 + b1 + g1) / 3;

            Red = 4 * sqrt(R1 + R2) + r1;
            Green = 4 * sqrt(G1 + G2) + g1;
            Blue = 4 * sqrt(B1 + B2) + b1;

            Red = Red > 255 ? 255 : Red;
            Green = Green > 255 ? 255 : Green;
            Blue = Blue > 255 ? 255 : Blue;

            Red = Red < 0 ? 0 : Red;
            Green = Green < 0 ? 0 : Green;
            Blue = Blue < 0 ? 0 : Blue;

            dst.at(i, j)[2] = (int)Red;
            dst.at(i, j)[1] = (int)Green;
            dst.at(i, j)[0] = (int)Blue;
        }
    }
}

效果图:

霓虹灯效果_第1张图片 

灰度图上加霓虹灯效果 ,这个就比较明显了

霓虹灯效果_第2张图片

你可能感兴趣的:(OpenCV,图像视频处理,计算机视觉)