OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
AtpAnnotatedImage.cpp
Go to the documentation of this file.
1 //**************************************************************************************************
2 //
3 // OSSIM Open Source Geospatial Data Processing Library
4 // See top level LICENSE.txt file for license information
5 //
6 //**************************************************************************************************
7 #include "AtpAnnotatedImage.h"
8 #include "AtpConfig.h"
9 #include "../AtpCommon.h"
22 
23 using namespace std;
24 
25 #define DEBUG_ATP
26 
27 namespace ATP
28 {
29 AtpAnnotatedImage::AtpAnnotatedImage(ossimRefPtr<ossimImageChain>& sourceChain,
31  const ossimDrect& aoi)
32 : m_annFilename("annotated.tif")
33 {
34  const char* MODULE = "AtpGenerator::setupAnnotatedChain() ";
35 
36  // Combine inputs into a two-color multiview
37 // ossimRefPtr<ossimTwoColorView> mosaic (new ossimTwoColorView);
38 // mosaic->connectMyInputTo(m_refChain.get());
39 // mosaic->connectMyInputTo(m_cmpChain.get());
40 // m_annChain->add(mosaic.get());
41  add(sourceChain.get()); // just use ref image.
42 
43  // Insure there is a renderer to output to a common view space:
44  ossimTypeNameVisitor vref ("ossimImageRenderer");
45  sourceChain->accept(vref);
46  ossimImageRenderer* r = vref.getObjectAs<ossimImageRenderer>();
47  if (!r)
48  {
49  auto renderer = new ossimImageRenderer;
50  add(renderer);
51  renderer->setImageViewTransform(ivt.get());
52 
53  // Add cache after resampler:
54  auto * cache = new ossimCacheTileSource();
55  add(cache);
56  }
57 
58  // The ref chain is necessarily single band. Need to map to RGB:
60  add(band_selector.get());
61  vector<ossim_uint32> bandList;
62  bandList.push_back(0);
63  bandList.push_back(0);
64  bandList.push_back(0);
65  band_selector->setOutputBandList(bandList, true);
66 
67  // Add the graphics annotation source:
69  add(m_annSource.get());
70 
71  initialize();
72 
73  // Keep track of the view geometry's image size to scale output when writing annotated chain:
74  int annSize = AtpConfig::instance().getParameter("annotatedImageSize").asInt();
75  if (annSize <= 0)
76  m_annScale = ossimDpt(1, 1);
77  else
78  m_annScale = ossimDpt(aoi.width()/annSize, aoi.height()/annSize);
79 
80  if (m_annScale.y > m_annScale.x)
82  else
84  if (m_annScale.x < 1.0)
85  m_annScale = ossimDpt(1.0, 1.0);
87 
88  setAOI(aoi);
89 }
90 
92 {
93 }
94 
96 {
97  // Establish the AOI in scaled space:
99 }
100 
101 void AtpAnnotatedImage::annotateResiduals(AtpList& atps, int r, int g, int b)
102 {
103  if (!m_annSource)
104  return;
105 
106  int halfWidth = AtpConfig::instance().getParameter("corrWindowSize").asUint() * m_annScaleInverse.x / 2 ;
107  if (halfWidth < 3)
108  halfWidth = 3;
109 
110  AtpConfig& config = AtpConfig::instance();
111  double residualMultiplier = config.getParameter("residualMultiplier").asFloat();
112 
113  ossimDpt refPt, cmpPt, residual;
114  for (auto &atp : atps)
115  {
116  atp->getRefViewPoint(refPt);
117  refPt *= m_annScaleInverse.x;
118 
119  // Draw box around feature:
120  ossimIrect box;
121  box.set_ul (ossimIpt(refPt.x - halfWidth, refPt.y - halfWidth));
122  box.set_ur (ossimIpt(refPt.x + halfWidth, refPt.y - halfWidth));
123  box.set_lr (ossimIpt(refPt.x + halfWidth, refPt.y + halfWidth));
124  box.set_ll (ossimIpt(refPt.x - halfWidth, refPt.y + halfWidth));
125  drawBox(box, r, g, 0);
126 
127  // Draw residual vector:
128  atp->getVectorResidual(residual);
129  cmpPt.x = refPt.x + residualMultiplier*residual.x;
130  cmpPt.y = refPt.y + residualMultiplier*residual.y;
132  m_annSource->addObject(l.get());
133  }
134 }
135 void AtpAnnotatedImage::annotateTPIDs(AtpList& atps, int r, int g, int b)
136 {
137  if (!m_annSource)
138  return;
139 
140  ossimDpt refPt, cmpPt, residual;
141  for (auto &atp : atps)
142  {
143  atp->getRefViewPoint(refPt);
145  new ossimAnnotationFontObject(refPt, atp->getTiePointId());
146  m_annSource->addObject(f.get());
147  }
148 }
149 
150 void AtpAnnotatedImage::annotateFeatures(vector<ossimDpt>& features, int r, int g, int b)
151 {
152  if (!m_annSource)
153  return;
154 
155  AtpConfig& config = AtpConfig::instance();
156  int d = 1 ;
157 
158  for (auto &p : features)
159  {
160  // For each correlation, draw +, scaling to the size of the annotated image:
161  p *= m_annScaleInverse.x;
162  ossimDpt p1 (p.x, p.y-d);
163  ossimDpt p2 (p.x, p.y+d);
164  ossimDpt p3 (p.x-d, p.y);
165  ossimDpt p4 (p.x+1, p.y);
166 
167  ossimAnnotationLineObject* line1 = new ossimAnnotationLineObject(p1, p2, r, g, b);
168  ossimAnnotationLineObject* line2 = new ossimAnnotationLineObject(p3, p4, r, g, b);
169 
170  m_annSource->addObject(line1);
171  m_annSource->addObject(line2);
172  }
173 }
174 
175 void AtpAnnotatedImage::annotateCorrelations(AtpList& atpList, int r, int g, int b)
176 {
177  if (!m_annSource)
178  return;
179 
180  AtpConfig& config = AtpConfig::instance();
181  int halfWidth = config.getParameter("corrWindowSize").asUint() * m_annScaleInverse.x / 2 ;
182  if (halfWidth < 3)
183  halfWidth = 3;
184 
185  double residualMultiplier = config.getParameter("residualMultiplier").asFloat();
186  if (residualMultiplier < 1)
187  residualMultiplier = 1.0;
188 
189  ossimDpt refPt, cmpPt, residual;
190  for (auto &atp : atpList)
191  {
192  // For each correlation, draw a box, scaling to the size of the annotated image:
193  atp->getCmpViewPoint(cmpPt);
194  cmpPt *= m_annScaleInverse.x;
195 
196  ossimIrect box;
197  box.set_ul (ossimIpt(cmpPt.x - halfWidth, cmpPt.y - halfWidth));
198  box.set_ur (ossimIpt(cmpPt.x + halfWidth, cmpPt.y - halfWidth));
199  box.set_lr (ossimIpt(cmpPt.x + halfWidth, cmpPt.y + halfWidth));
200  box.set_ll (ossimIpt(cmpPt.x - halfWidth, cmpPt.y + halfWidth));
201  drawBox(box, r, g, b);
202 
203  // Draw residual vector:
204  atp->getVectorResidual(residual);
205  refPt.x = cmpPt.x - residualMultiplier*residual.x;
206  refPt.y = cmpPt.y - residualMultiplier*residual.y;
208  m_annSource->addObject(l.get());
209  }
210 }
211 
213 {
214  ossimDpt ptA, ptB;
215  const vector<ossimDpt>& vlist = poly.getVertexList();
216 
217  for (size_t i=0; i<vlist.size(); ++i)
218  {
219  ptA = vlist[i];
220  if (i==0)
221  ptB = vlist.back();
222  else
223  ptB = vlist[i-1];
224 
225  ptA *= m_annScaleInverse.x;
226  ptB *= m_annScaleInverse.x;
227 
228  ossimAnnotationLineObject* line = new ossimAnnotationLineObject(ptA, ptB, 0,255,255);
229  m_annSource->addObject(line);
230  }
231 }
232 
233 void AtpAnnotatedImage::annotateFeatureSearchTiles(std::vector<ossimIrect>& searchTileRects)
234 {
235  const char* MODULE = "AtpAnnotatedImage::annotateFeatureSearchTiles() ";
236 
237  if (!m_annSource)
238  return;
239 
240  if (searchTileRects.empty())
241  {
242  CINFO<<MODULE<<"No feature search tiles were established in the overlap."<<endl;
243  return;
244  }
245 
246  ossimDpt pt;
247 
248  for (auto &searchTileRect : searchTileRects)
249  {
250  ossimIrect box;
251 
252  // For each search rect, draw a box:
253  pt.x = searchTileRect.ul().x;
254  pt.y = searchTileRect.ul().y;
255  box.set_ul(pt * m_annScaleInverse.x);
256 
257  pt.x = searchTileRect.ur().x;
258  pt.y = searchTileRect.ur().y;
259  box.set_ur(pt * m_annScaleInverse.x);
260 
261  pt.x = searchTileRect.lr().x;
262  pt.y = searchTileRect.lr().y;
263  box.set_lr(pt * m_annScaleInverse.x);
264 
265  pt.x = searchTileRect.ll().x;
266  pt.y = searchTileRect.ll().y;
267  box.set_ll(pt * m_annScaleInverse.x);
268 
269  drawBox(box, 200, 255, 100);
270  }
271 }
272 
274 {
275  if (!m_annSource)
276  return;
277 
278  ossimIrect scaledRect = tileRect * m_annScaleInverse.x;
279  drawBox(scaledRect, 255, 200, 100);
280 }
281 
283 {
285  writer->setFilename(m_annFilename);
286  writer->connectMyInputTo(this);
288  writer->initialize();
289  bool status = writer->execute();
290  writer->close();
291 
292  if (status)
293  CINFO<<"\nAtpAnnotatedImage::write() -- Wrote "<<m_annFilename<<endl;
294 
295  return status;
296 }
297 
298 void AtpAnnotatedImage::drawBox(const ossimIrect& box, int r, int g, int b)
299 {
300  ossimAnnotationLineObject* line1 = new ossimAnnotationLineObject(box.ul(), box.ur(), r, g, b);
301  ossimAnnotationLineObject* line2 = new ossimAnnotationLineObject(box.ur(), box.lr(), r, g, b);
302  ossimAnnotationLineObject* line3 = new ossimAnnotationLineObject(box.lr(), box.ll(), r, g, b);
303  ossimAnnotationLineObject* line4 = new ossimAnnotationLineObject(box.ll(), box.ul(), r, g, b);
304 
305  m_annSource->addObject(line1);
306  m_annSource->addObject(line2);
307  m_annSource->addObject(line3);
308  m_annSource->addObject(line4);
309 }
310 
311 }
JsonParam & getParameter(const char *paramName)
Returns a parameter (might be a null parameter if paramName not found in the configuration.
Definition: JsonConfig.cpp:377
ossim_float64 width() const
Definition: ossimDrect.h:522
void annotateCorrelations(AtpList &atpList, int r, int g, int b)
double y
Definition: ossimDpt.h:165
virtual void initialize()
double asFloat() const
Definition: JsonConfig.cpp:278
unsigned int asUint() const
Definition: JsonConfig.cpp:264
const ossimIpt & ul() const
Definition: ossimIrect.h:274
std::vector< std::shared_ptr< AutoTiePoint > > AtpList
Definition: AutoTiePoint.h:137
const ossimIpt & ll() const
Definition: ossimIrect.h:277
virtual bool addObject(ossimAnnotationObject *anObject)
void set_ul(const ossimIpt &pt)
Definition: ossimIrect.h:589
virtual void accept(ossimVisitor &visitor)
We will add a visitor interface for all connectable objects.
void annotateOverlap(ossimPolygon &poly)
Cache Tile Source.
void setAOI(const ossimDrect &aoi)
const ossimIpt & lr() const
Definition: ossimIrect.h:276
virtual bool add(ossimConnectableObject *source)
Will return true or false if an image source was added to the chain.
void annotateTPIDs(AtpList &atps, int r, int g, int b)
virtual ossim_int32 connectMyInputTo(ossimConnectableObject *inputObject, bool makeOutputConnection=true, bool createEventFlag=true)
Will try to connect this objects input to the passed in object.
Class to draw fonts onto an image.
void annotateFeatures(std::vector< ossimDpt > &atpList, int r, int g, int b)
ossim_float64 height() const
Definition: ossimDrect.h:517
THESE FUNCTIONS REQUIRE OPENCV.
const ossimIpt & ur() const
Definition: ossimIrect.h:275
const vector< ossimDpt > & getVertexList() const
return status
virtual void setFilename(const ossimFilename &file)
ossimRefPtr< ossimAnnotationSource > m_annSource
void set_lr(const ossimIpt &pt)
Definition: ossimIrect.h:623
void set_ll(const ossimIpt &pt)
Definition: ossimIrect.h:640
virtual void initialize()
Initialize method.
void annotateCorrelationSearchTile(const ossimIrect &tileRect)
void annotateResiduals(AtpList &atps, int r, int g, int b)
virtual void setOutputBandList(const vector< ossim_uint32 > &outputBandList, bool disablePassThru=false)
Sets the output band list.
virtual void setAreaOfInterest(const ossimIrect &inputRect)
double x
Definition: ossimDpt.h:164
static AtpConfig & instance()
Singleton implementation.
Definition: AtpConfig.cpp:20
void drawBox(const ossimIrect &rect, int r, int g, int b)
int asInt() const
Definition: JsonConfig.cpp:271
virtual void close()
#define CINFO
void annotateFeatureSearchTiles(std::vector< ossimIrect > &tileRects)
void set_ur(const ossimIpt &pt)
Definition: ossimIrect.h:606
virtual bool execute()
Calls: writeFile() writeMetaDataFiles()
Singleton class maintaining parameters affecting the automatic tie point generation.
Definition: AtpConfig.h:24