• opencv中snake的调用方法示例


    网上流传的关于opencv中snake的用法代码,要么太旧,要么就是运行有问题,在这里我对其进行修改,可以在比较新的opencv2.3版本上运行。

    Snake Code
      1 // TrainingTools.cpp : 定义控制台应用程序的入口点。
      2 //
      3 
      4 #include <iostream>
      5 #include <string.h>
      6 #include <legacy/legacy.hpp>
      7 #include <cv.h>
      8 #include <highgui.h>
      9 #include <fstream>
     10  
     11  
     12 IplImage *image = 0 ; //原始图像
     13 IplImage *image2 = 0, *image3 = 0; //原始图像copy
     14  
     15 using namespace std;
     16 int Thresholdness = 100;
     17 int ialpha = 20;
     18 int ibeta=20; 
     19 int igamma=20; 
     20 int gdwStart = 0;
     21 CvPoint *point, *point2;
     22 int length;
     23 
     24 void onmouse(int event, int x, int y, int flags, void* param)
     25 {
     26     switch(event)
     27     {
     28     case CV_EVENT_MOUSEMOVE:
     29         
     30         break;
     31     case CV_EVENT_LBUTTONDOWN:
     32         point[length++] = cvPoint(x,y);
     33         printf("%d,%d;\n", x, y);
     34         break;
     35     case CV_EVENT_RBUTTONDOWN:
     36         length = 0;
     37         break;
     38     }
     39 }
     40  
     41 void onChange(int pos)
     42 {
     43 
     44     float alpha = ialpha / 100.0f;
     45     float beta = ibeta / 100.0f;
     46     float gamma = igamma / 100.0f;
     47  
     48     CvSize size;
     49     size.width=3; 
     50     size.height=3; 
     51     CvTermCriteria criteria; 
     52     criteria.type=CV_TERMCRIT_ITER; 
     53     criteria.max_iter=1000; 
     54     criteria.epsilon=0.1; 
     55     
     56     memcpy(point2, point, length * sizeof(CvPoint));
     57     
     58     cvSnakeImage(image, point2, length, &alpha, &beta, &gamma, CV_VALUE, size, criteria, 0);
     59 }
     60  
     61 int main(int argc, char* argv[])
     62 {
     63  
     64      if(image3) cvReleaseImage(&image3);
     65     if(image2) cvReleaseImage(&image2);
     66     if(image) cvReleaseImage(&image);
     67      
     68      char *pbyFN = "as004.bmp";
     69     image2 = cvLoadImage(pbyFN, 1); //显示图片
     70     image3 = cvLoadImage(pbyFN, 1);
     71     image = cvLoadImage(pbyFN, 0); 
     72     
     73     point = new CvPoint[1000]; //分配轮廓点
     74     point2 = new CvPoint[1000];
     75     
     76     cvShowImage("win0", image2);
     77     cvSetMouseCallback("win0", onmouse, 0);
     78 #if 1    
     79     while (1)
     80     {
     81          //显示轮廓曲线
     82          
     83         for(int i = 0;i < length - 1; i++)
     84         {
     85             cvLine(image2, point[i], point[i + 1], CV_RGB(0, 0, 255), 1, 8, 0); 
     86         }
     87         cvCopyImage(image2, image3);
     88         cvShowImage("win0", image3);
     89         uchar key = cvWaitKey(40);
     90         if (key == 'c') break;
     91 //        printf("-\n");
     92     }
     93 #endif    
     94     printf("point number:%d\n", length);
     95     
     96     if (length < 10)
     97     {
     98         printf("point number must be bigger than 10.\n");
     99         return 0;
    100     }
    101     
    102     
    103     
    104     cvNamedWindow("win1",0); 
    105     cvCreateTrackbar("Thd", "win1", &Thresholdness, 255, onChange);
    106     cvCreateTrackbar("alpha", "win1", &ialpha, 100, onChange);
    107     cvCreateTrackbar("beta", "win1", &ibeta, 100, onChange);
    108     cvCreateTrackbar("gamma", "win1", &igamma, 100, onChange);
    109     cvResizeWindow("win1",300,500);
    110     
    111     onChange(0);
    112      
    113     while (1)
    114      {
    115          //显示曲线
    116          cvCopyImage(image2, image3);
    117         for(int i = 0; i < length - 1; i++)
    118         {
    119             cvLine(image3, point2[i], point2[i + 1], CV_RGB(0, 255, 0), 1, 8, 0);
    120         }
    121         
    122         cvShowImage("win1",image3);
    123         uchar key = cvWaitKey(40);
    124         if (key == 'q') break;
    125     }
    126     
    127     delete []point;
    128     delete []point2;
    129  
    130     return 0;
    131 }

    本代码在linux下g++编译运行成功,为了不伤读者的脑细胞,给出相应makefile,

    make file
     1 .PHONY:all clean
     2 
     3 inc:=-I/usr/local/include/opencv/ -I/usr/local/include/opencv2/
     4 libpath:=-L/usr/local/lib/
     5 libs:=-lopencv_core -lopencv_highgui -lopencv_ml -lopencv_imgproc -lopencv_legacy -lm
     6 objs:=snake.o
     7 all:$(objs)
     8     g++ -o snake $^ $(libpath) $(libs)
     9     
    10 $(objs):%.o:%.cpp
    11     g++ -o $@ -c $< $(inc)
    12     
    13 clean:
    14     rm snake *.o
  • 相关阅读:
    页面切换主题风格,利用本地缓存
    http请求响应的组成部分的介绍 用cherome查看请求响应内容 curl命令行的使用
    Linux命令学习 ls cat mv touch
    git入门 关于git init,git add,git commit -v 的使用
    CentOS7使用RPM安装Package遇到 error: Failed dependencies,解决方案。
    CentOS7/Linux 使用本地光驱制作yum源并且永久保存
    CentOS7压缩目录及解压
    Linux硬盘分区挂载及swap分区扩容
    Linux系统新建用户用ssh远程登陆显示-bash-4.1$
    Linux系统编程-进程控制
  • 原文地址:https://www.cnblogs.com/starimpact/p/2730998.html
Copyright © 2020-2023  润新知