我想使用活动轮廓OpenCV功能 . 我一直在尝试不同的代码,但我无法弄清楚如何让蛇穿过图像 . 当我运行下面的代码时,我收到与我在图像上绘制的线相同的点 . 我如何检查蛇是否真的在运行 .
//USED TO OPEN IMAGE
#include "opencv2/highgui/highgui.hpp"
//USED TO GAUSSIAN FILTER
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/core/core.hpp"
#include <iostream>
#include <fstream>
#include "opencv2/legacy/legacy.hpp"
#include <cstdlib>
using namespace std;
using namespace cv;
void myLine(Mat copy, Point pt1, Point pt2);
void Extract(Mat copy, Point pt1, Point pt2);
void readFile(Mat copy);
void snake(Mat copy, CvPoint* pointsA);
//void ActiveSnake(Mat copy, Point pt1, Point pt2);
int main(){
//CODE TO LOADING IMAGE
Mat frame = imread ("First.png", CV_LOAD_IMAGE_GRAYSCALE);
//putText(frame,"First", Point (120, 330), 1, 4, CV_RGB(255, 0, 0), 3, 8, false);
int rows = frame.rows;
int cols = frame.cols;
//If the image wont appear
if (frame.empty())
{
cout<<"No Image!"<<endl;
return -1;
}
//Cloning
Mat copy = frame.clone();
//Gaussian Filter (Originial Image, Clone Image, KSize(MUST BE POSITIVE OR ODD OR ZERO width, height), sigmaX, sigmaY
GaussianBlur(frame, copy, Size(9, 9), 0, 0);
Point pt1, pt2;
pt1.x = 0;
pt1.y = 95;
pt2.x = 900;
pt2.y = 95;
Extract (copy, pt1, pt2);
readFile(copy);
return 0;
}
void myLine (Mat copy, Point start, Point end)
{
int thickness = 2;
int lineType = 8;
line (copy, start, end, Scalar(0,0,0), thickness, lineType);
}
void Extract (Mat copy, Point pt1, Point pt2)
{
myLine(copy, Point(pt1), Point (pt2));
ofstream myfile;
myfile.open ("Test1.txt");
int copypt1x;
copypt1x = pt1.x;
for (int i = pt1.x; i <=pt2.x; i++){
for ( int j = pt1.y; 1 <=pt2.y; j++)
{
int copypt1y = pt1.y;
myfile <<copypt1x<<"\t"<<copypt1y<<endl;
}
copypt1x= copypt1x +1;
cout<<"\n";
}
myfile.close();
}
void readFile(Mat copy)
{
ifstream myfile;
CvPoint pointsA [1000];
myfile.open("Test1.txt");
if (!myfile.is_open()){
exit(EXIT_FAILURE);
}
int i = 0;
myfile >> pointsA[i].x>>pointsA[i].y;
while (myfile.good()){
i++;
myfile >> pointsA[i].x>>pointsA[i].y;
}
snake(copy, pointsA);
}
void snake(Mat copy, CvPoint* pointsA)
{
//IplImage* image2;
//image2 = cvCloneImage(&(IplImage)copy);
IplImage* image2;
image2 = cvCreateImage(cvSize(copy.cols,copy.rows),8,3);
IplImage ipltemp=copy;
cvCopy(&ipltemp,image2);
float alpha = 0.5; // Weight of continuity energy
float beta = 0.5; // Weight of curvature energy
float gamma = 0.9; // Weight of image energy
CvSize size; // Size of neighborhood of every point used to search the minimumm have to be odd
size.width = 5;
size.height = 5;
CvTermCriteria criteria;
criteria.type = CV_TERMCRIT_ITER; // terminate processing after X iteration
criteria.max_iter = 10000;
criteria.epsilon = 0.1;
int cpt = 1000;
cvSnakeImage(image2, pointsA, cpt, &alpha, &beta, &gamma, CV_VALUE, size, criteria, 0);
}