import numpy as np | |
import cv2 | |
import math | |
from scipy import ndimage | |
import matplotlib.pyplot as plt | |
from lxml import etree | |
import xml.etree.cElementTree as xml | |
import os | |
from PIL import Image | |
"""Tools for satellite imagery pre-processing""" | |
def sharp(img,level=3): #level[1:5] | |
def unsharp_mask(image, kernel_size=(5, 5), sigma=1.0, amount=1.0, threshold=0): | |
"""Return a sharpened version of the image, using an unsharp mask.""" | |
blurred = cv2.GaussianBlur(image, kernel_size, sigma) | |
sharpened = float(amount + 1) * image - float(amount) * blurred | |
sharpened = np.maximum(sharpened, np.zeros(sharpened.shape)) | |
sharpened = np.minimum(sharpened, 255 * np.ones(sharpened.shape)) | |
sharpened = sharpened.round().astype(np.uint8) | |
if threshold > 0: | |
low_contrast_mask = np.absolute(image - blurred) < threshold | |
np.copyto(sharpened, image, where=low_contrast_mask) | |
return sharpened | |
if level == 1: #low | |
sharpened = unsharp_mask(img) | |
elif level == 2: #med_low | |
kernel_sharp = np.array([[0, -1, 0], | |
[-1, 5, -1], | |
[0, -1, 0]]) | |
sharpened = cv2.filter2D(img, -1, kernel_sharp) | |
#sharpened = cv2.bilateralFilter(sbgimg, 3, 75 ,75) | |
elif level == 3: #med. Best result on average | |
kernel_sharp = np.array([[-1, -1, -1], | |
[-1, 8, -1], | |
[-1, -1, 0]]) | |
sharpened = cv2.filter2D(img, -1, kernel_sharp) | |
elif level == 4: #med_high | |
kernel_sharpening = np.array([[-1,-1,-1], | |
[-1, 9,-1], | |
[-1,-1,-1]]) | |
sharpened = cv2.filter2D(img, -1, kernel_sharpening) | |
elif level == 5: #high | |
kernel_sharp = np.array([[-2, -2, -2], | |
[-2, 17, -2], | |
[-2, -2, -2]]) | |
sharpened = cv2.filter2D(img, -1, kernel_sharp) | |
else: | |
sharpened = img | |
print("image didn't change...") | |
return sharpened | |
def thresholdingTOZERO(image,lower=100,upper=200): | |
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) | |
ret,thes=cv2.threshold(gray,lower,upper,cv2.THRESH_TOZERO) | |
return thes | |
def thresholdingBINARY(image,lower=100,upper=200): | |
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) | |
ret,thes=cv2.threshold(gray,lower,upper,cv2.THRESH_BINARY_INV) | |
return thes | |
def rectCoun(image,mode='bi',lower=None,upper=None,maxdim=4096,maxwidth=1000,maxheight=1000): | |
''' | |
upper=Upper limit of threshold | |
lower=Lower limit of threshold | |
maxdim=Maximum dimention example (64*64)==4096 | |
''' | |
listoflocations=[] | |
if lower==None: lower=100 | |
if upper==None: upper=200 | |
if mode=='bi': | |
thes=thresholdingBINARY(image,lower,upper) | |
elif mode=='zero': | |
thes=thresholdingTOZERO(image,lower,upper) | |
_,countours,heir=cv2.findContours(thes,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) | |
for c in countours: | |
temp_={} | |
x,y,w,h=cv2.boundingRect(c) | |
if w*h >maxdim and w<maxwidth and h<maxheight: | |
if w>h: | |
w=h | |
else: | |
h=w | |
temp_['xmin']=x | |
temp_['xmax']=x+w | |
temp_['ymin']=y | |
temp_['ymax']=y+h | |
listoflocations.append(temp_) | |
cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),2) | |
return image,listoflocations |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment