cv::Scalar SSIM::computeSSIM(const cv::Mat& img1, const cv::Mat& img2) { int ht = img1.rows; int wt = img1.cols; int w = wt - 10; int h = ht - 10; cv::Mat mu1(h,w,CV_32F), mu2(h,w,CV_32F); cv::Mat mu1_sq(h,w,CV_32F), mu2_sq(h,w,CV_32F), mu1_mu2(h,w,CV_32F); cv::Mat img1_sq(ht,wt,CV_32F), img2_sq(ht,wt,CV_32F), img1_img2(ht,wt,CV_32F); cv::Mat sigma1_sq(h,w,CV_32F), sigma2_sq(h,w,CV_32F), sigma12(h,w,CV_32F); cv::Mat tmp1(h,w,CV_32F), tmp2(h,w,CV_32F), tmp3(h,w,CV_32F); cv::Mat ssim_map(h,w,CV_32F), cs_map(h,w,CV_32F); // mu1 = filter2(window, img1, 'valid'); applyGaussianBlur(img1, mu1, 11, 1.5); // mu2 = filter2(window, img2, 'valid'); applyGaussianBlur(img2, mu2, 11, 1.5); // mu1_sq = mu1.*mu1; cv::multiply(mu1, mu1, mu1_sq); // mu2_sq = mu2.*mu2; cv::multiply(mu2, mu2, mu2_sq); // mu1_mu2 = mu1.*mu2; cv::multiply(mu1, mu2, mu1_mu2); cv::multiply(img1, img1, img1_sq); cv::multiply(img2, img2, img2_sq); cv::multiply(img1, img2, img1_img2); // sigma1_sq = filter2(window, img1.*img1, 'valid') - mu1_sq; applyGaussianBlur(img1_sq, sigma1_sq, 11, 1.5); sigma1_sq -= mu1_sq; // sigma2_sq = filter2(window, img2.*img2, 'valid') - mu2_sq; applyGaussianBlur(img2_sq, sigma2_sq, 11, 1.5); sigma2_sq -= mu2_sq; // sigma12 = filter2(window, img1.*img2, 'valid') - mu1_mu2; applyGaussianBlur(img1_img2, sigma12, 11, 1.5); sigma12 -= mu1_mu2; // cs_map = (2*sigma12 + C2)./(sigma1_sq + sigma2_sq + C2); tmp1 = 2*sigma12 + C2; tmp2 = sigma1_sq + sigma2_sq + C2; cv::divide(tmp1, tmp2, cs_map); // ssim_map = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))./((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2)); tmp3 = 2*mu1_mu2 + C1; cv::multiply(tmp1, tmp3, tmp1); tmp3 = mu1_sq + mu2_sq + C1; cv::multiply(tmp2, tmp3, tmp2); cv::divide(tmp1, tmp2, ssim_map); // mssim = mean2(ssim_map); double mssim = cv::mean(ssim_map).val[0]; // mcs = mean2(cs_map); double mcs = cv::mean(cs_map).val[0]; cv::Scalar res(mssim, mcs); return res; }
/* * Given 2 lat/longs and ellipse, find the distance * note original r = 1st, s=2nd location */ double distance_ellipse_calculation(double lat1, double long1, double lat2, double long2, SPHEROID *sphere) { /* * Code is taken from David Skea * Geographic Data BC, Province of British Columbia, Canada. * Thanks to GDBC and David Skea for allowing this to be * put in PostGIS. */ double L1,L2,sinU1,sinU2,cosU1,cosU2; double dl,dl1,dl2,dl3,cosdl1,sindl1; double cosSigma,sigma,azimuthEQ,tsm; double u2,A,B; double dsigma; double TEMP; int iterations; L1 = atan((1.0 - sphere->f ) * tan( lat1) ); L2 = atan((1.0 - sphere->f ) * tan( lat2) ); sinU1 = sin(L1); sinU2 = sin(L2); cosU1 = cos(L1); cosU2 = cos(L2); dl = long2- long1; dl1 = dl; cosdl1 = cos(dl); sindl1 = sin(dl); iterations = 0; do { cosSigma = sinU1 * sinU2 + cosU1 * cosU2 * cosdl1; sigma = acos(cosSigma); azimuthEQ = asin((cosU1 * cosU2 * sindl1)/sin(sigma)); /* * Patch from Patrica Tozer to handle minor * mathematical stability problem */ TEMP = cosSigma - (2.0 * sinU1 * sinU2)/ (cos(azimuthEQ)*cos(azimuthEQ)); if (TEMP > 1) { TEMP = 1; } else if (TEMP < -1) { TEMP = -1; } tsm = acos(TEMP); /* (old code?) tsm = acos(cosSigma - (2.0 * sinU1 * sinU2)/(cos(azimuthEQ)*cos(azimuthEQ))); */ dl2 = deltaLongitude(azimuthEQ, sigma, tsm,sphere); dl3 = dl1 - (dl + dl2); dl1 = dl + dl2; cosdl1 = cos(dl1); sindl1 = sin(dl1); iterations++; } while ( (iterations<999) && (fabs(dl3) > 1.0e-032)); /* compute expansions A and B */ u2 = mu2(azimuthEQ,sphere); A = bigA(u2); B = bigB(u2); /* compute length of geodesic */ dsigma = B * sin(sigma) * (cos(tsm) + (B*cosSigma*(-1.0 + 2.0 * (cos(tsm)*cos(tsm))))/4.0); return sphere->b * (A * (sigma - dsigma)); }
RcppExport SEXP nsem3b(SEXP data, SEXP theta, SEXP Sigma, SEXP modelpar, SEXP control ) { // srand ( time(NULL) ); /* initialize random seed: */ Rcpp::NumericVector Theta(theta); Rcpp::NumericMatrix D(data); unsigned nobs = D.nrow(), k = D.ncol(); mat Data(D.begin(), nobs, k, false); // Avoid copying Rcpp::NumericMatrix V(Sigma); mat S(V.begin(), V.nrow(), V.ncol()); S(0,0) = 1; mat iS = inv(S); double detS = det(S); Rcpp::List Modelpar(modelpar); // Rcpp::IntegerVector _nlatent = Modelpar["nlatent"]; unsigned nlatent = _nlatent[0]; Rcpp::IntegerVector _ny0 = Modelpar["nvar0"]; unsigned ny0 = _ny0[0]; Rcpp::IntegerVector _ny1 = Modelpar["nvar1"]; unsigned ny1 = _ny1[0]; Rcpp::IntegerVector _ny2 = Modelpar["nvar2"]; unsigned ny2 = _ny2[0]; Rcpp::IntegerVector _npred0 = Modelpar["npred0"]; unsigned npred0 = _npred0[0]; Rcpp::IntegerVector _npred1 = Modelpar["npred1"]; unsigned npred1 = _npred1[0]; Rcpp::IntegerVector _npred2 = Modelpar["npred2"]; unsigned npred2 = _npred2[0]; Rcpp::List Control(control); Rcpp::NumericVector _lambda = Control["lambda"]; double lambda = _lambda[0]; Rcpp::NumericVector _niter = Control["niter"]; double niter = _niter[0]; Rcpp::NumericVector _Dtol = Control["Dtol"]; double Dtol = _Dtol[0]; rowvec mu0(ny0), lambda0(ny0); rowvec mu1(ny1), lambda1(ny1); rowvec mu2(ny2), lambda2(ny2); rowvec beta0(npred0); rowvec beta1(npred1); rowvec beta2(npred2); rowvec gamma(2); rowvec gamma2(2); unsigned pos=0; for (unsigned i=0; i<ny0; i++) { mu0(i) = Theta[pos]; pos++; } for (unsigned i=0; i<ny1; i++) { mu1(i) = Theta[pos]; pos++; } for (unsigned i=0; i<ny2; i++) { mu2(i) = Theta[pos]; pos++; } for (unsigned i=0; i<ny0; i++) { lambda0(i) = Theta[pos]; pos++; } lambda1(0) = 1; for (unsigned i=1; i<ny1; i++) { lambda1(i) = Theta[pos]; pos++; } lambda2(0) = 1; for (unsigned i=1; i<ny2; i++) { lambda2(i) = Theta[pos]; pos++; } for (unsigned i=0; i<npred0; i++) { beta0(i) = Theta[pos]; pos++; } for (unsigned i=0; i<npred1; i++) { beta1(i) = Theta[pos]; pos++; } for (unsigned i=0; i<npred2; i++) { beta2(i) = Theta[pos]; pos++; } gamma(0) = Theta[pos]; gamma(1) = Theta[pos+1]; gamma2(0) = Theta[pos+2]; gamma2(1) = Theta[pos+3]; // cerr << "mu0=" << mu0 << endl; // cerr << "mu1=" << mu1 << endl; // cerr << "mu2=" << mu2 << endl; // cerr << "lambda0=" << lambda0 << endl; // cerr << "lambda1=" << lambda1 << endl; // cerr << "lambda2=" << lambda2 << endl; // cerr << "beta0=" << beta0 << endl; // cerr << "beta1=" << beta1 << endl; // cerr << "beta2=" << beta2 << endl; // cerr << "gamma=" << gamma << endl; // cerr << "gamma2=" << gamma2 << endl; mat lap(nobs,4); for (unsigned i=0; i<nobs; i++) { rowvec newlap = laNRb(Data.row(i), iS, detS, mu0, mu1, mu2, lambda0, lambda1, lambda2, beta0,beta1, beta2, gamma, gamma2, Dtol,niter,lambda); lap.row(i) = newlap; } List res; res["indiv"] = lap; res["logLik"] = sum(lap.col(0)) + (3-V.nrow())*log(2.0*datum::pi)*nobs/2; res["norm0"] = (3-V.nrow())*log(2*datum::pi)/2; return res; }
void main() { int x,y,x0,y0; int flag=0; int flag1=1; struct node * head; struct node * head2; int m[1]={0}; int r[21]; int po1[378]={0}; char po[24]={0}; beijing1(); beijing2(); choose(po); mukuai(); suiji(r,po1); head=creat(po1); // print(head); while(1) { HANDLE hOut,hIn; DWORD Result; INPUT_RECORD Buf; hOut=GetStdHandle(STD_OUTPUT_HANDLE); hIn=GetStdHandle(STD_INPUT_HANDLE); int a[2]; do { ReadConsoleInput(hIn,&Buf,1,&Result); if(Buf.EventType==MOUSE_EVENT) { HandleMouse(Buf.Event.MouseEvent,a); x=a[0]; y=a[1]; } } while(!(Buf.EventType==MOUSE_EVENT&&Buf.Event.MouseEvent.dwEventFlags==DOUBLE_CLICK)); HANDLE hOut1,hIn1; DWORD Result1; INPUT_RECORD Buf1; hOut1=GetStdHandle(STD_OUTPUT_HANDLE); hIn1=GetStdHandle(STD_INPUT_HANDLE); int a0[2]; do { ReadConsoleInput(hIn1,&Buf1,1,&Result1); if(Buf1.EventType==MOUSE_EVENT) { HandleMouse1(Buf1.Event.MouseEvent,a0); x0=a0[0]; y0=a0[1]; } } while(!(Buf1.EventType==MOUSE_EVENT&&Buf1.Event.MouseEvent.dwEventFlags==DOUBLE_CLICK)); // 清屏的按钮 /* if(x0>=86&&x0<=100&&y0>=8&&y0<=10) { clear(r,po1,po); free(head); continue; }*/ if(x0>=74&&x0<=82&&y0>=25&&y0<=27) flag=save(head); // if(x0>=85&&x0<=93&&y0>=25&&y0<=27) // open(); if(x0>=96&&x0<=104&&y0>=25&&y0<=27) { ////// 上次运行的结果 if(flag1==1) { flag1=0; //print(head); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|BACKGROUND_GREEN|BACKGROUND_INTENSITY); COORD pos1; pos1.X=0; pos1.Y=60; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos1); } if(flag==1) { flag=0; print(head); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|BACKGROUND_GREEN|BACKGROUND_INTENSITY); COORD pos1; pos1.X=0; pos1.Y=60; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos1); break; } else if(flag==0) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_BLUE|BACKGROUND_GREEN|BACKGROUND_INTENSITY); COORD pos0; pos0.X=10; pos0.Y=32; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos0); printf(" 请您先保存 ! "); } // 保存 } if(x0>=10&&x0<=51&&y0>=7&&y0<=28) { x0=x0/2*2; // 取偶数点 if(x>=68&&x<=69&&y>=8&&y<=10) { mu1(x0,y0-2,po,1); head2=insert(po,head); // print(head2); } else if(x>=76&&x<=81&&y==9) { mu2(x0-4,y0,po,1); head2=insert(po,head); // print(head2); } else if(x>=68&&x<=73&&y>=14&&y<=16) { mu3(x0-2,y0-1,po,1); head2=insert(po,head); // print(head2); } else if(x>=76&&x<=81&&y>=14&&y<=16) { mu4(x0-2,y0-1,po,1); head2=insert(po,head); // print(head2); } else if(x>=86&&x<=91&&y>=14&&y<=16) { mu5(x0-2,y0+1,po,1); head2=insert(po,head); // print(head2); } else if(x>=98&&x<=103&&y>=14&&y<=16) { mu6(x0-5,y0,po,1); head2=insert(po,head); // print(head2); } else if(x>=68&&x<=73&&y>=20&&y<=22) { mu7(x0,y0-2,po,1); head2=insert(po,head); // print(head2); } else if(x>=76&&x<=81&&y>=20&&y<=22) { mu8(x0-4,y0-1,po,1); head2=insert(po,head); // print(head2); } else if(x>=86&&x<=91&&y>=20&&y<=22) { mu9(x0,y0-2,po,1); head2=insert(po,head); // print(head2); } else if(x>=99&&x<=104&&y>=20&&y<=22) { mu10(x0,y0,po,1); head2=insert(po,head); // print(head2); } else if(x>=68&&x<=71&&y>=26&&y<=28) { mu11(x0-2,y0-1,po,1); head2=insert(po,head); // print(head2); } } } }
void choose(char po[24]) { mu1(68,8,po,0);mu2(76,9,po,0);mu3(68,14,po,0);mu4(76,14,po,0);mu5(86,16,po,0);mu6(98,14,po,0);mu7(68,20,po,0);mu8(76,21,po,0);mu9(88,20,po,0);mu10(100,21,po,0);mu11(68,26,po,0); }
// GAUSS-LOBATTO QUADRATURE ALONG EDGE void SetEdgeDataGL_Unst(const mesh& Mesh, int NumQuadPoints, int NumBasisOrder, edge_data_Unst* EdgeData) { // Quick error check if (NumQuadPoints<2 || NumQuadPoints>6 || NumBasisOrder<1 || NumBasisOrder>5) { printf(" \n"); printf(" Error in SetEdgeData_Unst.cpp \n"); printf(" NumQuadPoints must be 2,3,4,5, or 6.\n"); printf(" NumBasisOrder must be 1,2,3,4, or 5.\n"); printf(" NumQuadPoints = %i\n",NumQuadPoints); printf(" NumBasisOrder = %i\n",NumBasisOrder); printf("\n"); exit(1); } // --------------------------------- // Set quadrature weights and points // --------------------------------- switch( NumQuadPoints ) { case 2: EdgeData->GL_wgts1d->set(1, 1.0 ); EdgeData->GL_wgts1d->set(2, 1.0 ); EdgeData->GL_xpts1d->set(1, 1.0 ); EdgeData->GL_xpts1d->set(2, -1.0 ); break; case 3: EdgeData->GL_wgts1d->set(1, onethird ); EdgeData->GL_wgts1d->set(2, 4.0*onethird ); EdgeData->GL_wgts1d->set(3, onethird ); EdgeData->GL_xpts1d->set(1, 1.0 ); EdgeData->GL_xpts1d->set(2, 0.0 ); EdgeData->GL_xpts1d->set(3, -1.0 ); break; case 4: EdgeData->GL_wgts1d->set(1, 0.5*onethird ); EdgeData->GL_wgts1d->set(2, 2.5*onethird ); EdgeData->GL_wgts1d->set(3, 2.5*onethird ); EdgeData->GL_wgts1d->set(4, 0.5*onethird ); EdgeData->GL_xpts1d->set(1, 1.0 ); EdgeData->GL_xpts1d->set(2, osq5 ); EdgeData->GL_xpts1d->set(3, -osq5 ); EdgeData->GL_xpts1d->set(4, -1.0 ); break; case 5: EdgeData->GL_wgts1d->set(1, 0.1 ); EdgeData->GL_wgts1d->set(2, 49.0/90.0 ); EdgeData->GL_wgts1d->set(3, 32.0/45.0 ); EdgeData->GL_wgts1d->set(4, 49.0/90.0 ); EdgeData->GL_wgts1d->set(5, 0.1 ); EdgeData->GL_xpts1d->set(1, 1.0 ); EdgeData->GL_xpts1d->set(2, sq3*osq7 ); EdgeData->GL_xpts1d->set(3, 0.0 ); EdgeData->GL_xpts1d->set(4, -sq3*osq7 ); EdgeData->GL_xpts1d->set(5, -1.0 ); break; case 6: EdgeData->GL_wgts1d->set(1, 0.2*onethird ); EdgeData->GL_wgts1d->set(2, (1.4 - 0.1*sq7)*onethird ); EdgeData->GL_wgts1d->set(3, (1.4 + 0.1*sq7)*onethird ); EdgeData->GL_wgts1d->set(4, (1.4 + 0.1*sq7)*onethird ); EdgeData->GL_wgts1d->set(5, (1.4 - 0.1*sq7)*onethird ); EdgeData->GL_wgts1d->set(6, 0.2*onethird ); EdgeData->GL_xpts1d->set(1, 1.0 ); EdgeData->GL_xpts1d->set(2, (1/21.0)*sqrt(147.0+42.0*sq7) ); EdgeData->GL_xpts1d->set(3, (1/21.0)*sqrt(147.0-42.0*sq7) ); EdgeData->GL_xpts1d->set(4, -(1/21.0)*sqrt(147.0-42.0*sq7) ); EdgeData->GL_xpts1d->set(5, -(1/21.0)*sqrt(147.0+42.0*sq7) ); EdgeData->GL_xpts1d->set(6, -1.0 ); break; } // --------------------------------- // Legendre basis functions on the // left and right of each edge // --------------------------------- const int NumEdges = Mesh.get_NumEdges(); const int NumBasisComps = (NumBasisOrder*(NumBasisOrder+1))/2; dTensor1 xp1(3); dTensor1 yp1(3); dTensor1 xp2(3); dTensor1 yp2(3); dTensor1 xy1(2); dTensor1 xy2(2); dTensor1 mu1(NumBasisComps); dTensor1 mu2(NumBasisComps); for (int i=1; i<=NumEdges; i++) { // Get edge information const double x1 = Mesh.get_edge(i,1); const double y1 = Mesh.get_edge(i,2); const double x2 = Mesh.get_edge(i,3); const double y2 = Mesh.get_edge(i,4); const int e1 = Mesh.get_eelem(i,1); const int e2 = Mesh.get_eelem(i,2); // Get element information about // the two elements that meet at // the current edge const double Area1 = Mesh.get_area_prim(e1); const double Area2 = Mesh.get_area_prim(e2); for (int k=1; k<=3; k++) { xp1.set(k, Mesh.get_node(Mesh.get_tnode(e1,k),1) ); yp1.set(k, Mesh.get_node(Mesh.get_tnode(e1,k),2) ); xp2.set(k, Mesh.get_node(Mesh.get_tnode(e2,k),1) ); yp2.set(k, Mesh.get_node(Mesh.get_tnode(e2,k),2) ); } const double xc1 = (xp1.get(1) + xp1.get(2) + xp1.get(3))/3.0; const double yc1 = (yp1.get(1) + yp1.get(2) + yp1.get(3))/3.0; const double xc2 = (xp2.get(1) + xp2.get(2) + xp2.get(3))/3.0; const double yc2 = (yp2.get(1) + yp2.get(2) + yp2.get(3))/3.0; // quadrature points on the edge for (int m=1; m<=NumQuadPoints; m++) { // Take integration point s (in [-1,1]) // and map to physical domain const double s = EdgeData->GL_xpts1d->get(m); const double x = x1 + 0.5*(s+1.0)*(x2-x1); const double y = y1 + 0.5*(s+1.0)*(y2-y1); // Take physical point (x,y) // and map into the coordinates // of the two triangles that are // adjacent to the current edge xy1.set(1, ((yp1.get(3)-yp1.get(1))*(x-xc1) + (xp1.get(1)-xp1.get(3))*(y-yc1))/(2.0*Area1) ); xy1.set(2, ((yp1.get(1)-yp1.get(2))*(x-xc1) + (xp1.get(2)-xp1.get(1))*(y-yc1))/(2.0*Area1) ); xy2.set(1, ((yp2.get(3)-yp2.get(1))*(x-xc2) + (xp2.get(1)-xp2.get(3))*(y-yc2))/(2.0*Area2) ); xy2.set(2, ((yp2.get(1)-yp2.get(2))*(x-xc2) + (xp2.get(2)-xp2.get(1))*(y-yc2))/(2.0*Area2) ); // Evaluate monomials at locations xy1 double xi = xy1.get(1); double xi2 = xi*xi; double xi3 = xi*xi2; double xi4 = xi*xi3; double eta = xy1.get(2); double eta2 = eta*eta; double eta3 = eta*eta2; double eta4 = eta*eta3; switch( NumBasisOrder ) { case 5: // fifth order mu1.set(15, eta4 ); mu1.set(14, xi4 ); mu1.set(13, xi2*eta2 ); mu1.set(12, eta3*xi ); mu1.set(11, xi3*eta ); case 4: // fourth order mu1.set(10, eta3 ); mu1.set(9, xi3 ); mu1.set(8, xi*eta2 ); mu1.set(7, eta*xi2 ); case 3: // third order mu1.set(6, eta2 ); mu1.set(5, xi2 ); mu1.set(4, xi*eta ); case 2: // second order mu1.set(3, eta ); mu1.set(2, xi ); case 1: // first order mu1.set(1, 1.0 ); break; } // Evaluate monomials at locations xy2 xi = xy2.get(1); xi2 = xi*xi; xi3 = xi*xi2; xi4 = xi*xi3; eta = xy2.get(2); eta2 = eta*eta; eta3 = eta*eta2; eta4 = eta*eta3; switch( NumBasisOrder ) { case 5: // fifth order mu2.set(15, eta4 ); mu2.set(14, xi4 ); mu2.set(13, xi2*eta2 ); mu2.set(12, eta3*xi ); mu2.set(11, xi3*eta ); case 4: // fourth order mu2.set(10, eta3 ); mu2.set(9, xi3 ); mu2.set(8, xi*eta2 ); mu2.set(7, eta*xi2 ); case 3: // third order mu2.set(6, eta2 ); mu2.set(5, xi2 ); mu2.set(4, xi*eta ); case 2: // second order mu2.set(3, eta ); mu2.set(2, xi ); case 1: // first order mu2.set(1, 1.0 ); break; } // Finally, convert monomials to Legendre Polys // on the two adjacent triangle for (int k=1; k<=NumBasisComps; k++) { double tmp1 = 0.0; double tmp2 = 0.0; for (int j=1; j<=k; j++) { tmp1 = tmp1 + Mmat[k-1][j-1]*mu1.get(j); tmp2 = tmp2 + Mmat[k-1][j-1]*mu2.get(j); } EdgeData->GL_phi_left->set(i,m,k, tmp1 ); EdgeData->GL_phi_right->set(i,m,k, tmp2 ); } } } }