diff --git a/cpp_frep/CMakeLists.txt b/cpp_frep/CMakeLists.txt index 223013a50946bc276c84a91e7a5873fdd6152744..1938683442187f55182450ce5dedb2990c65de10 100755 --- a/cpp_frep/CMakeLists.txt +++ b/cpp_frep/CMakeLists.txt @@ -3,8 +3,9 @@ add_library(cpp_frep_lib frep_tree.cpp nelder_mead.h ) +find_package( OpenCV REQUIRED ) target_include_directories(cpp_frep_lib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) -target_link_libraries(cpp_frep_lib shared_settings Eigen3::Eigen) +target_link_libraries(cpp_frep_lib shared_settings Eigen3::Eigen ${OpenCV_LIBS}) target_compile_features(cpp_frep_lib PUBLIC cxx_std_17) add_executable(cpp_frep diff --git a/cpp_frep/frep.cpp b/cpp_frep/frep.cpp index e2ff1e766029a1bcdf8f8f55c14f02911b4ceab7..9d539fee851b4f208a93eb816d253f1d55057a5f 100755 --- a/cpp_frep/frep.cpp +++ b/cpp_frep/frep.cpp @@ -26,11 +26,11 @@ namespace cpp_frep { return *this; } - void setXY(scalar X_,scalar Y_){ X=X_; Y=Y_; } + virtual scalar eval(){ return 0.0f; } @@ -40,6 +40,9 @@ namespace cpp_frep { virtual void printName(){ std::cout<<"Frep"; } + virtual std::string getPrintedName(){ + return "Frep"; + } void setShape1(Frep* shape1_){ shape1=shape1_; @@ -54,7 +57,7 @@ namespace cpp_frep { // std::cout<<"deleted_"<<name<<" \n"; } - private: + // private: }; //..........................SHAPES..................................// @@ -81,6 +84,10 @@ namespace cpp_frep { std::cout<<name; } + virtual std::string getPrintedName(){ + return name; + } + }; @@ -158,6 +165,11 @@ namespace cpp_frep { std::cout<<")"; } + virtual std::string getPrintedName(){ + std::string frepName=name+"("+shape1->getPrintedName()+","+shape2->getPrintedName()+")" ; + return frepName; + } + }; class Add: public FrepOperators { @@ -349,6 +361,10 @@ namespace cpp_frep { shape->printName(); std::cout<<")"; } + virtual std::string getPrintedName(){ + std::string frepName=name+"("+shape->getPrintedName()+")" ; + return frepName; + } diff --git a/cpp_frep/frep_tree.cpp b/cpp_frep/frep_tree.cpp index 987e0b5706080bdd46e7ac81eba71949efb2fd31..cb5f50e8c5075423dde68b6f70f31aea9ee0d4a3 100644 --- a/cpp_frep/frep_tree.cpp +++ b/cpp_frep/frep_tree.cpp @@ -48,7 +48,7 @@ namespace cpp_frep { } - + void addPrimitives(){ std::vector<scalar> g,g1; g.push_back(10.0f/nodeLength); @@ -390,6 +390,12 @@ namespace cpp_frep { } + std::string getPrintedGenome(){ + + return parsedTree->getPrintedName(); + + } + virtual ~Frep_tree() {} private: diff --git a/cpp_frep/main.cpp b/cpp_frep/main.cpp index 54a29382218fc7ac5c48f1cbedc316b47c89172a..288b8f863ebfa8ae595c61542ac030eb15c3169d 100755 --- a/cpp_frep/main.cpp +++ b/cpp_frep/main.cpp @@ -1,10 +1,11 @@ -// #include "frep.cpp" #include "frep_tree.cpp" #include "nelder_mead.h" #include <iostream> #include <vector> #include <stdio.h> #include <stdlib.h> +#include <opencv2/opencv.hpp> + using namespace cpp_frep; @@ -48,23 +49,10 @@ void drawFrep(Frep_tree frep,scalar minX,scalar maxX,scalar minY,scalar maxY,sca void drawFrep(Frep_tree frep,scalar minX,scalar maxX,scalar minY,scalar maxY,scalar dx,scalar dy,std::string name){ - FILE *imageFile; int height=(int)((maxX-minX)/dx); int width=(int)((maxY-minY)/dy); - int size=height*width*3; - // int x,y,pixel; + int size=height*width*4; - std::string fileName=name+".ppm"; - const char* c = fileName.c_str(); - imageFile=fopen(c,"wb"); - if(imageFile==NULL){ - perror("ERROR: Cannot open output file"); - exit(EXIT_FAILURE); - } - - fprintf(imageFile,"P6\n"); // P6 filetype - fprintf(imageFile,"%d %d\n",width,height); // dimensions - fprintf(imageFile,"255\n"); // Max pixel int stride=0; @@ -76,39 +64,41 @@ void drawFrep(Frep_tree frep,scalar minX,scalar maxX,scalar minY,scalar maxY,sca frep.setXY(i,j); scalar val=frep.eval(); if(val>0.0f){ - std::cout << "+" ; + // std::cout << "+" ; pix[stride+0]=0; pix[stride+1]=0; pix[stride+2]=0; + pix[stride+3]=0; }else{ - std::cout << "o" ; + // std::cout << "o" ; pix[stride+0]=255; pix[stride+1]=255; pix[stride+2]=255; + pix[stride+3]=255; } - stride+=3; + stride+=4; } - std::cout << '\n'; + // std::cout << '\n'; } - std::cout << '\n'; - - - // unsigned char pix[]={200,200,200, 100,100,100, 0,0,0, 255,0,0, 0,255,0, 0,0,255}; - fwrite(pix,1,size,imageFile); - fclose(imageFile); - -} - -float f(Vector v) { - float x = v[0]; - float y = v[1]; - return ((-x*x*x*x+4.5*x*x+2)/pow(2.71828,2*y*y)); -} + // std::cout << '\n'; + + cv::Mat image = cv::Mat(width, height, CV_8UC4, pix); + cv::putText(image, + frep.getPrintedGenome(), + cv::Point(10,10), // Coordinates + cv::FONT_HERSHEY_SIMPLEX, // Font + 0.25, // Scale. 2.0 = 2x bigger + cv::Scalar(0,0,0), // BGR Color + 1); // Line Thickness (Optional) + + std::string fileName=name+".jpg"; + cv::imwrite( fileName, image ); + cv::namedWindow("Display Image", cv::WINDOW_AUTOSIZE ); + cv::imshow("Display Image", image); + cv::waitKey(0); + -float f1(Vector v) { - float x = v[0]; - return -(2*x*x+6*x-3); } float constraint(float num){ @@ -119,23 +109,14 @@ float constraint(float num){ float compareL1(Vector v, Frep* frep,Frep_tree* frep_tree,scalar minX,scalar maxX,scalar minY,scalar maxY,scalar dx,scalar dy,std::string name){ - FILE *imageFile; int height=(int)((maxX-minX)/dx); int width=(int)((maxY-minY)/dy); - int size=height*width*3; + int size=height*width*4; // int x,y,pixel; - std::string fileName=name+".ppm"; - const char* c = fileName.c_str(); - imageFile=fopen(c,"wb"); - if(imageFile==NULL){ - perror("ERROR: Cannot open output file"); - exit(EXIT_FAILURE); - } - - fprintf(imageFile,"P6\n"); // P6 filetype - fprintf(imageFile,"%d %d\n",width,height); // dimensions - fprintf(imageFile,"255\n"); // Max pixel + std::string fileName=name+".jpg"; + // const char* c = fileName.c_str(); + int stride=0; @@ -179,23 +160,33 @@ float compareL1(Vector v, Frep* frep,Frep_tree* frep_tree,scalar minX,scalar max pix[stride+0]=0; pix[stride+1]=0; pix[stride+2]=0; + pix[stride+3]=0; }else{ // std::cout << "o" ; pix[stride+0]=255; pix[stride+1]=255; pix[stride+2]=255; + pix[stride+3]=255; } - stride+=3; + stride+=4; } // std::cout << '\n'; } // std::cout << '\n'; - float result= (float)same/((float)size/3.0f); + float result= (float)same/((float)size/4.0f); std::cout<<result <<"\n"; - // unsigned char pix[]={200,200,200, 100,100,100, 0,0,0, 255,0,0, 0,255,0, 0,0,255}; - fwrite(pix,1,size,imageFile); - fclose(imageFile); + + cv::Mat image = cv::Mat(width, height, CV_8UC4, pix); + cv::putText(image, + frep_tree->getPrintedGenome(), + cv::Point(10,20), // Coordinates + cv::FONT_HERSHEY_SIMPLEX, // Font + 0.5, // Scale. 2.0 = 2x bigger + cv::Scalar(0,0,0), // BGR Color + 1); // Line Thickness (Optional) + + cv::imwrite( fileName, image ); return result; } @@ -212,14 +203,15 @@ Vector getRandomGenome(int n){ return Vector(v); } + int main(int const, char const**) { scalar minX=-3.0f; scalar maxX=3.0f; scalar minY=-3.0f; scalar maxY=3.0f; - scalar dx=0.1f; - scalar dy=0.1f; + scalar dx=0.01f; + scalar dy=0.01f; std::vector<std::vector<scalar>> genome; @@ -245,11 +237,11 @@ int main(int const, char const**) { Frep_tree tree(genome); // genome[0][0]=0.1; // tree.updateGenome(genome); - drawFrep(tree,minX, maxX, minY, maxY, dx, dy,"image"); + // drawFrep(tree,minX, maxX, minY, maxY, dx, dy,"image"); - float precision = 0.001; + float precision = 0.01; int dimension = 3; NelderMeadOptimizer o(dimension, precision); @@ -271,12 +263,14 @@ int main(int const, char const**) { Rectangle r(-1.0f,1.0f,-1.0f,1.0f); Subtract s(&r,&c); PolarArray pa(&r,0.0f, 0.0f, 45.0f,-1.0f); - drawFrep(&s,minX, maxX, minY, maxY, dx, dy); + // drawFrep(&s,minX, maxX, minY, maxY, dx, dy); int count=0; + + // dx=0.1f; + // dy=0.1f; float score=0.0f; - // while (!o.done()&& !o.done(score)) { while ( !o.done(score)) { if(count>50){ count=0; @@ -342,44 +336,9 @@ int main(int const, char const**) { //0 add, 1 subtract, 2 intersect, 3 morph, 4 translate, 5 scale, 6 rotate, 7 linearArray, 8 polar array - // float precision = 0.01; - // int dimension = 2; - // NelderMeadOptimizer o(dimension, precision); - - // // request a simplex to start with - // Vector v(0.5, 0.5); - // o.insert(v); - // o.insert(Vector(0.1, 0.1)); - // o.insert(Vector(0.2, 0.7)); - - - // std::cout<< v.at(0)<<","<<v.at(1)<<"\n"; - - // while (!o.done()) { - // v = o.step(v, f(v)); - // std::cout<< v.at(0)<<","<<v.at(1)<<"\n"; - // std::cout<< f(v)<<"\n"; - // } - - // float precision = 0.001; - // int dimension = 1; - // NelderMeadOptimizer o(dimension, precision); - - // // request a simplex to start with - // Vector v(0.5); - // o.insert(v); - // o.insert(Vector(0.1)); - // o.insert(Vector(0.2)); - - - // std::cout<< v.at(0)<<"\n"; - - // while (!o.done()) { - // v = o.step(v, f1(v)); - // std::cout<< f1(v)<<"\n"; - // } - // std::cout<< f1(v)<<"\n"; return 0; } + +