• 学习opencv-------函数使用二(图像变换)


      1 #include"cv.h"
      2 #include"highgui.h"
      3 using namespace cv;
      4 void CVFILTER2D(IplImage * img, IplImage *dst);
      5 void CVCOPYMAKEBORDER(IplImage *ori, IplImage *dst);
      6 void CVSOBEL(IplImage * img, IplImage *dst);
      7 void CVLAPLACE(IplImage *img, IplImage *dst);
      8 void CVCANNY(IplImage *img);
      9 void CVHOUGHCIRCLES(IplImage *img);
     10 void CVINVERT(IplImage *img);
     11 void CVCHANGE(IplImage *img);
     12 int main()
     13 {
     14     IplImage *fromimg = cvLoadImage("e:/picture/Wife2.jpg");
     15     IplImage *getimg = cvCreateImage(cvSize(400,600),fromimg->depth,fromimg->nChannels);
     16     cvResize(fromimg,getimg);
     17     //IplImage *dest = cvCreateImage(cvSize(getimg->width+1,getimg->height+1),getimg->depth,getimg->nChannels);//for cvCopyMakeBorder()
     18     IplImage *dest = cvCreateImage(cvSize(getimg->width, getimg->height), getimg->depth, getimg->nChannels);
     19     CVFILTER2D(getimg,dest);
     20     //CVCOPYMAKEBORDER(getimg,dest);
     21     //CVSOBEL(getimg,dest);
     22     //CVLAPLACE(getimg,dest);
     23     //CVCANNY(getimg);
     24     //CVHOUGHCIRCLES(getimg);
     25     //CVCHANGE(getimg);
     26     cvNamedWindow("convolution");
     27     cvNamedWindow("originpicture");
     28     cvShowImage("originpicture",getimg);
     29 
     30     cvShowImage("convolution",dest);
     31     cvWaitKey(0);
     32     cvReleaseImage(&getimg);
     33     cvReleaseImage(&dest);
     34     cvDestroyAllWindows();
     35 }
     36 void CVFILTER2D(IplImage * img ,IplImage *dst)
     37 {
     38     CvMat *mat = cvCreateMat(400,500,CV_32F);
     39     cvFilter2D(img, dst, mat);//cvPoint(1,1));
     40 }
     41 //卷积边界
     42 void CVCOPYMAKEBORDER(IplImage *ori, IplImage *dst)
     43 {
     44     cvCopyMakeBorder(ori,dst,cvPoint(1,1),IPL_BORDER_CONSTANT,cvScalarAll(255));
     45 }
     46 //梯度和Sobel导数
     47 void CVSOBEL(IplImage * img, IplImage *dst)
     48 {
     49     cvSobel(img,dst,0,1,3);//cvSobel(const CvArr* src,CvArr* dst,int xorder,int yorder,int aperture_size=3(1,3,5,7))
     50 }
     51 //拉普拉斯变换
     52 void CVLAPLACE(IplImage *img, IplImage *dst)
     53 {
     54     cvLaplace(img, dst);
     55 }
     56 //canny算子检测边界
     57 void CVCANNY(IplImage *img)
     58 {
     59     IplImage *grayimg = cvCreateImage(cvGetSize(img),img->depth,1);
     60     cvCvtColor(img,grayimg,CV_RGB2GRAY);
     61     cvCanny(grayimg,grayimg,100,100,3);
     62     cvShowImage("convolution", grayimg);
     63 }
     64 //霍夫圆变换
     65 void CVHOUGHCIRCLES(IplImage *img)
     66 {
     67     //IplImage *img = cvLoadImage(filename);
     68 
     69     IplImage *image = cvCreateImage(cvGetSize(img), img->depth, 1);
     70     cvCvtColor(img, image, CV_RGB2GRAY);
     71         CvMemStorage *storage = cvCreateMemStorage(0);
     72         cvSmooth(image,image,CV_GAUSSIAN,5,5);
     73         
     74         CvSeq *results = cvHoughCircles(
     75             image,
     76             storage,
     77             CV_HOUGH_GRADIENT,
     78             2,
     79             image->width / 10
     80             );
     81         for (int i = 0; i < results->total; i++)
     82         {
     83             float *p = (float *)cvGetSeqElem(results,i);
     84             CvPoint pt = cvPoint(cvRound(p[0]),cvRound(p[1]));
     85             cvCircle(image,pt,cvRound(p[2]),CV_RGB(0xff,0xff,0xff));
     86         }
     87         cvShowImage("convolution",image);
     88     
     89 }
     90 //图像颠倒位置
     91 void CVINVERT(IplImage *img)
     92 {
     93     //cvRectangle(img,cvPoint(220,160),cvPoint(280,220),cvScalar(200,100,100),5);
     94 
     95     cvSetImageROI(img,cvRect(220,160,60,60));
     96     cvFlip(img,img,-1);
     97     cvResetImageROI(img);
     98     //cvCvtColor(img,img,CV_RGB2BGR);
     99     //cvSmooth(img,img,CV_GAUSSIAN);
    100     //cvShowImage("convolution",img);
    101 }
    102 void CVCHANGE(IplImage *img)//仿射变换
    103 {
    104     //定义两个CvPoint2D32F的数组
    105     //第一个数组用来标定将要变换的原始图像中的区域
    106     //第二个数组用来标定变换后的图像在窗口中的位置
    107     CvPoint2D32f SrcTri[3], DstTri[3];
    108     //定义仿射映射矩阵,然后计算(2*3的矩阵)
    109     CvMat *warp_mat = cvCreateMat(2,3,CV_32FC1);
    110     CvMat *rot_mat = cvCreateMat(2, 3, CV_32FC1);
    111     IplImage *src, *dst;
    112     src = img;
    113     dst = cvCloneImage(src);
    114     SrcTri[0].x = 0; SrcTri[0].y = 0;
    115     SrcTri[1].x = src->width-1; SrcTri[1].y = 0;
    116     SrcTri[2].x = 0; SrcTri[2].y = src->height - 1;
    117     DstTri[0].x = 0; DstTri[0].y = src->height*0.33;
    118     DstTri[1].x = src->width*0.85; DstTri[1].y = src->height*0.25;
    119     DstTri[2].x = src->width*0.15; DstTri[2].y = src->height*0.7;
    120     //获取映射矩阵
    121     cvGetAffineTransform(SrcTri,DstTri,warp_mat);
    122     //映射变换
    123     cvWarpAffine(src,dst,warp_mat);
    124     cvCopy(dst,img);
    125     //cvShowImage("convolution",dst);
    126     //下面是对变换后的图像进一步旋转
    127     CvPoint2D32f center = cvPoint2D32f(src->width/2,src->height/2);
    128     double angle = 50.0;
    129     double scale = 0.1;
    130     //旋转图像
    131     //上面center是旋转中心
    132     //下面函数的第二个和第三个参数给出了旋转的角度和缩放的尺度
    133     //最后一个参数是输出的映射矩阵
    134     /*cv2DRotationMatrix(
    135     CvPoint2D32F center
    136     double angle
    137     double scale
    138     CvMat *map_matrix
    139     )*/
    140     cv2DRotationMatrix(center,angle,scale,rot_mat);
    141     cvWarpAffine(src,dst,rot_mat);
    142     cvShowImage("convolution",dst);
    143 }
    What I don't dare to say is I can't!
  • 相关阅读:
    python-configparser模块,xml.etree模块
    Ubuntu16.04环境下Vim 配置 for HTML,CSS,JAVASCRIPT(1)
    Windows 命令行及Git操作
    Ubuntu16.04 无任务栏问题
    ubuntu16.04安装中文输入法
    本地Web服务器搭建
    爬虫(1)
    Python(四):数字连珠2
    python学习(四)五数连珠
    Openjudge 百练第4109题
  • 原文地址:https://www.cnblogs.com/sytu/p/3983622.html
Copyright © 2020-2023  润新知