T operator()(const Array2D<T>& src, const XYScale& tr, const typename XYScale::point& p) { int nx = p.ix(); int ny = p.iy(); double v = src.value(nx, ny); double a=0; if (nx==0||nx==src.nj-1) return (T)v; if (ny==0||ny==src.ni-1) return (T)v; if (nx<src.nj-1) { double x0 = tr.ax.value(nx); double x1 = tr.ax.value(nx+1); a = (p.x()-x0)/(x1-x0); v = (1-a)*v+a*src.value(nx+1,ny); } if (ny>=src.ni-1) return (T)v; double v2 = src.value(nx,ny+1); double y0 = tr.ay.value(ny); double y1 = tr.ay.value(ny+1); double b = (p.y()-y0)/(y1-y0); if (nx<src.nj-1) { v2 = (1-a)*v2+a*src.value(nx+1,ny+1); } return (T)(v*(1-b)+b*v2); }
T operator()(const Array2D<T>& src, const TR& tr, const typename TR::point& p) { int nx = p.ix(); int ny = p.iy(); double v = src.value(nx, ny); double a=0; // The following couple of lines were commented out to avoid disabling // the linear interpolation on image edges. Demonstrating the effect of // this change is quite easy: just try to show a very small image // (e.g. 10x10) with guiqwt.pyplot.imshow for example. // if (nx==0||nx==src.nj-1) return (T)v; // if (ny==0||ny==src.ni-1) return (T)v; if (nx<src.nj-1) { a = p.x()-nx; v = (1-a)*v+a*src.value(nx+1,ny); } if (ny>=src.ni-1) return (T)v; double v2 = src.value(nx,ny+1); double b = p.y()-ny; if (nx<src.nj-1) { v2 = (1-a)*v2+a*src.value(nx+1,ny+1); } return (T)(v*(1-b)+b*v2); }
Array2D matrix_multiply(const Array2D &M1,const Array2D &M2) { if (M1.height()!=M2.width()) return Array2D(); Array2D ret(M1.width(),M2.height()); for (int j=0; j<ret.height(); j++) for (int i=0; i<ret.width(); i++) { double val=0; for (int k=0; k<M1.height(); k++) { val+=M1.value(i,k)*M2.value(k,j); } ret.setValue(val,i,j); } return ret; }
T operator()(const Array2D<T>& src, const TR& tr, const typename TR::point& p0) { int i,j; typename TR::point p, p1; typename num_trait<T>::large_type value = 0; typename num_trait<T>::large_type count=0, msk, val; p1.copy(p0); tr.incy(p1,-0.5); tr.incx(p1,-0.5); for(i=0;i<mask.ni;++i) { p.copy(p1); for(j=0;j<mask.nj;++j) { if (p.inside()) { msk = mask.value(j,i); val = src.value(p.ix(), p.iy()); value += msk*val; count += msk; //printf("i,j=%d,%d : %f,%f / %d,%d = %lfx%lf\n", i, j, p.x(), p.y(), p.ix(), p.iy(), (double)val, (double)msk ); } tr.incx(p,kj); } tr.incy(p1,ki); } //printf("%d/%d\n", (int)value, (int)count); if (count) return value/count; else return value; }
int ssfeatures(FILE* infile, FILE* outfile, const QMap<QString, QVariant>& params) { int nfeatures = params["nfeatures"].toInt(); int niterations = params["niterations"].toInt(); //get the input file header MDAIO_HEADER HH_infile; if (!mda_read_header(&HH_infile, infile)) return 0; int32_t M = HH_infile.dims[0]; int32_t T = HH_infile.dims[1]; int32_t N = HH_infile.dims[2]; if (M <= 0) return 0; if (T <= 0) return 0; if (N <= 0) return 0; Array2D X; X.allocate(M * T, N); float* inbuf = (float*)malloc(sizeof(float) * M * T); for (int i = 0; i < N; i++) { mda_read_float32(inbuf, &HH_infile, M * T, infile); for (int j = 0; j < M * T; j++) { X.setValue(inbuf[j], j, i); } } free(inbuf); PCASolver SS; SS.setVectors(X); SS.setNumIterations(niterations); SS.setComponentCount(nfeatures); SS.solve(); Array2D features = SS.coefficients(); //write the output header MDAIO_HEADER HH_outfile; mda_copy_header(&HH_outfile, &HH_infile); HH_outfile.num_dims = 2; HH_outfile.dims[0] = nfeatures; HH_outfile.dims[1] = N; HH_outfile.dims[2] = 1; HH_outfile.data_type = MDAIO_TYPE_FLOAT32; mda_write_header(&HH_outfile, outfile); float* outbuf = (float*)malloc(sizeof(float) * nfeatures); for (int i = 0; i < N; i++) { for (int j = 0; j < nfeatures; j++) { outbuf[j] = features.value(j, i); } mda_write_float32(outbuf, &HH_outfile, nfeatures, outfile); } free(outbuf); return 1; }
npy_uint32 operator()(const Array2D<npy_uint32>& src, const TR& tr, const typename TR::point& p) { int k; int nx = p.ix(); int ny = p.iy(); rgba_t p1, p2, p3, p4, r; p1.v = src.value(nx, ny); float v[4], v2[4]; double a=0; if (nx<src.nj-1) { p2.v = src.value(nx+1,ny); a = p.x()-nx; for(k=0;k<4;++k) { v[k] = (1-a)*p1.c[k]+a*p2.c[k]; } } else { for(k=0;k<4;++k) { v[k] = p1.c[k]; } } if (ny>=src.ni-1) { for(k=0;k<4;++k) { r.c[k] = (npy_uint8)(v[k]); } return r.v; } p3.v = src.value(nx,ny+1); double b = p.y()-ny; if (nx<src.nj-1) { p4.v = src.value(nx+1,ny+1); for(k=0;k<4;++k) { v2[k] = (1-a)*p3.c[k]+a*p4.c[k]; } } else { for(k=0;k<4;++k) { v2[k] = p3.c[k]; } } for(k=0;k<4;++k) { float px = v[k]*(1-b)+b*v2[k]; if (px<0.0) px = 0.0; if (px>255.0) px = 255.0; r.c[k] = (npy_uint8)px; } return r.v; }
static bool vert_line(double _x0, double _y0, double _x1, double _y1, int NX, vector<int>& imin, vector<int>& imax, bool draw, npy_uint32 col, Array2D<npy_uint32>& D) { int x0 = lrint(_x0); int y0 = lrint(_y0); int x1 = lrint(_x1); int y1 = lrint(_y1); int dx = abs(x1-x0); int dy = abs(y1-y0); int sx, sy; int NY=imin.size()-1; int err, e2; bool visible=false; NX = NX-1; if (x0 < x1) sx = 1; else sx = -1; if (y0 < y1) sy = 1; else sy = -1; err = dx-dy; do { if (y0>=0 && y0<=NY) { int _min = min(imin[y0],x0); int _max = max(imax[y0],x0); if (draw) { if (x0>=0 && x0<=NX) { D.value(x0,y0) = col; } } imin[y0] = max( 0,_min); imax[y0] = min(NX,_max); if (_min<=NX && _max>=0) { visible=true; } } if ((x0 == x1) && (y0 == y1)) break; e2 = 2*err; if (e2 > -dy) { err = err - dy; x0 = x0 + sx; } if (e2 < dx) { err = err + dx; y0 = y0 + sy; } } while(true); return visible; }
T operator()(const Array2D<T>& src, const TR& tr, const typename TR::point& p) { int nx = p.ix(); int ny = p.iy(); double v = src.value(nx, ny); double a=0; if (nx==0||nx==src.nj-1) return src.value(nx,ny); if (ny==0||ny==src.ni-1) return src.value(nx,ny); v0 = interp(src.value(nx-1,ny-1)); if (nx<src.nj-1) { a = p.x()-nx; v = (1-a)*v+a*src.value(nx+1,ny); } if (ny>=src.ni-1) return v; double v2 = src.value(nx,ny+1); double b = p.y()-ny; if (nx<src.nj-1) { v2 = (1-a)*v2+a*src.value(nx+1,ny+1); } return v*(1-b)+b*v2; }
void Array2D::add(const Array2D &X) { for (int y=0; y<height(); y++) for (int x=0; x<width(); x++) { setValue(value(x,y)+X.value(x,y),x,y); } }
T operator()(const Array2D<T>& src, const TR& tr, const typename TR::point& p) { int nx = p.ix(); int ny = p.iy(); return src.value(nx, ny); }