OpenCV系列(11)|图像渐变色

2022-06-16 16:17:03 浏览数 (1)

效果:拉动zoomBar,可以使一张图像进行渐变色,视频也如此。

应用:广场上的字变色过程,图像魔术等。

代码:

代码语言:javascript复制
#include <opencv2/core/utility.hpp>
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

Mat img;
int threshval = 100;

static void on_trackbar(int, void*)
{
    Mat bw = threshval < 128 ? (img < threshval) : (img > threshval);
    Mat labelImage(img.size(), CV_32S);
    int nLabels = connectedComponents(bw, labelImage, 8);//0,255的bw图变成0,1,2不同区域表示
    std::vector<Vec3b> colors(nLabels);
    colors[0] = Vec3b(0, 0, 0);//background
    for(int label = 1; label < nLabels;   label){//label从1开始,因为0所在区域是背景
        colors[label] = Vec3b( (rand()&255), (rand()&255), (rand()&255) );//不同区域显示颜色
    }
    Mat dst(img.size(), CV_8UC3);
    for(int r = 0; r < dst.rows;   r){
        for(int c = 0; c < dst.cols;   c){
            int label = labelImage.at<int>(r, c);
            Vec3b &pixel = dst.at<Vec3b>(r, c);
            pixel = colors[label];//dst画板上涂色
         }
     }

    imshow( "Connected Components", dst );
}

int main( int argc, const char** argv )
{
    CommandLineParser parser(argc, argv, "{@image|stuff.jpg|image for converting to a grayscale}");
    parser.about("nThis program demonstrates connected components and use of the trackbarn");
    parser.printMessage();
    cout << "nThe image is converted to grayscale and displayed, another image has a trackbarn"
            "that controls thresholding and thereby the extracted contours which are drawn in colorn";

    String inputImage = parser.get<string>(0);
    img = imread(inputImage, IMREAD_GRAYSCALE);

    if(img.empty())
    {
        cout << "Could not read input image file: " << inputImage << endl;
        return EXIT_FAILURE;
    }

    imshow( "Image", img );

    namedWindow( "Connected Components", WINDOW_AUTOSIZE);
    createTrackbar( "Threshold", "Connected Components", &threshval, 255, on_trackbar );
    on_trackbar(threshval, 0);//先执行一次出图 0无意义装饰品

    waitKey(0);
    return EXIT_SUCCESS;
}

输入:

输出:

0 人点赞