Skip to content

Commit

Permalink
merged with Joey
Browse files Browse the repository at this point in the history
  • Loading branch information
Fanchengyan committed Jun 19, 2023
2 parents aa86dd9 + 472e55d commit 5d812eb
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 53 deletions.
Binary file added icons/img_encoder.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
118 changes: 118 additions & 0 deletions icons/img_encoder.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 7 additions & 4 deletions tools/SAMTool.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import time

import numpy as np
import rasterio as rio
import rasterio
from rasterio.transform import from_bounds as transform_from_bounds
from rasterio.features import shapes as get_shapes
from PyQt5.QtWidgets import QMessageBox
from qgis.core import QgsRectangle, QgsMessageLog, Qgis
from torch.utils.data import DataLoader
Expand Down Expand Up @@ -94,7 +96,7 @@ def sam_predict(self, canvas_points: Canvas_Points, canvas_rect: Canvas_Rectangl
bbox = self.sample_bbox # batch['bbox'][0]
# Change to sam.img_encoder.img_size
img_width = img_height = self.predictor.model.image_encoder.img_size # 1024
img_clip_transform = rio.transform.from_bounds(
img_clip_transform = transform_from_bounds(
bbox.minx, bbox.miny, bbox.maxx, bbox.maxy, img_width, img_height)

input_point, input_label = canvas_points.get_points_and_labels(
Expand Down Expand Up @@ -129,13 +131,14 @@ def sam_predict(self, canvas_points: Canvas_Points, canvas_rect: Canvas_Rectangl
# results = ({'properties': {'raster_val': v}, 'geometry': s}
# for i, (s, v) in enumerate(rio.features.shapes(mask.astype(np.uint8), mask=mask, transform=img_clip_transform)))
# geoms = list(results)
shape_generator = rio.features.shapes(
shape_generator = get_shapes(
mask.astype(np.uint8),
mask=mask,
connectivity=8, # change from default:4 to 8
transform=img_clip_transform
)
geojson = [{'properties': {'raster_val': value}, 'geometry': polygon}
for polygon, value in shape_generator]
for polygon, value in shape_generator]

# add to layer
sam_polygon.rollback_changes()
Expand Down
10 changes: 6 additions & 4 deletions tools/geoTool.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import os
import typing
import numpy as np
from qgis.core import (QgsProject, QgsCoordinateReferenceSystem, Qgis, QgsMessageLog,
from qgis.core import (QgsProject, QgsCoordinateReferenceSystem, Qgis, QgsMessageLog,
QgsCoordinateTransform, QgsPointXY, QgsRectangle, QgsVectorLayer)


class ImageCRSManager:
'''Manage image crs and transform point and extent between image crs and other crs'''

def __init__(self, img_crs) -> None:
self.img_crs = QgsCoordinateReferenceSystem(
img_crs) # from str to QgsCRS
Expand Down Expand Up @@ -63,18 +64,19 @@ def extent_to_img_crs(
dst_crs, self.img_crs, QgsProject.instance())
extent_transformed = transform.transformBoundingBox(extent)
return extent_transformed

def img_extent_to_crs(self, extent: QgsRectangle, dst_crs: QgsCoordinateReferenceSystem):
'''transform extent from this image crs to destination crs
Parameters:
----------
extent: QgsRectangle
extent in this image crs
dst_crs: QgsCoordinateReferenceSystem
destination crs for extent
'''
transform = QgsCoordinateTransform(self.img_crs, dst_crs, QgsProject.instance())
transform = QgsCoordinateTransform(
self.img_crs, dst_crs, QgsProject.instance())
extent_transformed = transform.transformBoundingBox(extent)
return extent_transformed

Expand Down
Loading

0 comments on commit 5d812eb

Please sign in to comment.