美文网首页
opencv(NoneAPI100例---1)--红蓝颜色通道交

opencv(NoneAPI100例---1)--红蓝颜色通道交

作者: SeatonLv | 来源:发表于2021-12-06 13:37 被阅读0次

    Mat channel_swap(cv::Mat img) 

    {

        // get height and width

        int width = img.cols;

        int height = img.rows;

        // prepare output

        cv::Mat out = cv::Mat::zeros(height, width, CV_8UC3);

        // each y, x

        for (int y = 0; y < height; y++) 

        {

            for (int x = 0; x < width; x++) {

                // R -> B

                out.at<cv::Vec3b>(y, x)[0] = img.at<cv::Vec3b>(y, x)[2];

                // B -> R

                out.at<cv::Vec3b>(y, x)[2] = img.at<cv::Vec3b>(y, x)[0];

                // G -> G

                out.at<cv::Vec3b>(y, x)[1] = img.at<cv::Vec3b>(y, x)[1];

                }

        }

            return out;

    }

    cv::Mat img = cv::imread("D:/chat.png", cv::IMREAD_COLOR);

    cv::imshow("old", img);

    // channel swap

    cv::Mat out = channel_swap(img);

    //cv::imwrite("out.jpg", out);

    cv::imshow("sample", out);

    cv::waitKey(0);

    相关文章

      网友评论

          本文标题:opencv(NoneAPI100例---1)--红蓝颜色通道交

          本文链接:https://www.haomeiwen.com/subject/hewgxrtx.html