Last active
December 20, 2015 01:18
-
-
Save huy10/6047553 to your computer and use it in GitHub Desktop.
first day here
This file contains 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
// Face_SwappingFinal.cpp : ��������̨Ӧ�ó��������ڵ㡣 | |
// | |
// FaceSwapping_new.cpp : ��������̨Ӧ�ó��������ڵ㡣 | |
// | |
#include "stdafx.h" | |
#include "opencv2/objdetect/objdetect.hpp" | |
#include "opencv2/highgui/highgui.hpp" | |
#include "opencv2/imgproc/imgproc.hpp" | |
#include "opencv/cxcore.h" | |
#include <fstream> | |
#include <math.h> | |
#include <iostream> | |
#include <stdio.h> | |
using namespace std; | |
using namespace cv; | |
static Scalar colors[] = | |
{ | |
Scalar(0,0,255), // red | |
Scalar(255,0,0), // blue | |
Scalar(0,255,0), // green | |
Scalar(0,255,255), // yellow | |
Scalar(255,255,0), // cyan | |
Scalar(255,0,255), // magenta | |
Scalar(0,0,0), // black | |
Scalar(255,255,255), // white | |
Scalar(0,128,255), // | |
Scalar(255,128,0), // | |
Scalar(0,255,128) // | |
}; | |
void showhist(Mat input,Mat output) | |
{ | |
vector<Mat> rgb_planes; | |
split( input, rgb_planes ); | |
int histSize = 255; | |
float range[] = { 0, 255 } ; | |
const float* histRange = { range }; | |
bool uniform = true; | |
bool accumulate = false; | |
Mat r_hist, g_hist, b_hist; | |
calcHist( &rgb_planes[0], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate ); | |
calcHist( &rgb_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate ); | |
calcHist( &rgb_planes[2], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate ); | |
int hist_w = 400; int hist_h = 400; | |
int bin_w = cvRound( (double) hist_w/histSize ); | |
normalize(r_hist, r_hist, 0, output.rows, NORM_MINMAX, -1, Mat() ); | |
normalize(g_hist, g_hist, 0, output.rows, NORM_MINMAX, -1, Mat() ); | |
normalize(b_hist, b_hist, 0, output.rows, NORM_MINMAX, -1, Mat() ); | |
for( int i = 1; i < 255; i++ ) | |
{ | |
rectangle( output, Point( bin_w*(i-1), 400 ) , | |
Point( bin_w*(i), hist_h - cvRound(b_hist.at<float>(i-1))), | |
Scalar( 0, 0, 255), 1, 8, 0 ); | |
rectangle( output, Point( bin_w*(i-1), hist_h ) , | |
Point( bin_w*(i), hist_h - cvRound(g_hist.at<float>(i-1)) ), | |
Scalar( 0, 255, 0), 1, 8, 0 ); | |
rectangle( output, Point( bin_w*(i-1), hist_h ) , | |
Point( bin_w*(i), hist_h - cvRound(r_hist.at<float>(i-1)) ), | |
Scalar( 255, 0, 0), 1, 8, 0 ); | |
} | |
} | |
void generateGaussian(Mat input, double dst[]) | |
{ | |
//double dst[256] = {0}; | |
float a; | |
double sum = 0; | |
for (int i = 1; i < 255; i++) | |
{ | |
a = input.at<float>(i); | |
//dst[i] = 1/255* a*a; | |
if (i > 1 && i < 30) | |
{ | |
dst[i] =(double) 40 - i; | |
} | |
if (i>30 && i < 180) | |
dst[i] =(double) 10 - (i-30)/15; | |
if (i > 180 && i < 200) | |
dst [i] = (double)i - 180; | |
if (i > 200 && i < 250) | |
dst[i] = (double ) 20-(i-200)*0.4; | |
/*if (i > 150 && i <200) | |
{ | |
dst[i] = i-150; | |
}*/ | |
sum = sum + dst[i]; | |
} | |
for (int i = 1; i < 255; i++) | |
dst[i] = dst[i] /sum; //normalize | |
//return dst; | |
} | |
void myMatchHist( Mat & src, Mat & dst ) | |
{ | |
int histSize = 255; | |
float range[] = { 0, 255 } ; | |
const float* histRange = { range }; | |
bool uniform = true; | |
bool accumulate = false; | |
Mat hist; | |
calcHist( &src, 1, 0, Mat(), hist, 1, &histSize, &histRange, uniform, accumulate ); | |
normalize(hist, hist, 0, src.rows, NORM_MINMAX, -1, Mat() ); | |
Mat hist_dst; | |
calcHist( &dst, 1, 0, Mat(), hist_dst, 1, &histSize, &histRange, uniform, accumulate ); | |
normalize(hist_dst, hist_dst, 0, dst.rows, NORM_MINMAX, -1, Mat() ); | |
double val1 = 0.0; | |
double val2 = 0.0; | |
double T[256] = {0}; | |
double S[256] = {0}; | |
double G[256] = {0}; | |
for (int i = 0; i <255;++i) | |
{ | |
val1 += hist.at<float>(i); | |
val2 += hist_dst.at<float>(i); | |
S[i] = val1; | |
G[i] = val2; | |
} | |
for (int i = 0; i <255 ; ++i) | |
S[i] = S[i] /S[254]; | |
for (int i = 0; i <255 ; ++i) | |
G[i] = G[i] /G[254]; | |
double min_val = 0.0; | |
int PG = 0; | |
for (int i=0 ;i < 256 ;++i) | |
{ | |
min_val = 10.0; | |
for (int j = 0; j <256;++j) | |
{ | |
if( (G[j] - S[i]) < min_val && (G[j] - S[i]) >= 0) | |
{ | |
min_val = (G[j] - S[i]); | |
PG = j; | |
} | |
} | |
T[i] = (double)PG; | |
} | |
for (int x = 1; x< src.rows;++x) | |
{ | |
for (int y = 1; y<src.cols;++y) | |
{ | |
int i = (int) src.at<uchar>(x,y); | |
src.at<uchar>(x,y)=(uchar)T[i]; | |
} | |
} | |
} | |
void m_swap(Mat & roi_of_src, Mat & roi_of_dst, Mat & warp_rotate_dst,int maxX,int maxY,int minX,int minY) | |
{ | |
Point2f srcTri[3]; | |
Point2f dstTri[3]; | |
Mat rot_mat( 2, 3, CV_32FC3 ); | |
Mat warp_mat( 2, 3, CV_32FC3 ); | |
Mat warp_dst; | |
// edited by HU YANG | |
warp_dst = Mat::zeros( roi_of_dst.rows, roi_of_dst.cols, roi_of_dst.type() ); | |
srcTri[0] = Point2f( 0,0 ); | |
srcTri[1] = Point2f( roi_of_src.cols - 1, 0 ); | |
srcTri[2] = Point2f( 0, roi_of_src.rows - 1 ); | |
dstTri[0] = Point2f( 0,0); | |
dstTri[1] = Point2f( roi_of_dst.cols -1, 0 ); | |
dstTri[2] = Point2f( 0, roi_of_dst.rows - 1 ); | |
warp_mat = getAffineTransform( srcTri, dstTri ); | |
warpAffine( roi_of_src, warp_dst, warp_mat, warp_dst.size() ); | |
Point center = Point( warp_dst.cols/2, warp_dst.rows/2 ); | |
//angle && scale need to be revised | |
double angle = atan2((float)maxY-minY,(float)maxX-minX); | |
//double scale = (double)roi_of_dst.cols * (double)roi_of_dst.rows / (double)roi_of_src.cols / (double)roi_of_src.rows; | |
double scale =1 ; | |
scale = sqrt(scale); | |
rot_mat = getRotationMatrix2D( center, angle, scale ); | |
warpAffine( warp_dst, warp_rotate_dst, rot_mat, warp_dst.size() ); | |
namedWindow( "source_window", CV_WINDOW_AUTOSIZE ); | |
imshow( "source_window", roi_of_src ); | |
namedWindow( "warp_window", CV_WINDOW_AUTOSIZE ); | |
imshow( "warp_window", warp_dst ); | |
namedWindow( "warp_rotate_window", CV_WINDOW_AUTOSIZE ); | |
imshow( "warp_rotate_window", warp_rotate_dst ); | |
namedWindow( "test_window", CV_WINDOW_AUTOSIZE ); | |
imshow( "test_window", roi_of_dst ); | |
Mat hist1( 400, 400, CV_8UC3, Scalar( 0,0,0) ); | |
showhist(warp_rotate_dst,hist1); | |
/*imshow("����ǰ", hist1 );*/ | |
// // zhi fang tu gui ding hua | |
// vector<Mat> rgb_planes; | |
//vector<Mat> rgb_planes_dst; | |
// split( warp_rotate_dst, rgb_planes ); | |
//split( roi_of_dst, rgb_planes_dst ); | |
/*myMatchHist( rgb_planes[0], rgb_planes_dst[0]); | |
myMatchHist( rgb_planes[1], rgb_planes_dst[1]); | |
myMatchHist( rgb_planes[2], rgb_planes_dst[2]);*/ | |
//equalizeHist(rgb_planes[0], rgb_planes[0]); | |
//equalizeHist(rgb_planes[1], rgb_planes[1]); | |
//equalizeHist(rgb_planes[2], rgb_planes[2]); | |
// merge(rgb_planes,warp_rotate_dst); | |
//merge(rgb_planes_dst,roi_of_dst); | |
Mat hist2( 400, 400, CV_8UC3, Scalar( 0,0,0) ); | |
showhist(warp_rotate_dst,hist2); | |
//imshow("������", hist2 ); | |
Mat hist3( 400, 400, CV_8UC3, Scalar( 0,0,0) ); | |
showhist(roi_of_dst,hist3); | |
/*imshow("Ŀ��", hist3 );*/ | |
} | |
//void calclab(Mat & src,double * m1[], double *m2[], double* m3[]) | |
//{ | |
// vector<Mat> rgb_planes_src; | |
// split( src, rgb_planes_src ); | |
// double L_src[500][500] ; | |
// double M_src[500][500] ; | |
// double S_src[500][500]; | |
// | |
// for (int i = 0; i <500; i++ ) | |
// for (int j = 0; j < 500; j++) | |
// { | |
// L_src[i][j] = 0; | |
// M_src[i][j] = 0; | |
// S_src[i][j] = 0; | |
// } | |
// for (int i = 0; i <rgb_planes_src[2].rows; i++ ) | |
// for (int j = 0; j < rgb_planes_src[2].cols; j++) | |
// { | |
// *(L_src[i]+j) = 0.3811 * (double)rgb_planes_src[2].at<uchar>(i,j) + 0.5783 * rgb_planes_src[1].at<uchar>(i,j) + 0.0402 * rgb_planes_src[0].at<uchar>(i,j); | |
// M_src[i][j] =0.1967 * (double)rgb_planes_src[2].at<uchar>(i,j) +0.7244 * rgb_planes_src[1].at<uchar>(i,j) +0.0782 * rgb_planes_src[0].at<uchar>(i,j); | |
// S_src[i][j] = 0.0241 * (double)rgb_planes_src[2].at<uchar>(i,j) + 0.1288 * rgb_planes_src[1].at<uchar>(i,j) + 0.8444 * rgb_planes_src[0].at<uchar>(i,j); | |
// | |
// | |
// } | |
// | |
// | |
// | |
// /* Mat L_src = 0.3811 * rgb_planes_src[2] + 0.5783 * rgb_planes_src[1] + 0.0402 * rgb_planes_src[0]; | |
// Mat M_src = 0.1967 * rgb_planes_src[2] + 0.7244 * rgb_planes_src[1] + 0.0782 * rgb_planes_src[0]; | |
// Mat S_src = 0.0241 * rgb_planes_src[2] + 0.1288 * rgb_planes_src[1] + 0.8444 * rgb_planes_src[0];*/ | |
// ofstream ofile1("lmc.txt"); | |
// ofile1 << L_src << endl; | |
// ofile1 << M_src << endl; | |
// ofile1 << S_src << endl; | |
// ofile1.close(); | |
// double two = 2.0,ten = 10.0; | |
// for (int i = 0; i < rgb_planes_src[0].rows; i++ ) | |
// for (int j = 0; j < rgb_planes_src[0].cols; j++ ) | |
// { | |
// L_src[i][j] = log((double)L_src[i][j]) / log(ten); | |
// M_src[i][j] = log((double)M_src[i][j]) / log(ten); | |
// S_src[i][j] = log((double)S_src[i][j]) / log(ten); | |
// } | |
// | |
// double three = 3.0, six = 6.0; | |
// //double ** m1, **m2, **m3 ; | |
// for (int i = 0; i <rgb_planes_src[2].rows; i++ ) | |
// for (int j = 0; j < rgb_planes_src[2].cols; j++) | |
// { | |
// m1[i][j] = 0.577 * L_src[i][j] + 0.577 * M_src[i][j] + 0.577 * S_src[i][j]; | |
// m2[i][j] = 0.408 * L_src[i][j] + 0.408 * M_src[i][j] - 0.816 * S_src[i][j]; | |
// m3[i][j] = 0.707 * L_src[i][j] -0.707 * M_src[i][j] ; | |
// } | |
// /* m1 = 0.577 * L_src + 0.577 * M_src + 0.577 * S_src; | |
// m2 = 0.408 * L_src + 0.408 * M_src - 2*0.408 * S_src; | |
// m3 = 0.707 * L_src - 0.707 * M_src ;*/ | |
// | |
// ofstream ofile("m.txt"); | |
// ofile << m1 << endl; | |
// ofile << m2 << endl; | |
// ofile << m3 << endl; | |
// ofile.close(); | |
//} | |
Mat1f colortransfer(Mat src, Mat dst) | |
{ | |
Mat m_final; | |
double s1 = 0; | |
Mat src1 = src; | |
Mat dst1 = dst; | |
vector<Mat> rgb_planes_src; | |
split( src1, rgb_planes_src ); | |
vector<Mat> rgb_planes_dst; | |
split( dst1, rgb_planes_dst ); | |
vector<Mat> rgb_planes_src_f; | |
rgb_planes_src_f.resize(3); | |
rgb_planes_src[0].convertTo(rgb_planes_src_f[0], CV_32FC1); | |
rgb_planes_src[1].convertTo(rgb_planes_src_f[1], CV_32FC1); | |
rgb_planes_src[2].convertTo(rgb_planes_src_f[2], CV_32FC1); | |
//vector<Mat> rgb_planes_dst_f; | |
rgb_planes_dst.resize(3); | |
rgb_planes_dst[0].convertTo(rgb_planes_dst[0], CV_32FC1); | |
rgb_planes_dst[1].convertTo(rgb_planes_dst[1], CV_32FC1); | |
rgb_planes_dst[2].convertTo(rgb_planes_dst[2], CV_32FC1); | |
cout << "Hahaha"; | |
Mat1f L_src = 0.3811 * rgb_planes_src_f[2] +0.5783 * rgb_planes_src_f[1]+ 0.0402 * rgb_planes_src_f[0]; | |
Mat1f M_src = 0.1967 * rgb_planes_src_f[2] +0.7244 * rgb_planes_src_f[1]+ 0.0782 * rgb_planes_src_f[0]; | |
Mat1f S_src = 0.0241 * rgb_planes_src_f[2] +0.1288 * rgb_planes_src_f[1]+ 0.8444 * rgb_planes_src_f[0]; | |
ofstream ofile("src_f.txt"); | |
ofile <<rgb_planes_src_f[2] << endl; | |
ofile << rgb_planes_src_f[1]<< endl; | |
ofile << rgb_planes_src_f[0] << endl; | |
ofile.close(); | |
imshow("L_src",rgb_planes_src_f[0]); | |
imshow("M_src",rgb_planes_src_f[1]); | |
imshow("S_src",rgb_planes_src_f[2]); | |
Mat1f L_dst = 0.3811 * rgb_planes_dst[2] +0.5783 * rgb_planes_dst[1]+ 0.0402 * rgb_planes_dst[0]; | |
Mat1f M_dst = 0.1967 * rgb_planes_dst[2] +0.7244 * rgb_planes_dst[1]+ 0.0782 * rgb_planes_dst[0]; | |
Mat1f S_dst = 0.0241 * rgb_planes_dst[2] +0.1288 * rgb_planes_dst[1]+ 0.8444 * rgb_planes_dst[0]; | |
imshow("L_dst",L_dst); | |
imshow("M_dst",M_dst); | |
imshow("S_dst",S_dst); | |
for (int i = 0; i <rgb_planes_src[2].rows; i++ ) | |
for (int j = 0; j < rgb_planes_src[2].cols; j++) | |
{ | |
if (L_src(i,j) != 0) | |
L_src(i,j) = log10(L_src(i,j)); | |
if (M_src(i,j) != 0) | |
M_src(i,j) = log10(M_src(i,j)); | |
if (S_src(i,j) != 0) | |
S_src(i,j) = log10(S_src(i,j)); | |
} | |
for (int i = 0; i <rgb_planes_dst[2].rows; i++ ) | |
for (int j = 0; j < rgb_planes_dst[2].cols; j++) | |
{ | |
if (L_dst(i,j) != 0) | |
L_dst(i,j) = log10(L_dst(i,j)); | |
if (M_dst(i,j) != 0) | |
M_dst(i,j) = log10(M_dst(i,j)); | |
if (S_dst(i,j) != 0) | |
S_dst(i,j) = log10(S_dst(i,j)); | |
} | |
Mat1f m1 = 0.577 * L_src + 0.577 * M_src + 0.577 * S_src; | |
Mat1f m2 = 0.408 * L_src + 0.408 * M_src - 2 * 0.408 * S_src; | |
Mat1f m3 = 0.707 * L_src - 0.707 * M_src ; | |
for (int i = 0; i < src.rows; i++ ) | |
for (int j = 0; j < src.cols; j++ ) | |
{ | |
if(m1(i,j) > 5.0) | |
m1(i,j) = 5.0; | |
if(m2(i,j) > 5.0) | |
m2(i,j) = 5.0; | |
if(m3(i,j) > 5.0) | |
m3(i,j) = 5.0; | |
if(m1(i,j) < 0.00001) | |
m1(i,j) = 0.00001; | |
m2(i,j) = abs(m2(i,j)); | |
m3(i,j) = abs(m3(i,j)); | |
} | |
Mat1f m4 = 0.577 * L_dst + 0.577 * M_dst + 0.577 * S_dst; | |
Mat1f m5 = 0.408 * L_dst + 0.408 * M_dst - 2*0.408 * S_dst; | |
Mat1f m6 = 0.707 * L_dst - 0.707 * M_dst ; | |
for (int i = 0; i < dst.rows; i++ ) | |
for (int j = 0; j < dst.cols; j++ ) | |
{ | |
if(m4(i,j) > 5.0) | |
m4(i,j) = 5.0; | |
if(m5(i,j) > 5.0) | |
m5(i,j) = 5.0; | |
if(m6(i,j) > 5.0) | |
m6(i,j) = 5.0; | |
if(m4(i,j) < 0.00001) | |
m4(i,j) = 0.00001; | |
m5(i,j) = abs(m5(i,j)); | |
m6(i,j) = abs(m6(i,j)); | |
} | |
/*ofstream ofile("m.txt"); | |
ofile << m1 << endl; | |
ofile << m2 << endl; | |
ofile << m3 << endl; | |
ofile.close();*/ | |
float temp1 = 0.0, temp2 = 0.0,temp3 = 0.0,temp4 = 0.0,temp5 = 0.0,temp6 = 0.0; | |
for (int i = 0; i < src.rows; i++ ) | |
for (int j = 0; j < src.cols; j++ ) | |
{ | |
temp1 += m1(i,j)/src.rows/src.cols; | |
temp2 += m2(i,j); | |
temp3 += m3(i,j); | |
} | |
//temp1 = temp1 / src.rows / src.cols; // mean | |
temp2 = temp2 / src.rows / src.cols; // mean | |
temp3 = temp3 / src.rows / src.cols; // mean | |
for (int i = 0; i < dst.rows; i++ ) | |
for (int j = 0; j < dst.cols; j++ ) | |
{ | |
temp4 += m4(i,j)/ dst.rows / dst.cols; | |
temp5 += m5(i,j)/ dst.rows / dst.cols; | |
temp6 += m6(i,j)/ dst.rows / dst.cols; | |
} | |
//temp4 = temp4 / dst.rows / dst.cols; // mean | |
//temp5= temp5 / dst.rows / dst.cols; // mean | |
//temp6 = temp6 / dst.rows / dst.cols; // mean | |
double var1 = 0.0,var2 = 0.0,var3 = 0.0,var4 = 0.0,var5 = 0.0,var6 = 0.0; | |
for (int i = 0; i < src.rows; i++ ) | |
for (int j = 0; j < src.cols; j++ ) | |
{ | |
var1 += (m1(i,j)- temp1 )*(m1(i,j)- temp1 ); | |
var2 += ( m2(i,j) - temp2 )*( m2(i,j) - temp2 ); | |
var3 += ( m3(i,j) - temp3 )*(m3(i,j) - temp3 ); | |
} | |
for (int i = 0; i < dst.rows; i++ ) | |
for (int j = 0; j <dst.cols; j++ ) | |
{ | |
var4 += ( m4(i,j) - temp4 )*( m4(i,j) - temp4 ); | |
var5 += (m5(i,j) - temp5 )*( m5(i,j) - temp5 ); | |
var6 += ( m6(i,j) - temp6 )*( m6(i,j)- temp6 ); | |
} | |
var1 = var1 / src.rows / src.cols / src.rows /src.cols; // variance | |
var2 = var2 /src.rows / src.cols / src.rows /src.cols; | |
var3 = var3 /src.rows / src.cols / src.rows /src.cols; | |
var4 = var4 / dst.rows / dst.cols / dst.rows /dst.cols; | |
var5= var5 /dst.rows / dst.cols / dst.rows /dst.cols; | |
var6 = var6 / dst.rows / dst.cols / dst.rows /dst.cols; | |
double sig1 = (double)sqrt((double)var1); | |
double sig2 = (double)sqrt((double)var2); | |
double sig3 = (double)sqrt((double)var3); | |
double sig4 = (double)sqrt((double)var4); | |
double sig5 = (double)sqrt((double)var5); | |
double sig6 = (double)sqrt((double)var6); | |
for (int i = 0; i < src.rows; i++ ) | |
for (int j = 0; j < src.cols; j++ ) | |
{ | |
m1(i,j) -= temp1; | |
m1(i,j) /= sig1; //var4 = var1_dst | |
m1(i,j) *= sig4; | |
m1(i,j) += temp4; | |
m2(i,j) -= temp2; | |
m2(i,j) /= sig2; //var5 = var2_dst | |
m2(i,j) *= sig5; | |
m2(i,j)+= temp5; | |
m3(i,j) -= temp3; | |
m3(i,j) /= sig3; //var6 = var3_dst | |
m3(i,j) *= sig6; | |
m3(i,j) += temp6; | |
} | |
L_src = 1/(sqrt(3.0)) * m1 + 1/(sqrt(6.0))* m2 + 1/(sqrt(2.0)) * m3; | |
M_src = 1/(sqrt(3.0)) * m1 + 1/(sqrt(6.0))* m2 - 1/(sqrt(2.0)) * m3; | |
S_src = 1/(sqrt(3.0)) * m1 - 2/(sqrt(6.0))* m2 ; | |
for (int i = 0; i <src.rows; i++ ) | |
for (int j = 0; j < src.cols; j++) | |
{ | |
L_src(i,j) = pow(10,L_src(i,j)); | |
M_src(i,j) = pow(10,M_src(i,j)); | |
S_src(i,j) = pow(10,S_src(i,j)); | |
} | |
rgb_planes_src_f[2] = 4.4679 * L_src - 3.5873 * M_src + 0.1193 * S_src; | |
rgb_planes_src_f[1] = -1.2186 * L_src + 2.3809 * M_src - 0.1624 * S_src; | |
rgb_planes_src_f[0] = 0.0497 * L_src - 0.2439 * M_src + 1.2045 * S_src; //RGB shunxu? | |
ofstream ofile1("after.txt"); | |
ofile1 <<rgb_planes_src_f[2] << endl; | |
ofile1 << rgb_planes_src_f[1]<< endl; | |
ofile1 << rgb_planes_src_f[0] << endl; | |
imshow("m1",rgb_planes_src_f[2]); | |
imshow("m2",rgb_planes_src_f[1]); | |
imshow("m3",rgb_planes_src_f[0]); | |
ofile1.close(); | |
m_final.convertTo(m_final,CV_8UC3,255.0); | |
merge(rgb_planes_src_f,m_final); // src_final | |
for (int n = 0; n < src.channels(); n++) | |
for (int i = 0; i <src.rows; i++ ) | |
for(int j = 0; j < src.cols; j++) | |
{ | |
if (m_final.at<float>(i,j*src.channels() + n) > 255.0) | |
m_final.at<float>(i,j*src.channels() + n) = 255.0; | |
} | |
//m_final = src; | |
return m_final; | |
} | |
int main(int argc, _TCHAR* argv[]) | |
{ | |
Mat testImage; | |
testImage = imread("E:\\Programs\\vosm-0.3.3\\Debug\\JIAPEI\\images\\testing\\yangmi.jpg"); | |
//"E:\\documents\\visual studio 2010\\Projects\\Face\\Face\\00.jpg"); | |
ifstream file("E:\\Programs\\vosm-0.3.3\\Debug\\points\\yangmi.txt"); | |
Point ishp[88]; | |
int i = 0; | |
while(!file.eof()) | |
{ | |
file >> ishp[i].x >> ishp[i].y; | |
//cv::circle( testImage, ishp[i], 2, colors[1], -1, 8, 0 ); | |
i++ ; | |
} | |
file.close(); | |
//imshow("lena",testImage); | |
Mat srcImage; | |
srcImage = imread("E:\\Programs\\vosm-0.3.3\\Debug\\JIAPEI\\images\\testing\\liuyifei.jpg"); //hello.png | |
ifstream sfile("E:\\Programs\\vosm-0.3.3\\Debug\\points\\liuyifei.txt"); | |
Point ishp2[88]; | |
i = 0; | |
while(!sfile.eof()) | |
{ | |
sfile >> ishp2[i].x >> ishp2[i].y; | |
//if ((i>=67 && i <=87)|| (i >= 4 && i<=10)) | |
// cv::circle( srcImage, ishp2[i], 2, colors[0], -1, 8, 0 ); | |
i++; | |
} | |
sfile.close(); | |
Mat mask = Mat::zeros( testImage.rows, testImage.cols, testImage.type() ); | |
int t = 67; | |
while (t < 87) | |
{ | |
cv::line(mask,ishp[t],ishp[t+1],colors[7],5,8,0); | |
t++; | |
} | |
cv::line(mask,ishp[87],ishp[10],colors[7],5,8,0); | |
cv::line(mask,ishp[67],ishp[7],colors[7],5,8,0); | |
int m = 4; | |
while (m < 10) | |
{ | |
cv::line(mask,ishp[m],ishp[m+1],colors[7],5,8,0); | |
m++; | |
} | |
Point center_mask; | |
center_mask.x = ishp[67].x - 10; | |
center_mask.y = ishp[67].y - 10; | |
floodFill(mask,center_mask,colors[7]); | |
Mat mask_gray; | |
cvtColor(mask,mask_gray,CV_BGR2GRAY); | |
//namedWindow( "mask", CV_WINDOW_AUTOSIZE ); | |
// imshow("mask",mask_gray); | |
//now for the source image | |
Mat mask_src = Mat::zeros( srcImage.rows, srcImage.cols, srcImage.type() ); | |
t = 67; | |
while (t < 87) | |
{ | |
cv::line(mask_src,ishp2[t],ishp2[t+1],colors[7],5,8,0); | |
t++; | |
} | |
cv::line(mask_src,ishp2[87],ishp2[10],colors[7],5,8,0); | |
cv::line(mask_src,ishp2[67],ishp2[7],colors[7],5,8,0); | |
m = 4; | |
while (m < 10) | |
{ | |
cv::line(mask_src,ishp2[m],ishp2[m+1],colors[7],5,8,0); | |
m++; | |
} | |
Point center_mask_src; | |
center_mask_src.x = ishp2[67].x - 10; | |
center_mask_src.y = ishp2[67].y - 10; | |
floodFill(mask_src,center_mask_src,colors[7]); | |
Mat mask_gray_src; | |
cvtColor(mask_src,mask_gray_src,CV_BGR2GRAY); | |
//namedWindow( "mask source", CV_WINDOW_AUTOSIZE ); | |
// imshow("mask source",mask_gray_src); | |
//calculate four points to transform | |
//Point dst1,dst2,dst3,dst4; | |
int maxX = 0,minX = 1000; | |
int maxY = 0,minY = 1000; | |
for (unsigned int i = 0 ; i < 88 ; i++) | |
{ | |
if (ishp[i].x > maxX) | |
{ | |
maxX = ishp[i].x ; | |
//dst2.x = ishp[i].x; | |
//dst2.y = ishp[i].y; | |
} | |
if (ishp[i].x < minX) | |
{ | |
minX = ishp[i].x ; | |
//dst4.x = ishp[i].x; | |
//dst4.y = ishp[i].y; | |
} | |
} | |
for (unsigned int i = 0 ; i < 88 ; i++) | |
{ | |
if (ishp[i].y > maxY) | |
{ | |
maxY =ishp[i].y; | |
// dst3.x = ishp[i].x; | |
// dst3.y = ishp[i].y; | |
} | |
if (ishp[i].y < minY) | |
{ | |
minY = ishp[i].y ; | |
// dst1.x = ishp[i].x; | |
//dst1.y = ishp[i].y; | |
} | |
} | |
//calculate four points to transform | |
//Point src1,src2,src3,src4; | |
int maxX_src = 0,minX_src = 1000; | |
int maxY_src = 0,minY_src = 1000; | |
for (unsigned int i = 0 ; i < 88 ; i++) | |
{ | |
if (ishp2[i].x > maxX_src) | |
{ | |
maxX_src = ishp2[i].x ; | |
//src2.x = ishp2[i].x; | |
//src2.y = ishp2[i].y; | |
} | |
if (ishp2[i].x < minX_src) | |
{ | |
minX_src = ishp2[i].x ; | |
//src4.x = ishp2[i].x; | |
//src4.y = ishp2[i].y; | |
} | |
} | |
for (unsigned int i = 0 ; i < 88 ; i++) | |
{ | |
if (ishp2[i].y > maxY_src) | |
{ | |
maxY_src =ishp2[i].y; | |
//src3.x = ishp2[i].x; | |
//src3.y = ishp2[i].y; | |
} | |
if (ishp2[i].y < minY_src) | |
{ | |
minY_src = ishp2[i].y ; | |
//src1.x = ishp2[i].x; | |
//src1.y = ishp2[i].y; | |
} | |
} | |
//smaller region | |
/* maxX -= 10; minX += 10; | |
maxY -= 10; minY += 10; | |
maxX_src -= 10; minX_src += 10; | |
maxY_src -= 10; minY_src += 10;*/ | |
/* Point center_src; | |
center_src.x = (maxX_src + minX_src)/2; | |
center_src.y = (maxY_src + minY_src)/2; | |
int radius_src = ((maxX_src - minX_src) > (maxY_src - minY_src) )?(maxY_src - minY_src):(maxX_src - minX_src); | |
*/ | |
// cout <<"dst1 " <<dst1.x <<" "<<dst1.y<<endl; | |
// cout <<"dst2 " <<dst2.x <<" "<<dst2.y<<endl; | |
// cout <<"dst3 " <<dst3.x <<" "<<dst3.y<<endl; | |
// cout <<"dst4 " <<dst4.x <<" "<<dst4.y<<endl; | |
//cout <<"src1 " <<src1.x <<" "<<src1.y<<endl; | |
// cout <<"src2 " <<src2.x <<" "<<src2.y<<endl; | |
// cout <<"src3 " <<src3.x <<" "<<src3.y<<endl; | |
// cout <<"src4 " <<src4.x <<" "<<src4.y<<endl; | |
// double k1 = 0,k2 = 0,k3 = 0,k4 = 0,k5 = 0,k6 = 0,k7 = 0,k8 = 0; | |
/*Mat src = (Mat_<double>(4,4) << src1.x,src1.y,src1.x*src1.y,1,src2.x,src2.y,src2.x*src2.y,1,src3.x,src3.y,src3.x*src3.y,1,src4.x,src4.y,src4.x*src4.y,1); | |
Mat K = (Mat_<double>(4,2) << k1,k5,k2,k6,k3,k7,k4,k8); | |
Mat dst = (Mat_<double>(4,2) << dst1.x,dst1.y,dst2.x,dst2.y,dst3.x,dst3.y,dst4.x,dst4.y); | |
*/ | |
//get the ROI of src | |
Rect roi_src(minX_src,minY_src,maxX_src-minX_src,maxY_src-minY_src); | |
Mat roi_of_src = srcImage(roi_src); | |
//get the ROI of dst | |
Rect roi_dst(minX , minY , maxX-minX, maxY-minY); | |
Mat roi_of_dst = testImage(roi_dst); | |
Mat temp_dst = roi_of_dst; | |
Mat1f warp_rotate_dst, warp_rotate_src; | |
//m_swap(roi_of_src, roi_of_dst, warp_rotate_dst,maxX,maxY,minX,minY); | |
//m_swap(roi_of_dst, roi_of_src, warp_rotate_src,maxX_src,maxY_src,minX_src,minY_src); | |
//warp_rotate_dst.convertTo(convertTo(warp_rotate_dst,CV_8UC3); | |
//warp_rotate_src.convertTo(warp_rotate_src ,CV_8UC3); | |
Mat warp_rotate_dst_temp,warp_rotate_src_temp; | |
m_swap(roi_of_src, roi_of_dst, warp_rotate_dst_temp,maxX,maxY,minX,minY); | |
m_swap(roi_of_dst, roi_of_src, warp_rotate_src_temp,maxX_src,maxY_src,minX_src,minY_src); | |
warp_rotate_dst = colortransfer( warp_rotate_dst_temp, warp_rotate_src_temp ); | |
warp_rotate_src = colortransfer( warp_rotate_src_temp, warp_rotate_dst_temp ); | |
imshow("warp_rotate_src",warp_rotate_src_temp); | |
imshow("warp_rotate_dst",warp_rotate_dst_temp); | |
int channels = testImage.channels(); | |
for (int n = 0; n < channels; n++) | |
for (int i = roi_dst.y; i < roi_dst.y + roi_dst.height; i++) | |
for (int j = roi_dst.x ; j < roi_dst.x + roi_dst.width; j++) | |
{ | |
if(mask_gray.at<uchar>(i,j) == 0) | |
testImage.at<uchar>(i,j*channels+n) = warp_rotate_dst(i-roi_dst.y,(j-roi_dst.x)*channels+n); | |
if (i==roi_dst.y || i == roi_dst.y + roi_dst.height) | |
{ | |
Rect temp (j,i,3,3); | |
Mat temp_mat = testImage(temp); | |
//blur(temp_mat,testImage(temp),Size(3,3)); | |
} | |
if (j == roi_dst.x || j == roi_dst.x + roi_dst.width) | |
{ | |
Rect temp (j,i,3,3); | |
Mat temp_mat = testImage(temp); | |
// blur(temp_mat,testImage(temp),Size(3,3)); | |
} | |
} | |
for (int i = roi_dst.y; i < roi_dst.y + roi_dst.height-10; i++) | |
for (int j = roi_dst.x ; j < roi_dst.x + roi_dst.width-10; j++) | |
{ | |
Rect temp (j,i,3,3); | |
Mat temp_mat = testImage(temp); | |
//blur(temp_mat,testImage(temp),Size(3,3)); | |
} | |
imshow("image1",testImage); | |
channels = srcImage.channels(); | |
for (int n = 0; n < channels; n++) | |
for (int i = roi_src.y; i < roi_src.y + roi_src.height; i++) | |
for (int j = roi_src.x ; j < roi_src.x + roi_src.width; j++) | |
{ | |
if(mask_gray_src.at<uchar>(i,j) == 0) | |
srcImage.at<uchar>(i,j*channels+n) = warp_rotate_src(i-roi_src.y,(j-roi_src.x)*channels+n); | |
if (i==roi_src.y || i == roi_src.y + roi_dst.height) | |
{ | |
Rect temp (j,i,3,3); | |
Mat temp_mat = srcImage(temp); | |
// blur(temp_mat,srcImage(temp),Size(3,3)); | |
} | |
if (j ==roi_src.x || j == roi_src.x + roi_src.width) | |
{ | |
Rect temp (j,i,3,3); | |
Mat temp_mat = srcImage(temp); | |
// blur(temp_mat,srcImage(temp),Size(3,3)); | |
} | |
} | |
imshow("image2",srcImage); | |
waitKey(0); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment