00001 #ifndef SEGMENT_SCT_TOOL_H
00002 #define SEGMENT_SCT_TOOL_H
00003
00004 #include <cmath>
00005 #include <iostream>
00006 #include <fstream>
00007 #include <values.h>
00008 #include <boost/numeric/ublas/matrix.hpp>
00009 #include "object.h"
00010 #include "image.h"
00011 #include "rgba.h"
00012
00013 namespace mimas {
00021 class segment_sct_tool: public object
00022 {
00023 public:
00024 typedef boost::numeric::ublas::matrix< double > Matrix;
00025
00026 private:
00027 int angsplitA, angsplitB;
00028
00029 public:
00030 segment_sct_tool(void);
00031 void segment(const image< rgba< unsigned char > > &imagein, image< rgba< unsigned char > > &imageout);
00032 void setSplitA(int sa);
00033 void setSplitB(int sb);
00034
00035
00036 };
00037
00038 segment_sct_tool::segment_sct_tool(void)
00039 {
00040 angsplitA=angsplitB=2;
00041 }
00042
00043 void segment_sct_tool::setSplitA(int sa)
00044 {
00045 angsplitA=sa;
00046 }
00047
00048 void segment_sct_tool::setSplitB(int sb)
00049 {
00050 angsplitB=sb;
00051 }
00052
00053 void segment_sct_tool::segment(const image< rgba< unsigned char > > &imagein, image< rgba< unsigned char > > &imageout)
00054 {
00055 image<int> imagetemp;
00056 int w=imagein.getWidth();
00057 int h=imagein.getHeight();
00058 double l;
00059 Matrix angA(h,w);
00060 Matrix angB(h,w);
00061 double minA,maxA,minB,maxB;
00062
00063
00064 if (imageout.initialised())
00065 {
00066 if ((imageout.getWidth()!=imagein.getWidth()) || (imageout.getHeight()!=imagein.getHeight()))
00067 imageout.clear();
00068 }
00069
00070 if (!imageout.initialised())
00071 imageout.init(w,h);
00072
00073 imagetemp.init(w,h);
00074
00075
00076 minA=MAXDOUBLE;
00077 minB=MAXDOUBLE;
00078 maxA=-MAXDOUBLE;
00079 maxB=-MAXDOUBLE;
00080
00081 for (int j=0; j<h; j++)
00082 for (int i=0; i<w; i++)
00083 {
00084 const rgba< unsigned char > *pixel = &imagein.getPixel( i,j );
00085 double r = (double)pixel->getRed();
00086 double g = (double)pixel->getGreen();
00087 double b = (double)pixel->getBlue();
00088
00089 l=sqrt (r*r + g*g + b*b);
00090
00091 angA(j,i)=acos(b/l);
00092 angB(j,i)=acos(r/(l*sin(angA(j,i))));
00093
00094 if (angA(j,i) > maxA) maxA=angA(j,i);
00095 if (angB(j,i) > maxB) maxB=angB(j,i);
00096 if (angA(j,i) < minA) minA=angA(j,i);
00097 if (angB(j,i) < minB) minB=angB(j,i);
00098
00099 imagetemp.setPixel(i,j,0);
00100
00101 }
00102
00103 int checkA=0, checkB=0, segcount=0;
00104 for (double sa=minA; sa<maxA; sa+=(maxA-minA)/angsplitA)
00105 {
00106 checkA++;
00107 if (checkA>angsplitA) continue;
00108
00109 checkB=0;
00110 for (double sb=minB; sb<maxB; sb+=(maxB-minB)/angsplitB)
00111 {
00112 checkB++;
00113 if (checkB>angsplitB) continue;
00114
00115
00116 for (int j=0; j<h; j++)
00117 for (int i=0; i<w; i++)
00118 if ((angA(j,i)>=sa) && (angB(j,i)>=sb))
00119 imagetemp.setPixel(i,j,segcount);
00120
00121 segcount++;
00122
00123 }
00124 }
00125
00126 int aveR[angsplitA*angsplitB];
00127 int aveG[angsplitA*angsplitB];
00128 int aveB[angsplitA*angsplitB];
00129
00130
00131 for (segcount=0; segcount<angsplitA*angsplitB; segcount++)
00132 {
00133 int counter=0;
00134 aveR[segcount]=0;
00135 aveG[segcount]=0;
00136 aveB[segcount]=0;
00137
00138 for (int j=0; j<h; j++)
00139 for (int i=0; i<w; i++)
00140 if (imagetemp.getPixel(i,j)==segcount)
00141 {
00142 const rgba< unsigned char > *pixel = &imagein.getPixel( i,j );
00143 int r = pixel->getRed();
00144 int g = pixel->getGreen();
00145 int b = pixel->getBlue();
00146
00147 aveR[segcount]+=r;
00148 aveG[segcount]+=g;
00149 aveB[segcount]+=b;
00150 counter++;
00151 }
00152
00153 if (counter==0) continue;
00154
00155 aveR[segcount]=aveR[segcount]/counter;
00156 aveG[segcount]=aveG[segcount]/counter;
00157 aveB[segcount]=aveB[segcount]/counter;
00158 }
00159
00160 for (segcount=0; segcount<angsplitA*angsplitB; segcount++)
00161 {
00162 for (int j=0; j<h; j++)
00163 for (int i=0; i<w; i++)
00164 if (imagetemp.getPixel(i,j)==segcount)
00165 {
00166 rgba<int> pixcol;
00167 pixcol.setRed(aveR[segcount]);
00168 pixcol.setGreen(aveG[segcount]);
00169 pixcol.setBlue(aveB[segcount]);
00170 imageout.setPixel(i,j,pixcol);
00171 }
00172 }
00173
00174 #ifndef NDEBUG
00175 std::cerr << std::endl << minA << std::endl;
00176 std::cerr << maxA << std::endl;
00177 std::cerr << minB << std::endl;
00178 std::cerr << maxB << std::endl;
00179 std::cerr << checkA << std::endl;
00180 std::cerr << checkB << std::endl;
00181 std::cerr << segcount << std::endl;
00182 #endif
00183
00184 }
00185
00186 }
00187
00188 #endif