Code for the ship detection

I downloaded the following image from Planetscope (20170720_082653_101e_3B_Visual.tif) and i clipped it using qgis. Then i run the script you have for ship detection adding a print command  in the for loop you have. 


The code is the following:



from IPython.display import Image
# Read image into scimage package
img ='original.png', img)
# Display original image
import json
from osgeo import gdal, osr
import numpy
from skimage.segmentation import felzenszwalb
from skimage.segmentation import mark_boundaries
from skimage.measure import regionprops
result = {
    "ship_count": 0,
    "ships": []
# Open image with gdal
ds = gdal.Open(sample_data_file_name)
xoff, a, b, yoff, d, e = ds.GetGeoTransform()
# Get projection information from source image
ds_proj = ds.GetProjectionRef()
ds_srs = osr.SpatialReference(ds_proj)
# Get the source image's geographic coordinate system (the 'GEOGCS' node of ds_srs)
geogcs = ds_srs.CloneGeogCS()
# Set up a transformation between projected coordinates (x, y) & geographic coordinates (lat, lon)
transform = osr.CoordinateTransformation(ds_srs, geogcs)
# Convert multi-channel image it into red, green and blueb[, alpha] channels 
red, green, blue, alpha = numpy.rollaxis(numpy.array(img), axis=-1)
# Mask: threshold + stops canny detecting image boundary edges
mask = red > 75
# Create mask for edge detection'mask.png', mask * 255)
# Use Felzenszwalb algo to find segements
segments_fz = felzenszwalb(numpy.dstack((mask, mask, mask)),
# Build labeled mask to show where ships were dectected
segmented_img = mark_boundaries(mask, segments_fz)'mask_labeled.png', segmented_img)
# Count ships and save image of each boat clipped from masked image
for idx, ship in enumerate(regionprops(segments_fz)):
    print ('test')
    # If area matches that of a stanard ship, count it
    if (ship.area >= 300 and ship.area <= 10000):
        # Incrment count
        result['ship_count'] += 1
        # Create ship thumbnail
        x, y = (int(numpy.average([ship.bbox[0],
        sx, ex = max(x - 35, 0), min(x + 35, img.shape[0] - 1)
        sy, ey = max(y - 35, 0), min(y + 35, img.shape[1] - 1)
        img_ship = img[sx:ex, sy:ey]'ship-%s.png' % str(idx + 1), img_ship)
        # Get global coordinates from pixel x, y coords
        projected_x = a * y + b * x + xoff
        projected_y = d * y + e * x + yoff
        # Transform from projected x, y to geographic lat, lng
        (lat, lng, elev) = transform.TransformPoint(projected_x, projected_y)
        # Add ship to results cluster
            "id": idx + 1,
            "lat": lat,
            "lng": lng
# Display results
print(json.dumps(result, indent=2))
#Display mask used for ship detection.
# Display labled mask where we detected ships
# Display each individual ship cropped out of the original image
for idx,ship in enumerate(result['ships']):
    print("Ship "+ str(idx + 1)) 
    display(Image(filename='ship-' + str(idx + 1) + '.png'))

However when i run the code it doesn 't go in the for loop to print ('test') so i can try to play with an area to detect the ships i would like. 


For me this doesn t make any sense. 
Any ideas? 


1 comment
  • Assistance for this post was handled via Planet Technical Support. Please contact for similar technical issues. 


Please sign in to leave a comment.

Didn't find what you were looking for?

New post