opencv模拟景深效果

简介

  在htc的相机应用中发现了一个叫景深的拍照场景,然后练习着用opencv来模拟实现。


hcl景深场景效果

                              


本例实现效果

                                     


实现过程

  要实现本例,需要用到两方面知识:1、图像的高斯模糊。 2.在图像中去出感兴趣的圆形区域。


高斯模糊

  用的是opencv函数:cvSmooth。该函数参数具体使用,看opencv官网的api介绍吧:http://docs.opencv.org/modules/refman.html
 
  
#include                                                                                                    
#include 
#include 
#include 
#include 
#include 
 
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;
}
  显示效果如下:
                                


圆形兴趣区域

  在opencv的ROI中,最常使用的在图片中截取出感兴趣区域方式如下:
 
  
cv::Mat imageROI;
imageROI = img(cv::Rect(40,40,40,40));
  该代码表示,在图像img的坐标(40,40)位置,取出一个width:40,height:40的矩形复制到imageROI中。
但是如果我们想复制取出的区域为圆形的话,就不能用这个办法。需要使用如下方式:
 
  
/**************取出圆形感兴趣区域**********/
    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(50, 50), 30,CV_RGB(255, 255, 255),-1, 8, 0);
    cvAnd(&src, &src, res, roi);    
    /******************************************/
  首先新建两个和原图像src相同大小的Mat:res、roi,然后将res和roi内容都清空,接着在roi中坐标(50,50)为圆形,30为半径画一个圆。
接着调用cvAnd函数中,将roi作为掩码输入,就在res中复制得到了src对应在roi画圆区域的图像内容。
  具体的效果演示如下:
                                       


本例代码

  本例使用的代码主要就是如上知识,另外还有鼠标拖动来控制图像景深圆圈的大小和开始位置。
具体代码如下:
 
  
#include                                                                                                    
#include 
#include 
#include 
#include 
#include 
 
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, &pI2, flag, width, height);
}
 
void mypic_merge(IplImage* src, IplImage* res, IplImage* dst){
	CvScalar s;
	int height = src->height;
	int width = src->width;
	int i, j;
 
	for(i=0;i
  如上,就能实现在图像窗口"1"中,通过鼠标点击和拖拽,控制景深圆圈变化。

你可能感兴趣的:(opencv,图形图像)