diff --git a/fastlib/trunk/contrib/tqlong/affineNMF/image_type.cc b/fastlib/trunk/contrib/tqlong/affineNMF/image_type.cc new file mode 100644 index 0000000000..a97020c6e5 --- /dev/null +++ b/fastlib/trunk/contrib/tqlong/affineNMF/image_type.cc @@ -0,0 +1,34 @@ + +#include +#include "image_type.h" + +double ImageType::Difference(const ImageType& image, + double (*kernel)(const PointType&, + const PointType&) ) const { + double s = 0; + for (index_t i = 0; i < pList.size(); i++) + for (index_t j = 0; j < image.pList.size(); j++) + s += kernel(pList[i], image.pList[j]); + return s; +} + +double exp_kernel(const PointType& p1, const PointType& p2) { + double sigma2 = 1; + double s = (p1.r-p2.r)*(p1.r-p2.r) + (p1.c-p2.c)*(p1.c-p2.c); + return exp(-0.5/sigma2*s) * (p1.f-p2.f)*(p1.f-p2.f); +} + +void ImageType::Scale(ImageType& image_out, double s) { + image_out.pList.Renew(); + image_out.pList.InitCopy(pList); + for (index_t i = 0; i < pList.size(); i++) + image_out.pList[i].f *= s; +} + +void ImageType::Transform(ImageType& image_out, + const Transformation& t, double s) { + image_out.pList.Renew(); + image_out.pList.Init(pList.size()); + for (index_t i = 0; i < pList.size(); i++) + image_out.pList[i] = pList[i].Transform(t, s); +} diff --git a/fastlib/trunk/contrib/tqlong/affineNMF/image_type.h b/fastlib/trunk/contrib/tqlong/affineNMF/image_type.h new file mode 100644 index 0000000000..b3618834ea --- /dev/null +++ b/fastlib/trunk/contrib/tqlong/affineNMF/image_type.h @@ -0,0 +1,61 @@ +#pragma once + +/** image_type.h + **/ + +struct Transformation { + Vector m; + Transformation() { + m.Init(8); + m[0] = 1; m[1] = 0; m[2] = 0; + m[3] = 0; m[4] = 1; m[5] = 0; + m[6] = 0; m[7] = 0; + } + Transformation(const Transformation& t) { + m.Copy(t.m); + } +}; + +struct PointType { + double r; // row + double c; // col + double f; // feature value or intensity + + PointType Transform(const Transformation& t, double s = 1.0) const { + PointType newPoint; + double d = t.m[6]*r + t.m[7]*c + 1; + newPoint.r = (t.m[0]*r + t.m[1]*c + t.m[2])/d; + newPoint.c = (t.m[3]*r + t.m[4]*c + t.m[5])/d; + newPoint.f = f * s; + return newPoint; + } +}; + +double exp_kernel(const PointType&, const PointType&); + +struct ImageType { + ArrayList pList; + + // Members + ImageType() { + pList.Init(); + } + + ImageType(const ImageType& image) { + pList.InitCopy(image.pList); + } + + void Add(const ImageType& image) { + pList.AppendCopy(image.pList); + } + + double Difference(const ImageType& image, + double (*kernel)(const PointType&, + const PointType&) = exp_kernel) const; + + void Scale(ImageType& image_out, double s = 1.0); + + void Transform(ImageType& image_out, const Transformation& t, double s = 1.0); + +}; +