OpenCV simulating depth of field effect

Source: Internet
Author: User

Introduction
A photo scene called depth of field was found in HTC's camera app, and then practiced using OPENCV to simulate the implementation.

HCL depth of field scene effect
                                                   

This example achieves the effect
                                                   

Implementation process
To realize this example, two aspects are needed: 1. The Gaussian blur of the image. 2. In the image, go out to the circle area of interest.

Gaussian Blur
The OPENCV function is used: Cvsmooth. This function parameter specific use, see OPENCV official website of API introduction: http://docs.opencv.org/modules/refman.html
 #include <                                                                                                   Opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <math.h> #include <string.h> #include <opencv/ Cv.h> #include <stdio.h> using namespace CV; int width, Height;char pic_name[20]; Mat MAT1;        void Mysmooth (Mat Mat1, Mat mat2, int flag, int width, int height) {iplimage pI1 = MAT1;     Iplimage pI2 = MAT2; Cvsmooth (&PI1, &PI2, flag, width, height);}    int main (int argc,char *argv[]) {memcpy (pic_name,argv[1], sizeof (argv[1]));     MAT1 = Imread (pic_name, 1);    Imshow ("1", MAT1);                                                                                          Mysmooth (MAT1, Mat1,cv_gaussian, 23, 23);    Imshow ("2", MAT1);    Cv::waitkey (0); return 0;} 
The display results are as follows:                                

Circle area of Interest
In the ROI of OpenCV, the most commonly used methods of capturing areas of interest in images are as follows:
Cv::mat Imageroi;imageroi = img (cv::rect (40,40,40,40));
 The code indicates that in the image IMG's coordinates (40,40) position, take out a width:40,height:40 rectangle copied to Imageroi. But if we want to replicate the area that is taken out as a circle, we can't use this method. You need to use the following method: 
 
/************                                                                                                        * * Remove Circle area of interest **********/MAT1 = Imread (pic_name, 1);    src = mat1;    res = Cvcreateimage (cvgetsize (&SRC), 8, 3);     ROI = Cvcreateimage (cvgetsize (&SRC), 8, 1);    Cvzero (ROI);    Cvzero (RES);    Cvcircle (Roi,cvpoint (), 30,cv_rgb (255, 255, 255), 1, 8, 0);        Cvand (&SRC, &SRC, res, ROI); /******************************************/
First create a new two and the original image src the same size mat:res, ROI, then the res and ROI content are emptied, and then in the ROI of the coordinates (50,50) is a circle, 30 for the radius to draw a circle.  Then call the Cvand function, the ROI as a mask input, in the res copy obtained src corresponding to the ROI Circle area of the image content. The specific effects are shown below:                                       

This example code
The code used in this example is basically the knowledge, plus the mouse drag to control the image depth of field circle size and start position. The specific code is as follows:
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <math.h> #include <string.h> #include <op Encv/cv.h> #include <stdio.h> using namespace CV; int width, height;int pic_info[3];char pic_name[20]; Mat mat1;iplimage src, *res, *roi; void Mysmooth (Mat Mat1, Mat mat2, int flag, int width, int height) {iplimage pI1 = mat1;iplimage pI2 = mat2; Cvsmooth (& PI1, &AMP;PI2, flag, width, height);} void Mypic_merge (iplimage* src, iplimage* res, iplimage* DST) {cvscalar S;int height = src->height;int width = src->w Idth;int I, J; for (i=0;i
As above, can be implemented in the image window "1", through the mouse click and drag, control the depth of the circle changes.

OpenCV simulating depth of field effect

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.