Below is C++ code using opencv and running it through the terminal. The code so
ID: 663526 • Letter: B
Question
Below is C++ code using opencv and running it through the terminal. The code so far brings up the original pic, Pic with edit, Pic with 2nd edit, then the points of the line. I have been looking for a way to implement the opencv CVSnakeImage within the code with the point printed out from the line.
//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>
using namespace std;
using namespace cv;
void myLine(Mat copy, Point pt1, Point pt2);
void Extract(Mat copy, Point pt1, Point pt2);
int main(){
//CODE TO LOADING IMAGE
Mat frame = imread ("First.png", CV_LOAD_IMAGE_UNCHANGED);
//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;
}
//Displaying image without filter (Orignal))
imshow ("Original", frame);
waitKey(0);
//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);
//Display the blurred image
imshow("Blurred Image", copy);
waitKey(0);
Point pt1, pt2;
pt1.x = 0;
pt1.y = 95;
pt2.x = 900;
pt2.y = 95;
//Calling Function (image, starting point, ending point)
//(X,Y)
// myLine(copy, Point(0, 95), Point (1000, 95));
//
// return 0;
Extract (copy, pt1, pt2);
imshow("After Line", copy);
waitKey(0);
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");
Point copypt1;
copypt1.x = pt1.x;
for (int i = pt1.x; i <=pt2.x; i++){
for (int j=pt1.y; j<=pt2.y ; j++){
copypt1.y = pt1.y;
myfile <<copypt1;
}
copypt1.x = copypt1.x +1;
cout<<" ";
}
myfile.close();
}
Explanation / Answer
#include "cvtest.h" class CV_SnakeTest : public CvTest { public: CV_SnakeTest(); protected: void run(int); }; #define SCAN 0 CV_SnakeTest::CV_SnakeTest(): CvTest( "snakes", "cvSnakeImage" ) { support_testing_modes = CvTS::CORRECTNESS_CHECK_MODE; } void CV_SnakeTest::run( int /*start_from*/ ) { int code = CvTS::OK; static const char* file_name[] = { "ring", "square" }; const int numfig_image = 1; const int numfig_grad = 1; FILE* file = 0; #ifndef _MAX_PATH #define _MAX_PATH 1024 #endif char abs_file_name[_MAX_PATH]; char rel_path[_MAX_PATH]; int i,j; /* source image */ IplImage* iplSrc = NULL; CvSize win; int length; float alpha,beta,gamma; CvTermCriteria criteria; long lErrors = 0; int progress = 0, test_case_count = numfig_image + numfig_grad; CvPoint* Pts = 0; CvPoint* resPts = 0; sprintf( rel_path, "%ssnakes/", ts->get_data_path() ); criteria.type = CV_TERMCRIT_ITER; win.height = win.width = 3; for( i = 0; i update_context( this, i, false ); /* create full name of bitmap file */ strcpy(tmp, rel_path); strcat(tmp, file_name[i]); strcpy( abs_file_name, tmp ); strcat( abs_file_name, ".bmp" ); /* read bitmap with 8u image */ iplSrc = cvLoadImage( abs_file_name, -1 ); if (!iplSrc) { ts->printf( CvTS::LOG, "can not load %s ", abs_file_name ); code = CvTS::FAIL_MISSING_TEST_DATA; goto _exit_; } /* init snake reading file with snake */ strcpy(tmp, rel_path); strcat(tmp, file_name[i]); strcpy( abs_file_name, tmp ); strcat( abs_file_name, ".txt" ); #if !SCAN file = fopen( abs_file_name, "r" ); #else file = fopen( abs_file_name, "r+" ); #endif if (!file) { ts->printf( CvTS::LOG, "can not load %s ", abs_file_name ); code = CvTS::FAIL_MISSING_TEST_DATA; goto _exit_; } /* read snake parameters */ fscanf(file, "%d", &length ); fscanf(file, "%f", &alpha ); fscanf(file, "%f", &beta ); fscanf(file, "%f", &gamma ); /* allocate memory for snakes */ Pts = (CvPoint*)cvAlloc( length * sizeof(Pts[0]) ); resPts = (CvPoint*)cvAlloc( length * sizeof(resPts[0]) ); /* get number of snake positions */ fscanf(file, "%d", &num_pos ); /* get number iterations between two positions */ fscanf(file, "%d", &criteria.max_iter ); /* read initial snake position */ for ( j = 0; jRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.