00001 #include "ImageCalculator.h" 00002 #include <iostream> 00003 #include <sstream> 00004 #include <fstream> 00005 00006 00007 ImageCalculator::ImageCalculator(GLIMSProject *proj){ 00008 mProj = proj; 00009 mImage = ImageInterface::getInterface(mProj); 00010 } 00011 00012 ImageCalculator::~ImageCalculator(){ 00013 if (mImage != NULL) { 00014 mImage->ImageInterface::destroyInterface(); 00015 } 00016 } 00017 00018 void ImageCalculator::calculate(std::string eq){ 00019 calculate(eq, -1); 00020 } 00021 00022 void ImageCalculator::calculate(std::string eq, int lineStyle){ 00023 //create an arithmatic cube with the proper dimentions 00024 ImageArithmaticCube mathCube(0, 0, mImage->getWidth(), mImage->getHeight()); 00025 00026 //run through and get only the bands needed for the calculation 00027 for(int k=0; k < mImage->getBands(); k++){ 00028 //put together a band name 00029 std::stringstream bandname; 00030 bandname << "b" << k; 00031 00032 //see if the current name is in the equation 00033 if(eq.find(bandname.str(), 0) != std::string::npos){ 00034 //if it's in the equation, get it and add it 00035 std::vector<double>* layers = mImage->getSliceScaled(0, 0, k, (mImage->getWidth())-1, (mImage->getHeight())-1, k); 00036 mathCube.addBand(layers, k); 00037 } 00038 } 00039 00040 //set the equation in the arithmaticCube 00041 mathCube.setEquation(eq); 00042 00043 //calculate and get results 00044 std::vector<double>* results = mathCube.calculateLayer(); 00045 if (results == NULL) return ; 00046 00047 // debugging - write out raw image 00048 fstream calcdFile ; 00049 calcdFile.open ("imageCalcCalculate.raw", fstream::out | fstream::binary) ; 00050 if (calcdFile.is_open()) { 00051 for (int hei = 0 ; hei < mImage->getHeight() ; hei++) { 00052 for (int wid = 0 ; wid < mImage->getWidth() ; wid++) { 00053 calcdFile << (int) ((*results)[(mImage->getWidth())*hei + wid]) ; 00054 } 00055 } 00056 calcdFile.close() ; 00057 } 00058 // end debugging 00059 00060 //create a shapeFinder 00061 ImageShapeFinder shapes(0, 0, mImage->getWidth(), mImage->getHeight(), results); 00062 shapes.setAlgorithm(algorithm) ; 00063 00064 00065 //create a vector of selectable values, this is just 1 for us 00066 std::vector<double> sel(1); 00067 sel[0] = 1; 00068 00069 00070 //set the selected values to the created vector 00071 shapes.setSelectedValues(sel); 00072 00073 00074 //do calculations and get the selections 00075 //std::vector<ImageSelectionCube> sh = shapes->getSelections(); 00076 std::vector<ImageSelectionCube*> sh = shapes.getSelections(); 00077 00078 00079 00080 /************************************************************* 00081 * This code is more or less copied from the Threshold tool. 00082 * I will try to explain it, but I'm not always sure. 00083 */ 00084 00085 //get the line data - for attempt at changing line color 00086 GLIMSLineData &ldata = mProj->getDataset()->getLineData(); 00087 //get the linedefset - for attempt at changing line color 00088 LineDefSet mLDFSet = ldata.getLDFSet(); 00089 00090 //create a vector to store the lines in 00091 std::vector<GlacierLine> * glineset = new std::vector<GlacierLine> ; 00092 00093 //for each selection from the imageSelector 00094 for(unsigned int iline = 0; iline < sh.size(); iline++ ){ 00095 00096 //get the points in this selection 00097 std::vector<ImageCube::ThreeDPoint> line = sh[iline]->getPoints(); 00098 00099 //create a glacier line args are for color change 00100 GlacierLine * gline = new GlacierLine(-1, lineStyle); 00101 00102 //get the nodeset from the line we just created 00103 std::vector<Node> &ns = gline->nodeset() ; 00104 00105 //node for adding vals 00106 Node * n = new Node ; 00107 00108 //for each point in the selection 00109 for( unsigned int ipnt = 0; ipnt < line.size(); ipnt++ ) { 00110 if (line[ipnt].x >= 0 && line[ipnt].y >= 0) { 00111 //set the node x and y (add 0.5 to shift to center of pixel) 00112 n->x = line[ipnt].x+0.5; 00113 n->y = line[ipnt].y+0.5; 00114 00115 //translate x and y to lat and long 00116 mImage->getLL( 0, n->x, n->y ); 00117 00118 //add the node 00119 ns.push_back( *n ); 00120 } 00121 } 00122 00123 if(lineStyle >=0){ 00124 //these next 5 lines are for changing line color 00125 DspAttr attr = gline->getAttr(); 00126 attr.color = GLIMSGlobals::AVAILCOLORS[ mLDFSet[lineStyle].mColor ]; 00127 attr.style = (Qt::PenStyle)mLDFSet[lineStyle].mStyle; 00128 attr.width = mLDFSet[lineStyle].mWidth; 00129 gline->setAttr(attr); 00130 } 00131 00132 00133 //add the line 00134 glineset->push_back( *gline ); 00135 } 00136 00137 00138 //add the set of lines to the lineData 00139 ldata.addLineSetLL( *glineset ); 00140 00141 //repaint 00142 mProj->getViewSet()->repaintViews(); 00143 00144 /*end of code from the Threashold tool 00145 *********************************************************/ 00146 00147 00148 //delete the shape files 00149 // for (unsigned int i = 0; i<sh.size(); i++){ 00150 // delete sh[i]; 00151 // } 00152 } 00153 00154 void ImageCalculator::testDriver(){ 00155 00156 calculate("b0==255"); 00157 return; 00158 00159 // QMessageBox::information( 0, "ImageCalculatorDialog::test", "in doCalc" ); 00160 00161 ImageArithmaticCube cube(0, 0, 10, 10); 00162 std::vector<double> temp(300); 00163 for (int i=0; i<100; i++){ 00164 temp[i] = 5; 00165 } 00166 00167 for(int j=100; j<200; j++){ 00168 temp[j] = j; 00169 } 00170 00171 for (int k=200; k<300; k++){ 00172 temp[k] = 10; 00173 } 00174 00175 // QMessageBox::information( 0, "ImageCalculatorDialog::test", "setup layers"); 00176 00177 00178 cube.addBands(&temp); 00179 cube.setEquation("130<(b1+b2+b0)"); 00180 00181 // QMessageBox::information( 0, "ImageCalculatorDialog::test", "before calc" ); 00182 00183 00184 std::vector<double>* res = cube.calculateLayer(); 00185 00186 // QMessageBox::information( 0, "ImageCalculatorDialog::test", "after calc" ); 00187 00188 00189 ImageShapeFinder shapes = ImageShapeFinder(0, 0, 10, 10, res); 00190 00191 std::vector<double> sel(1); 00192 sel[0] = 1; 00193 00194 shapes.setSelectedValues(sel); 00195 00196 std::vector<ImageSelectionCube*> sh = shapes.getSelections(); 00197 00198 00199 std::stringstream ss; 00200 /* for(int l=0; l<res.size(); l++){ 00201 if(l%10 == 0) ss << "\n"; 00202 ss << res[l]; 00203 */ 00204 for(unsigned int l=0; l<sh.size(); l++){ 00205 ss << sh[l]->getAsString() << "\n"; 00206 } 00207 00208 QMessageBox::information( 0, "ImageCalculatorDialog::test", (ss.str()).c_str() ); 00209 } 00210
Home |
Search |
Disclaimers & Privacy |
Contact Us GLIMSView Maintainer: dsoltesz@usgs.gov |