Mastering OpenCV with practical Computer Vision Projects Learning notes-Cartoonifier

Source: Internet
Author: User
Tags int size

Recently in the process of learning OpenCV, found a very good book "Mastering OpenCV with practical Computer Vision". In this book, the author explains how to use OPENCV (c + + version) in Practical computer vision projects in a very understandable way. Unfortunately, there is no Chinese version of the book so far. So I'm going to write a series of blogs to share some of the insights I've read about this book.

e-book Connection for "Mastering OpenCV with practical Computer Vision Projects"

The book's Matching source code download address: Https://github.com/MasteringOpenCV/code - use OpenCV to the image of the cartoon (Cartoonifier)

As shown in the following figure, the basic idea of the cartoon operation of the image is: (1) to fill the flat part of the picture with a color or to make the flat part more smooth, (2) to draw thicker lines at the edge of the image or to make the edge part more uneven.

The basic steps are: Make median filter smoothing image (Median filter) using edge detection operator to detect image Edge (Laplacian edge Filter) using threshold method to treat edge image with binary value

Using the bilateral filter (bilateral filter) to cartoon the image, the bilateral filter can keep the edge information of the image well while smoothing the flat area of the image, but its speed is very slow. In the book, the author reduces the image, processes it, and then enlarges it to the original image.

Source:

void Cartoonifyimage (Mat srccolor, Mat DST) {//converted to grayscale images Mat Srcgray;

    Cvtcolor (Srccolor, Srcgray, Cv_bgr2gray);

    Using median filter to remove image noise Medianblur (Srcgray, Srcgray, 7);
    Size size = Srccolor.size ();
    Mat mask = Mat (size, cv_8u);
    Mat edges = Mat (size, cv_8u);

    Mat Edges2;
    ScHARR is a filter, which acts like an edge detection operator and does a differential operation ScHARR (Srcgray, edges, cv_8u, 1, 0);
    ScHARR (Srcgray, Edges2, cv_8u, 1, 0,-1);

    edges + = Edges2;
    Adaptive filtering threshold (edges, mask, 255, THRESH_BINARY_INV);

    Medianblur (Mask, mask, 3);
    Imshow ("edges", edges);

    Imshow ("Mask", mask);
    Reduce the size of the image to the original half, processing size smallsize;
    Smallsize.width = SIZE.WIDTH/2;
    Smallsize.height = SIZE.HEIGHT/2;
    Mat smallimg = Mat (smallsize, CV_8UC3);

    Resize (Srccolor, smallimg, Smallsize, 0,0, inter_linear);
    Several bilateral filters for the reduced image Mat tmp = Mat (smallsize, CV_8UC3);        int repetitions = 7;
    Repetitions for strong cartoon effect. for (int i=0; i<repetitions;           i++) {int size = 9; Filter size.
        Has a large effect on speed.  Double Sigmacolor = 9;
        Filter color strength.  Double sigmaspace = 7; Positional strength.
        Effects speed.
        Bilateralfilter (SMALLIMG, TMP, size, Sigmacolor, sigmaspace);
    Bilateralfilter (TMP, smallimg, size, Sigmacolor, sigmaspace);

    //The image after the bilateral filter is magnified to enlarge to the original size resize (smallimg, srccolor, size, 0,0, inter_linear);

    The data set for DST is all 0 memset ((char*) dst.data, 0, Dst.step * dst.rows);
Add processed pictures and mask pictures to DST images Srccolor.copyto (DST, mask); }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.