Created
May 6, 2017 07:42
-
-
Save ollewelin/a29ce1a65a25df89a9f7de3e16a000d5 to your computer and use it in GitHub Desktop.
Image transforming tool to produce more training and verify images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This code take some posXXX.jpg and verXXX.jpg files in program root dir and tranform images to a folder with name \positive_data\posXXX.jpg verXXX.jpg | |
//#include <stdio.h> | |
//#include <unistd.h> | |
//#include <ctime> | |
//#include <iostream> | |
//#include <raspicam/raspicam_cv.h> | |
#include <opencv2/highgui/highgui.hpp> // OpenCV window I/O | |
#include <opencv2/imgproc/imgproc.hpp> // Gaussian Blur | |
#include <stdio.h> | |
#include <raspicam/raspicam_cv.h> | |
#include <opencv2/opencv.hpp> | |
#include <opencv2/core/core.hpp> // Basic OpenCV structures (cv::Mat, Scalar) | |
#include <cstdlib> | |
#include <ctime> | |
#include <math.h> // exp | |
#include <stdlib.h>// exit(0); | |
#include <iostream> | |
using namespace std; | |
using namespace cv; | |
//default capture width and height | |
//const int FRAME_WIDTH = 640; | |
//const int FRAME_HEIGHT = 480; | |
const int FRAME_WIDTH = 128; | |
const int FRAME_HEIGHT = 96; | |
const string WindowName = "Image viewer"; | |
#include <termios.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
Mat transf(Mat src) | |
{ | |
Point2f srcTri[3]; | |
Point2f dstTri[3]; | |
float scale_r; | |
float scale_r_n; | |
float scale_gain=0.1f; | |
float trans_x_r; | |
float trans_y_r; | |
float transelation_x = 0.0f; | |
float transelation_y = 0.0f; | |
float x_gain = 0.2f; | |
float y_gain = 0.1f; | |
float rot_max_min = 6.0f; | |
float rot_random =0.0f; | |
// srand (static_cast <unsigned> (time(0)));//Seed the randomizer | |
scale_r = (float) (rand() % 65535) / 65536; | |
printf("scale_r %f\n", scale_r); | |
trans_x_r = (float) (rand() % 65535) / 65536; | |
printf("trans_x_r %f\n", trans_x_r); | |
trans_y_r = (float) (rand() % 65535) / 65536; | |
printf("trans_y_r %f\n", trans_y_r); | |
rot_random = (float) (rand() % 65535) / 65536; | |
// printf("rot_random %f\n", rot_random); | |
scale_r = 0.5f + scale_r; | |
printf("scale_r %f\n", scale_r); | |
scale_r_n = 1.0f - scale_r; | |
transelation_x = (trans_x_r - 0.5f) * x_gain; | |
transelation_y = (trans_y_r - 0.5f) * y_gain; | |
scale_r = ((scale_r - 1.0f) * scale_gain) + 1.0f; | |
scale_r_n = scale_r_n * scale_gain; | |
printf("scale_r_n %f\n", scale_r_n); | |
Mat rot_mat( 2, 3, CV_8UC1 ); | |
Mat warp_mat( 2, 3, CV_8UC1 ); | |
Mat warp_dst, warp_rotate_dst; | |
/// Set the dst image the same type and size as src | |
warp_dst = Mat::zeros( src.rows, src.cols, src.type() ); | |
/// Set your 3 points to calculate the Affine Transform | |
srcTri[0] = Point2f( 0,0 ); | |
srcTri[1] = Point2f( src.cols - 1, 0 ); | |
srcTri[2] = Point2f( 0, src.rows - 1 ); | |
//2:1 | |
dstTri[0] = Point2f( src.cols*0.25, src.rows*0.25 ); | |
dstTri[1] = Point2f( src.cols*0.75, src.rows*0.25 ); | |
dstTri[2] = Point2f( src.cols*0.25, src.rows*0.75 ); | |
/// Get the Affine Transform | |
warp_mat = getAffineTransform( srcTri, dstTri ); | |
/// Apply the Affine Transform just found to the src image | |
warpAffine( src, warp_dst, warp_mat, warp_dst.size() ); | |
// dstTri[0] = Point2f( src.cols*(0.25+transelation_x), src.rows*0.25 ); | |
// dstTri[1] = Point2f( src.cols*(0.75+transelation_x), src.rows*0.25 ); | |
// dstTri[2] = Point2f( src.cols*(0.25+transelation_x), src.rows*0.75 ); | |
// dstTri[0] = Point2f( src.cols*(0.0+transelation_x), src.rows*(0.0+transelation_y) ); | |
// dstTri[1] = Point2f( src.cols*(1.0+transelation_x), src.rows*(0.0+transelation_y) ); | |
// dstTri[2] = Point2f( src.cols*(0.0+transelation_x), src.rows*(1.0+transelation_y) ); | |
dstTri[0] = Point2f( src.cols*(scale_r_n+transelation_x), src.rows*(scale_r_n+transelation_y) ); | |
dstTri[1] = Point2f( src.cols*(scale_r+transelation_x), src.rows*(scale_r_n+transelation_y) ); | |
dstTri[2] = Point2f( src.cols*(scale_r_n+transelation_x), src.rows*(scale_r+transelation_y) ); | |
/// Get the Affine Transform | |
warp_mat = getAffineTransform( srcTri, dstTri ); | |
/// Do the Affine Transformation scale and translation | |
warpAffine( src, warp_dst, warp_mat, warp_dst.size() ); | |
//================== Rotation ============================== | |
/** Rotating the image after Warp */ | |
rot_random = rot_random - 0.5f;// Make it to a random value of +/- 0.5 | |
rot_random = rot_random * rot_max_min; | |
printf("rot_random %f\n", rot_random); | |
/// Compute a rotation matrix with respect to the center of the image | |
Point center = Point( warp_dst.cols/2, warp_dst.rows/2 ); | |
double angle = 0.0; | |
double rot_scale = 1.0; | |
angle = (double)rot_random; | |
/// Get the rotation matrix with the specifications above | |
rot_mat = getRotationMatrix2D( center, angle, rot_scale ); | |
/// Rotate the warped image | |
warpAffine( warp_dst, warp_rotate_dst, rot_mat, warp_dst.size() ); | |
//============= End Rotation ============================== | |
imshow("warp_dst", warp_dst); | |
imshow("warp_rotate_dst", warp_rotate_dst); | |
imshow("src", src); | |
return warp_rotate_dst; | |
} | |
int main () | |
{ | |
//JPG File store | |
int ret; | |
char ch; | |
int files_number = 2000; | |
// FILE *store_files[files_number]; | |
char filename_src[100]; | |
char filename_dst[100]; | |
int verification=0; | |
int transformed_nr=0; | |
int nr_of_orginals=0; | |
float Rando=0.0f; | |
float trans_x=0; | |
float trans_y=0; | |
float scale=0; | |
//------------- | |
Mat color_img_ver,color_img_pos,image_pos, image_ver, cloned_img_pos , cloned_img_ver; | |
printf("Enter how many orginal images you have (same nr of pos.. and ver..)\n"); | |
scanf("%d", &nr_of_orginals); | |
printf("How many orginal image_poss you have enter = %d\n", nr_of_orginals); | |
printf("Enter how many transformed copy's of orginal images you want\n"); | |
scanf("%d", &transformed_nr); | |
printf("Transformed copy's of orginal will be = %d\n", transformed_nr); | |
srand (static_cast <unsigned> (time(0)));//Seed the randomizer | |
for(int i=0; i<10; i++) | |
{ | |
Rando = (float) (rand() % 65535) / 65536; | |
printf("Rando %f\n", Rando); | |
} | |
for(int org_nr=0; org_nr<nr_of_orginals; org_nr++) | |
{ | |
sprintf(filename_src, "pos%d.JPG", org_nr);//Assigne a filename "pos" with index number added | |
color_img_pos = imread( filename_src, 1 ); | |
if ( !color_img_pos.data ) | |
{ | |
printf("\n"); | |
printf("==================================================\n"); | |
printf("No image_pos data Error! Probably not find pos%d.JPG \n", org_nr); | |
printf("==================================================\n"); | |
printf("\n"); | |
//return -1; | |
} | |
image_pos = color_img_pos; | |
imshow("color_img_pos", color_img_pos); | |
imshow("image_pos", image_pos); | |
// m1_cloned = m1.clone(); | |
cloned_img_pos = image_pos.clone(); | |
for(int trans_nr=0; trans_nr<transformed_nr; trans_nr++) | |
{ | |
if(trans_nr>0) | |
{ | |
image_pos = transf(cloned_img_pos); | |
} | |
cv::imwrite("temporary_file.JPG",image_pos); | |
//--- Save JPG files ---- | |
sprintf(filename_dst, "./positive_data/pos%d.JPG", ((org_nr*transformed_nr) + trans_nr)); | |
ret = rename("temporary_file.JPG", filename_dst); | |
if(ret == 0) | |
{ | |
printf("File renamed successfully"); | |
} | |
else | |
{ | |
printf("Error: unable to rename the file"); | |
} | |
//--------------------- | |
//cout<<"image_pos saved at ./x.JPG"<<endl; | |
printf("image_pos save a file: %d", trans_nr); | |
printf(".JPG\n" ); | |
printf("trans_nr %d\n", trans_nr); | |
waitKey(1); | |
} | |
//======================== | |
sprintf(filename_src, "ver%d.JPG", org_nr);//Assigne a filename "pos" with index number added | |
color_img_ver = imread( filename_src, 1 ); | |
if ( !color_img_ver.data ) | |
{ | |
printf("\n"); | |
printf("==================================================\n"); | |
printf("No image data Error! Probably not find ver%d.JPG \n", org_nr); | |
printf("==================================================\n"); | |
printf("\n"); | |
//return -1; | |
} | |
image_ver = color_img_ver; | |
imshow("color_img_ver", color_img_ver); | |
imshow("image_ver", image_ver); | |
// m1_cloned = m1.clone(); | |
cloned_img_ver = image_ver.clone(); | |
for(int trans_nr=0; trans_nr<transformed_nr; trans_nr++) | |
{ | |
if(trans_nr>0) | |
{ | |
image_ver = transf(cloned_img_ver); | |
} | |
cv::imwrite("temporary_file.JPG",image_ver); | |
//--- Save JPG files ---- | |
sprintf(filename_dst, "./positive_data/ver%d.JPG", ((org_nr*transformed_nr) + trans_nr)); | |
ret = rename("temporary_file.JPG", filename_dst); | |
if(ret == 0) | |
{ | |
printf("File renamed successfully"); | |
} | |
else | |
{ | |
printf("Error: unable to rename the file"); | |
} | |
//--------------------- | |
//cout<<"image_ver saved at ./x.JPG"<<endl; | |
printf("image_ver save a file: %d", trans_nr); | |
printf(".JPG\n" ); | |
printf("trans_nr %d\n", trans_nr); | |
waitKey(1); | |
} | |
//======================== | |
waitKey(1); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment