Qt做一個卡通攝像頭

在這篇文章的基礎上,做視覺卡通處理:Qt打開USB攝像頭,做圓檢測

先在painting.h中添加一個函數,

painting.h

private slots:
    void colorpainting(Mat image,Mat& result);

在painting.cpp中添加一下程序:
#include "painting.h"
#include "ui_painting.h"
#include <iostream>
#include <vector>
using namespace std;
using namespace cv;
VideoCapture capture(1);
painting::painting(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::painting)
{
    ui->setupUi(this);
    time_clock=new QTimer();
    time_clock->setInterval(10);
    time_clock->start();
    connect(time_clock,SIGNAL(timeout()),this,SLOT(colorpainting()));
}

painting::~painting()
{
    delete ui;
}

void painting::colorpainting()
{
    Mat srcImage;
    if(capture.isOpened())
    {
        capture>>srcImage;
        if(!srcImage.empty())
        {
            cvtColor(srcImage,srcImage,CV_BGR2RGB);
            Mat result;
            colorpainting(srcImage, result);
            img1=QImage((const unsigned char*)srcImage.data,srcImage.cols,srcImage.rows,QImage::Format_RGB888);   //Format_RGB888
            img2=QImage((const unsigned char*)result.data,result.cols,result.rows,QImage::Format_RGB888);
            ui->label->setPixmap(QPixmap::fromImage(img1));
            ui->label_2->setPixmap(QPixmap::fromImage(img2));
            ui->label->show();
            ui->label_2->show();
        }
    }
}
void painting::colorpainting(Mat image, Mat &result)
{
    QString str;
    str=ui->lineEdit->text();
    int repetitions=str.toInt();
    Mat bigImg,gray,masks,smallImg,tmp,edges;

    int MEDIAN_BLUR_FILTER_SIZE = 7;
    int LAPLACIAN_FILTER_SIZE = 5;
    int EDGES_THRESHOLD = 70;

    Size size = image.size();
    Size smallSize;
    smallSize.width = size.width / 2;
    smallSize.height = size.height / 2;
    smallImg = Mat(smallSize, CV_8UC3);
    tmp = Mat(smallSize, CV_8UC3);
    result=Mat(size,CV_8UC3);

    cvtColor(image,gray,CV_RGB2GRAY);
    medianBlur(gray,gray,MEDIAN_BLUR_FILTER_SIZE);
    Laplacian(gray,edges,CV_8U,LAPLACIAN_FILTER_SIZE);
    threshold(edges,masks,EDGES_THRESHOLD,255,THRESH_BINARY_INV);
    cv::resize(image, smallImg, smallSize, 0, 0, INTER_LINEAR);
    for (int i = 0; i < repetitions; i++)
    {
         int ksize = 9;
         double sigmaColor = 9;
         double sigmaSpace = 7;
         bilateralFilter(smallImg, tmp, ksize, sigmaColor, sigmaSpace);
         bilateralFilter(tmp, smallImg, ksize, sigmaColor, sigmaSpace);
    }
    cv::resize(smallImg, bigImg, size, 0, 0, INTER_LINEAR);
    result.setTo(0);
    bigImg.copyTo(result, masks);
    //MarrHildreth(result,result);

}

實現效果:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章