-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshipDetectionFilter.cpp
More file actions
619 lines (503 loc) · 16.4 KB
/
shipDetectionFilter.cpp
File metadata and controls
619 lines (503 loc) · 16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/**
* OVERVIEW:
* Filter which converts each tile to IplImage .
*
* Makes use of the ossim framework and opencv libraries.
* The main operation starts with the getTile function.
* Ossim splits up the image into tiles and for each tile,
* the getTile function is called.
*
*/
#include "shipDetectionFilter.h"
#define DEBUG_FILTER 0
RTTI_DEF1(shipDetectionFilter, "shipDetectionFilter", ossimImageCombiner)
shipDetectionFilter::shipDetectionFilter(ossimObject* owner)
: ossimImageCombiner(owner,
1,
0,
true,
false),
theTile(NULL),
tileNumber(0)
{
xScale = 0;
yScale = 0;
numberOfImageInputs = 1;
geom = 0;
writer.open("detections.kml");
}
shipDetectionFilter::~shipDetectionFilter()
{
writeDetectionsToKmlFile();
writeShpFile();
}
void shipDetectionFilter::initialize()
{
ossimImageCombiner::initialize();
if(getInput(0))
{
// Force an allocate on the next getTile.
theTile = NULL;
}
// theTile = NULL;
//
// if(!isSourceEnabled())
// {
// return;
// }
//
// theTile = ossimImageDataFactory::instance()->create(this, this);
// if(theTile.valid())
// {
// theTile->initialize();
// }
}
void shipDetectionFilter::setScale(double scaleX, double scaleY)
{
xScale = scaleX;
yScale = scaleY;
}
ossimScalarType shipDetectionFilter::getOutputScalarType() const
{
if(!isSourceEnabled())
{
return ossimImageCombiner::getOutputScalarType();
}
return OSSIM_UCHAR;
}
ossim_uint32 shipDetectionFilter::getNumberOfOutputBands() const
{
if(!isSourceEnabled())
{
return ossimImageCombiner::getNumberOfOutputBands();
}
return 1;
}
bool shipDetectionFilter::saveState(ossimKeywordlist& kwl,
const char* prefix)const
{
ossimImageCombiner::saveState(kwl, prefix);
return true;
}
bool shipDetectionFilter::loadState(const ossimKeywordlist& kwl,
const char* prefix)
{
ossimImageCombiner::loadState(kwl, prefix);
return true;
}
double shipDetectionFilter::round(double x)
{
return floor(x + 0.5);
}
ossimRefPtr<ossimImageData> shipDetectionFilter::getTile(const ossimIrect& tileRect,
ossim_uint32 resLevel)
{
tileNumber++;
cout << "Getting Tile: " << tileNumber << endl;
// Check input data sources for valid and null tiles
ossimImageSource *imageSource = PTR_CAST(ossimImageSource, getInput(0));
ossimRefPtr<ossimImageData> imageSourceData;
if (imageSource){
imageSourceData = imageSource->getTile(tileRect, resLevel);
}
if (!isSourceEnabled()){
return imageSourceData;
}
if (!theTile.valid())
{
if(getInput(0))
{
theTile = ossimImageDataFactory::instance()->create(this, this);
theTile->initialize();
}
}
if(!imageSourceData.valid()){
cout << "imageSouceData not valid" << endl;
}
if(!theTile.valid()){
cout << "theTile.valid() not valid" << endl;
}
if (!imageSourceData.valid() || !theTile.valid()){
return ossimRefPtr<ossimImageData>();
}
theTile->setOrigin(tileRect.ul());
if (theTile->getImageRectangle() != tileRect)
{
theTile->setImageRectangle(tileRect);
theTile->initialize();
}
IplImage *input = cvCreateImage(cvSize(tileRect.width(), tileRect.height()),IPL_DEPTH_8U,3);
IplImage *output = cvCreateImage(cvSize(tileRect.width(),tileRect.height()),IPL_DEPTH_8U,1);
cvZero(input);
cvZero(output);
// If 16 or 32 bits, downsample to 8 bits
ossimScalarType inputType = imageSourceData->getScalarType();
if(inputType == OSSIM_UINT16 || inputType == OSSIM_USHORT11){
CopyTileToIplImage(static_cast<ossim_uint16>(0), imageSourceData, input, tileRect);
}else{
CopyTileToIplImage(static_cast<ossim_uint8>(0), imageSourceData, input, tileRect);
}
//cvCopy(input, output);
// Determine the actual height and width of each tile
ossimIrect fullImageRect;
fullImageRect = imageSource->getBoundingRect(0);
ossim_int32 tileHeight, tileWidth, imageWidth, imageHeight;
tileHeight = tileRect.height();
tileWidth = tileRect.width();
imageWidth = fullImageRect.width();
imageHeight = fullImageRect.height();
ossim_int32 totRows, totCols;
totRows = (ossim_uint32)round(imageHeight / tileHeight);
totCols = (ossim_uint32)round(imageWidth / tileWidth);
ossimIpt upperLeftTile = tileRect.ul();
if ((upperLeftTile.x + 1) > fullImageRect.ul().x + totCols * tileWidth)
tileWidth = imageWidth - totCols * tileWidth;
if ((upperLeftTile.y + 1) > fullImageRect.ul().y + totRows * tileHeight)
tileHeight = imageHeight - totRows * tileHeight;
//Begin Ship Detect Algorithim
if(tileNumber == 1){
//Write out four corners to kml
//writeFourCornersToKml();
ossimDpt UL(fullImageRect.ul());
ossimDpt UR(fullImageRect.ur());
ossimDpt LR(fullImageRect.lr());
ossimDpt LL(fullImageRect.ll());
ossimGpt worldul, worldur, worldlr, worldll;
geom->localToWorld(UL, worldul);
geom->localToWorld(UR, worldur);
geom->localToWorld(LR, worldlr);
geom->localToWorld(LL, worldll);
writer.writePlacemark("UL","upper left corner",worldul.lat, worldul.lon, worldul.hgt);
writer.writePlacemark("UR","upper right corner",worldur.lat, worldur.lon, worldur.hgt);
writer.writePlacemark("LR","lower right corner",worldlr.lat, worldlr.lon, worldlr.hgt);
writer.writePlacemark("LL","lower left corner",worldll.lat, worldll.lon, worldll.hgt);
}
//Create a mask for your detected objects
//Detected Object == 255 ... Everything else == 0
markObjects(input,output);
//Extract blobs of objects and their contours
//ossimIrect* rect = new ossimIrect(tileRect);
extractBlobsAndContours(output, tileRect, &fullImageRect);
//delete rect;
// Create sub-image to ignore zeros created by OSSIM
// ie, the tile is 512x512 but on the edges, the information is only in 512x10
/*
CvRect subRect = cvRect(0, 0, tileWidth, tileHeight);
IplImage *subImg = cvCreateImage(cvSize(tileWidth, tileHeight),IPL_DEPTH_8U,3);
cvSetImageROI(input, subRect);
cvCopy(input, subImg);
cvResetImageROI(input);
*/
if(DEBUG_FILTER){
showImage(input, output);
}
cvReleaseImage(&input);
cvReleaseImage(&output);
//cvReleaseImage(&subImg);
return theTile;
}
template<class T>
void shipDetectionFilter::CopyTileToIplImage(T dummyVariable, ossimRefPtr<ossimImageData> inputTile, IplImage *output, ossimIrect neighborhoodRect)
{
ossimDataObjectStatus status = inputTile->getDataObjectStatus();
uchar *outputData = (uchar *)output->imageData;
int outputStep = output->widthStep/sizeof(uchar);
int outputChannels = output->nChannels;
ossimScalarType inputType = inputTile->getScalarType();
double scFactor;
if (inputType == OSSIM_UINT16)
scFactor = 0.0039; // 255 / 65535
else if (inputType == OSSIM_USHORT11)
scFactor = 0.1246; //255 / 2047
else if (inputType == OSSIM_UINT8)
scFactor = 1;
else
scFactor = 1;
int pixVal;
if (status == OSSIM_PARTIAL)
{
for( int band = 0; band < outputChannels; band++){
T* inBuf = static_cast<T*>(inputTile->getBuf(band));
for (long y = 0; y < output->height; ++y)
{
for (long x = 0; x < output->width; ++x)
{
pixVal = (int)(*inBuf);
if ((int)round(pixVal * scFactor) > 255)
outputData[y * outputStep + x*outputChannels + band] = 255;
else if ((int)round(pixVal * scFactor) < 0)
outputData[y * outputStep + x*outputChannels + band] = 0;
else
outputData[y * outputStep + x*outputChannels + band] = (uchar)round(pixVal * scFactor);
++inBuf;
}
}
}
}
else
{
for(int band = 0; band < outputChannels; band++){
T* inBuf = static_cast<T*>(inputTile->getBuf(band));
for (int y = 0; y < output->height; ++y)
{
for (int x = 0; x < output->width; ++x)
{
pixVal = (int)(*inBuf);
if ((int)round(pixVal * scFactor) > 255)
outputData[y * outputStep + x*outputChannels + band] = 255;
else if ((int)round(pixVal * scFactor) < 0)
outputData[y * outputStep + x*outputChannels + band] = 0;
else
outputData[y * outputStep + x*outputChannels + band] = (uchar)round(pixVal * scFactor);
++inBuf;
}
}
}
}
}
template<class T>
void shipDetectionFilter::CopyIplImageToTile(T dummyVariable, ossimRefPtr<ossimImageData> inputTile, IplImage *output)
{
// Determine if tile is full or partially filled
ossimDataObjectStatus status = inputTile->getDataObjectStatus();
uchar *outputData = (uchar *)output->imageData;
int outputStep = output->widthStep/sizeof(uchar);
int pixVal;
long outputOffset = 0;
T maxPix = static_cast<T>(getMaxPixelValue(0));
T minPix = static_cast<T>(getMinPixelValue(0));
T np = static_cast<T>(inputTile->getNullPix(0));
if (status == OSSIM_PARTIAL)
{
for (ossim_uint32 bandIdx = 0; bandIdx < inputTile->getNumberOfBands(); ++bandIdx)
{
T* outBuf = static_cast<T*>(inputTile->getBuf(bandIdx));
if (outBuf)
{
outputOffset = 0;
for (long y = 0; y < output->height; ++y)
{
for (long x = 0; x < output->width; ++x)
{
if (!inputTile->isNull(outputOffset))
{
pixVal = (int)round(outputData[y * outputStep + x]);
if (pixVal > maxPix)
*outBuf = maxPix;
else if (pixVal < 0)
*outBuf = minPix;
else
*outBuf = static_cast<T>(pixVal);
}
else
inputTile->setNull(outputOffset);
++outBuf;
++outputOffset;
}
}
}
}
}
else
{
for (ossim_uint32 bandIdx = 0; bandIdx < inputTile->getNumberOfBands(); ++bandIdx)
{
T* outBuf = (T*)(inputTile->getBuf(bandIdx));
if (outBuf)
{
for (int y = 0; y < output->height; ++y)
{
for (int x = 0; x < output->width; ++x)
{
pixVal = (int)round(outputData[y * outputStep + x]);
if (pixVal > maxPix)
*outBuf = (maxPix);
else if (pixVal < 0)
*outBuf = (minPix);
else
*outBuf = static_cast<T>(pixVal);
// Increment the output buffer to the next pixel value
++outBuf;
}
}
}
else
*outBuf = np;
}
}
}
void shipDetectionFilter::showImage(IplImage* src1, IplImage* src2)
{
if (src1==NULL || src2==NULL)
cout << "ERROR ERROR _____ERROR " << endl;
// create window
cvNamedWindow("image1", CV_WINDOW_AUTOSIZE);
cvMoveWindow("image1", 100, 100);
cvNamedWindow("image2", CV_WINDOW_AUTOSIZE);
cvMoveWindow("image2", 600, 100);
// show the image
cvShowImage("image1", src1);
cvShowImage("image2", src2);
// wait for a key
int userKey = cvWaitKey(0);
if(userKey == 81 || userKey==113){ //exit if user presses q or Q
exit(1);
}
cvDestroyWindow("image1");
cvDestroyWindow("image2");
}
int shipDetectionFilter::GetTotalNumberTiles()
{
return totalTiles;
}
void shipDetectionFilter::setGeometry(ossimRefPtr<ossimImageSource> handler){
geom = handler->getImageGeometry();
}
void shipDetectionFilter::markObjects(IplImage* in, IplImage* out){
//Insert your better detection here
//create a 1 channel image for grayscale image
//this is the same size as the input
IplImage* grayImage =
cvCreateImage(
cvSize(in->width, in->height),
IPL_DEPTH_8U,
1);
//use gray image
//convert input image to grayscale image
cvCvtColor(in, grayImage, CV_BGR2GRAY);
cvThreshold(grayImage, grayImage, 100, 255, CV_THRESH_BINARY);
//This is where you would put your
//super awesome ship detector
//at this point we have a binary image of our detections
//so now we want to detect the blobs in the image
//these blobs are our targets
//perform a closing operation
cvDilate(grayImage, grayImage); //local max
cvErode(grayImage, grayImage); //local min
//now grayImage is "closed"
cvCopy(grayImage, out); //copy the result to the output
cvReleaseImage(&grayImage); //free the memory
}
void shipDetectionFilter::extractBlobsAndContours(IplImage* detectionsMask, const ossimIrect& tileRect, ossimIrect* fullImageRect){
int AREA_THRESH_LOWER = 5;
ossimIpt iUpperLeftTilePoint = tileRect.ul();
ossimIpt contourPoint;
int offset = 2; //this is the size of border we are making around the input when we create blobMask
IplImage *blobMask = cvCreateImage(cvSize(detectionsMask->width+2*offset, detectionsMask->height+2*offset), IPL_DEPTH_8U, 1); //add padding so blob won't touch edge
IplImage *blobMaskCrop = cvCreateImage(cvSize(detectionsMask->width, detectionsMask->height), IPL_DEPTH_8U, 1);
cvCopyMakeBorder(detectionsMask, blobMask, cvPoint(offset,offset), IPL_BORDER_REPLICATE);
CBlobResult blobs = CBlobResult(blobMask, NULL, 0);
if (blobs.GetNumBlobs() > 0)
{
//get rid of blobs smaller than AREA_THRESH
blobs.Filter(blobs, B_INCLUDE, CBlobGetArea(), B_GREATER, AREA_THRESH_LOWER);
}
// mark the blobs on the image
// delare a single blob
CBlob Blob;
ossimIrect blobRect;
static int currentBlobNumber = 1;
int numBlobs = blobs.GetNumBlobs();
for (int i=0; i<numBlobs; ++i)
{
// get the blob info
Blob = blobs.GetBlob(i);
//cout << "blob(" << i << ").Area()=" <<Blob.Area() << endl;
//GET CONTOUR FOR KML POLYGON
cvSetZero(blobMask);
CvScalar color = CV_RGB(255,255,255);
Blob.FillBlob(blobMask,color); //set all pixels in blob mask =255
if(DEBUG_FILTER){
cvShowImage("blob", blobMask);
cvWaitKey(0);
}
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* contours = NULL;
cvFindContours(blobMask, storage, &contours, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
int contourCount = 0;
for(CvSeq* currentContour = contours; currentContour!=NULL; currentContour=currentContour->h_next)
{
contourCount++;
vector<ossimGpt> contour;
for(int contourPosition = 0; contourPosition < currentContour->total; ++contourPosition)
{
CvPoint* p = CV_GET_SEQ_ELEM(CvPoint, currentContour, contourPosition);
/*cout << p->x << " " << p->y << endl;
cout << geom->getMetersPerPixel() << endl;
ossimGpt tempGpt;
geom->localToWorld(ossimDpt(p->x,p->y),tempGpt);
cout << "lat: " << tempGpt.lat <<
" lon:" << tempGpt.lon << endl;*/
contourPoint = ossimIpt(p->x-offset, p->y-offset) + iUpperLeftTilePoint;
ossimDpt temp_dpt = ossimDpt(contourPoint.x, contourPoint.y);
ossimGpt temp_gpt;
geom->localToWorld(temp_dpt, temp_gpt);
contour.push_back(temp_gpt);
//writer.writePlacemark("contour points","",temp_gpt.lat, temp_gpt.lon, 0);
}
polygons.push_back(contour);
contour.clear();
}
currentBlobNumber++;
cvReleaseMemStorage(&storage);
//END OF GET CONTOUR FOR KML POLYGON
}
cvReleaseImage(&blobMask);
cvReleaseImage(&blobMaskCrop);
blobs.ClearBlobs();
}
void shipDetectionFilter::writeDetectionsToKmlFile(){
for(int i = 0; i < polygons.size(); i++){
string name = "Ship ";
char index[10];
itoa(i, index, 10);
name+=index;
writer.writePolygon(name, " ", polygons[i], 1, 1, "clampToGround");
}
}
void shipDetectionFilter::writeShpFile(){
ossimFilename shapefileName("detections.shp");
SHPHandle hSHP = SHPCreate(shapefileName.c_str(),SHPT_POLYGON);
SHPObject *psObject;
if(hSHP == NULL){
cout << "Shapefile could not be created" << endl;
}
DBFHandle hDBF;
hDBF = DBFCreate(shapefileName.c_str());
//Add all fields that we wish to access from DBF
DBFAddField(hDBF, "Area", FTString, 30, 0);
/*DBFAddField(hDBF, "Length", FTString, 30, 0);
DBFAddField(hDBF, "Width", FTString, 30, 0);*/
for(int i = 0; i<polygons.size(); i++){
int nVertices = polygons[i].size();
int* panParts = new int[nVertices+1];
double* padfX = new double[nVertices+1];
double* padfY = new double[nVertices+1];
for(int j=0; j<nVertices; j++){
padfX[j] = polygons[i][j].lon;
padfY[j] = polygons[i][j].lat;
//padfZ[j] = polygons[i][j].hgt;
}
//the last must be equal to the first
padfX[nVertices] = polygons[i][0].lon;
padfY[nVertices] = polygons[i][0].lat;
psObject = SHPCreateObject(SHPT_POLYGON, -1, 1, panParts, NULL, nVertices + 1,
padfX, padfY, NULL, NULL);
SHPWriteObject(hSHP, -1, psObject );
DBFWriteStringAttribute(hDBF, i, 0,
ossimString::toString(polygons[i].area()));
/*
//for length
DBFWriteStringAttribute(hDBF, i, 1,
ossimString::toString(123));
//for width
DBFWriteStringAttribute(hDBF, i, 2,
ossimString::toString(456));*/
SHPDestroyObject(psObject );
delete [] panParts;
delete [] padfX;
delete [] padfY;
}
SHPClose(hSHP );
DBFClose( hDBF );
}