With the effect of meanshift filter and canny edge detection, the cartoon effect of simple picture can be realized. Simply put, the result of subtracting the canny algorithm with the result of meanshift filter is to get the cartoon effect.
The code is as follows:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui//highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace Std;
using namespace CV;
int main ()
{
Cv::mat img = Cv::imread (".. /lenna.jpg ");
Cv::namedwindow ("image");
Cv::imshow ("Image", IMG);
Cv::mat IMG1;
IMG1 = Img.clone ();
Meanshift Filter
CV::p yrmeanshiftfiltering (Img1.clone (), IMG1, 10, 30);
Cv::namedwindow ("Image1");
Cv::imshow ("Image1", IMG1);
Cv::mat Img2;
Cv::mat IMG3;
Cv::mat Img4;
Canny
Cv::cvtcolor (IMG, IMG2, Cv_bgr2gray);
Cv::canny (Img2, IMG3, 150, 150);
Cv::cvtcolor (IMG3, IMG4, CV_GRAY2BGR);
Cv::namedwindow ("Image4");
Cv::imshow ("Image4", IMG4);
Cartoon pictures
IMG4 = IMG1-IMG4;
Cv::namedwindow ("Image4_1");
Cv::imshow ("Image4_1", IMG4);
Cv::waitkey (0);
}
The following are, respectively, the original image, Meanshift filter image, canny edge image, and the final cartoon image.
Let's look at the principle of the Meanshift filter algorithm.
In OpenCV, the Meanshift filter function is pyrmeanshiftfiltering, and its function invocation format is as follows:
-
C + +: void pyrmeanshiftfiltering (inputarray
src, outputarray
DST, double
sp, double
sr, int
maxlevel=1, Termcriteria
termcrit=termcriteria (TERMCR iteria::max_iter+termcriteria::eps,5,1) )
-
Parameters:
- src , haven source 8-bit, 3-channel image.
- DST , haven destination image of the same format and the same size as the source.
- SP , Haven Spatial window radius.
- SR , Haven Color window radius.
- Maxlevel –maximum level of the pyramid for the segmentation.
- Termcrit –termination criteria:when to stop meanshift iterations.//Stop Meanshift standard
The function implements the filtering stage of meanshift segmentation, that's, the output of the function is the filtered "posterized" image with color gradients and fine-grain texture flattened. At every pixel (x, y) of the input image (or down-sized input image, see below) The function executes meanshift it Erations, that is, the pixel (x, y) neighborhood in the joint Space-color hyperspace are considered:
where (r,g,b) and (r,g,b) is the vectors of color components at (x, y) and (x, y), resp Ectively (though, the algorithm does not depend in the color space used, so any 3-component color space can be used Instea d). Over the neighborhood the average spatial value (X ', Y ') and average color vector (R ', G ', B ') is Foun D and they act as the neighborhood center on the next iteration:
After the iterations over, the color components of the initial pixel (that's, the pixel from where the iterations started is set to the final value (average color in the last iteration):
When maxlevel > 0, the Gaussian pyramid of maxlevel+1 levels are built, and the above procedure is run On the smallest layer first. After that, the results is propagated to the larger layer and the iterations is run again only on those pixels where the Layer colors differ by + than SRfrom the lower-resolution layer of the pyramid. That makes boundaries of color regions sharper. Note that the results would be actually different from the ones obtained by running the Meanshift procedure on the whole or Iginal image (i.e. when maxlevel==0).
Meanshift Filter for a simple picture cartoon effect