Image Features – Harris Corner and Hough Line Detection

Feature_Donike
In [1]:
import time
import math
import random
import rasterio
import cv2
import matplotlib.pyplot as plt
import numpy as np
from skimage import color
from skimage import io
from skimage import data, img_as_float

import os
import glob
import mahotas as mt
from sklearn.neighbors import KNeighborsClassifier

import pandas as pd

Ex.1

1.1 - Loading image

In [2]:
!ls UCMerced_LandUse/Images/intersection
intersection00.tif  intersection25.tif	intersection50.tif  intersection75.tif
intersection01.tif  intersection26.tif	intersection51.tif  intersection76.tif
intersection02.tif  intersection27.tif	intersection52.tif  intersection77.tif
intersection03.tif  intersection28.tif	intersection53.tif  intersection78.tif
intersection04.tif  intersection29.tif	intersection54.tif  intersection79.tif
intersection05.tif  intersection30.tif	intersection55.tif  intersection80.tif
intersection06.tif  intersection31.tif	intersection56.tif  intersection81.tif
intersection07.tif  intersection32.tif	intersection57.tif  intersection82.tif
intersection08.tif  intersection33.tif	intersection58.tif  intersection83.tif
intersection09.tif  intersection34.tif	intersection59.tif  intersection84.tif
intersection10.tif  intersection35.tif	intersection60.tif  intersection85.tif
intersection11.tif  intersection36.tif	intersection61.tif  intersection86.tif
intersection12.tif  intersection37.tif	intersection62.tif  intersection87.tif
intersection13.tif  intersection38.tif	intersection63.tif  intersection88.tif
intersection14.tif  intersection39.tif	intersection64.tif  intersection89.tif
intersection15.tif  intersection40.tif	intersection65.tif  intersection90.tif
intersection16.tif  intersection41.tif	intersection66.tif  intersection91.tif
intersection17.tif  intersection42.tif	intersection67.tif  intersection92.tif
intersection18.tif  intersection43.tif	intersection68.tif  intersection93.tif
intersection19.tif  intersection44.tif	intersection69.tif  intersection94.tif
intersection20.tif  intersection45.tif	intersection70.tif  intersection95.tif
intersection21.tif  intersection46.tif	intersection71.tif  intersection96.tif
intersection22.tif  intersection47.tif	intersection72.tif  intersection97.tif
intersection23.tif  intersection48.tif	intersection73.tif  intersection98.tif
intersection24.tif  intersection49.tif	intersection74.tif  intersection99.tif
In [3]:
img_rgb = cv2.imread('UCMerced_LandUse/Images/intersection/intersection00.tif',1)
plt.imshow(img_rgb)
plt.show()
In [4]:
# convert to grayscale
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
plt.imshow(img_gray,cmap="gray")
plt.show()

1.2 - Harris Detector to get Roads

In [5]:
def harris_det(img):
    
    # get thresholds
    ret,thresh = cv2.threshold(img,120,255,cv2.THRESH_BINARY_INV)
    
    # dilation
    kernel = np.ones((5,5),np.uint8)
    img = cv2.dilate(thresh,kernel,iterations=2)
    plt.imshow(img,cmap="gray")
    plt.title("Dilated Image")
    plt.show()
    
    # erosion
    kernel = np.ones((10,10),np.uint8)
    img = cv2.erode(img,kernel,iterations=1)
    plt.imshow(img,cmap="gray")
    plt.title("Eroded Image")
    plt.show()
    
    # get corners
    gray = np.float32(img)
    dst = cv2.cornerHarris(gray,3,5,0.06)
    plt.imshow(dst,cmap="gray")
    plt.title("Harris - Edges and Corners")
    plt.show()
    dst = cv2.dilate(dst,None,iterations=2)
    plt.imshow(dst,cmap="gray")
    plt.title("Harris - Corners Dilated")
    plt.show()
    
    # overlay edges over original image
    img_overlay_corners = img_rgb.copy()
    img_overlay_corners[dst>0.01]=[0,0,255]
    plt.imshow(img_overlay_corners,cmap="gray")
    plt.title("Corners overlaid over Image")
    plt.show()
In [6]:
harris_det(img_gray)

1.3 - Main Road Orientations w/ Hughes transform

In [7]:
img = cv2.imread(cv2.samples.findFile('UCMerced_LandUse/Images/intersection/intersection25.tif'), cv2.IMREAD_GRAYSCALE)
plt.imshow(img)
plt.show()

dst1 = cv2.Canny(img,100,20,None,3)
plt.imshow(dst1,cmap="gray")
plt.show()
In [8]:
# convert to RGB and Copy
cdst = cv2.cvtColor(dst1, cv2.COLOR_GRAY2BGR)
cdst_copy = np.copy(cdst)
In [9]:
# ?cv2.HoughLines
In [10]:
"""
# Manual Solution from Prati
lines = cv2.HoughLines(dst1, 1, np.pi / 180, 125, None, 0, 0)

if lines is not None:
    for i in range(0,len(lines)):
        
        rho = lines[i][0][0]
        theta = lines[i][0][1]
        
        a = math.cos(theta)
        b = math.sin(theta)
        
        x0 = a*rho
        y0 = b*rho
        
        pt1 = (int(x0 + 1000*(-b)),int(y0 + 1000*(a)))
        pt2 = (int(x0 - 1000*(-b)), int(y0 - 1000*(a)))
        
        cv2.line(cdst,pt1,pt2,(0,0,255),7,cv2.LINE_AA)
        
        
plt.imshow(cdst)
plt.show()
"""
# transorm lines
linesP = cv2.HoughLinesP(dst1, 1, np.pi / 180, 155, None, 50, 10)

# draw lines
if linesP is not None:
    for i in range(0, len(linesP)):
        l = linesP[i][0]
        cv2.line(cdst_copy, (l[0], l[1]), (l[2], l[3]), (0,0,255), 7, cv2.LINE_AA)

        

plt.imshow(cdst_copy)
plt.show()

Exercise 2

In [11]:
train_path = "/home/simon/CDE_UBS/Image_Analysis/03_Image_Features/UCMerced_LandUse/Images"
train_classes = os.listdir(train_path) 
In [12]:
# Extract Train and Test DFs holding paths and targets

x_paths_train = []
y_train = []
for train_class in train_classes:
    # get files in train folders
    files = os.listdir(train_path+"/"+train_class)
    # shuffle list to get random 10 images
    random.shuffle(files)
    files = files[:10]
    # append files with paths to dataset and Y label
    for file in files:
        x_paths_train.append(train_path+"/"+train_class+"/"+file)
        y_train.append(train_class)

# create pd df
df_train = pd.DataFrame({"x_path":x_paths_train,"y":y_train})

x_paths_test = []
y_test = []
for test_class in train_classes:
    # get files in train folders
    files = os.listdir(train_path+"/"+test_class)
    # shuffle list to get random 10 images
    random.shuffle(files)
    file = files[0]
    y_test.append(test_class)
    x_paths_test.append(train_path+"/"+test_class+"/"+file)
    """# append files with paths to dataset and Y label
    for file in files:
        x_paths_test.append(train_path+"/"+test_class+"/"+file)
        y_test.append(train_class)"""

# create pd df
df_test = pd.DataFrame({"x_path":x_paths_test,"y":y_test})
    
In [13]:
def get_features(path):
    img = cv2.imread(path)
    textures = mt.features.haralick(img)
    ht_mean = textures.mean(axis=0)
    return(ht_mean)
In [14]:
# append texture
tex = []
for path in df_train["x_path"]:
    tex.append(get_features(path))
df_train["x"] = tex

tex = []
for path in df_test["x_path"]:
    tex.append(get_features(path))
df_test["x"] = tex
In [15]:
df_train.head(5)
Out[15]:
x_path y x
0 /home/simon/CDE_UBS/Image_Analysis/03_Image_Fe... river [0.0012447339431267647, 213.6953739032691, 0.7...
1 /home/simon/CDE_UBS/Image_Analysis/03_Image_Fe... river [0.0002697358640565882, 671.6049101314522, 0.7...
2 /home/simon/CDE_UBS/Image_Analysis/03_Image_Fe... river [0.0014521793386555479, 261.5884032588976, 0.8...
3 /home/simon/CDE_UBS/Image_Analysis/03_Image_Fe... river [0.00016310613691894715, 302.30642994773154, 0...
4 /home/simon/CDE_UBS/Image_Analysis/03_Image_Fe... river [0.0007591564715914901, 606.6542579302326, 0.7...
In [16]:
df_test.head(5)
Out[16]:
x_path y x
0 /home/simon/CDE_UBS/Image_Analysis/03_Image_Fe... river [0.000854585229168331, 802.7071070005217, 0.85...
1 /home/simon/CDE_UBS/Image_Analysis/03_Image_Fe... buildings [0.0002937545387317787, 392.2858152413333, 0.7...
2 /home/simon/CDE_UBS/Image_Analysis/03_Image_Fe... intersection [0.00021946310388044858, 534.7896318158239, 0....
3 /home/simon/CDE_UBS/Image_Analysis/03_Image_Fe... overpass [0.0017166742149301141, 2099.2503651676557, 0....
4 /home/simon/CDE_UBS/Image_Analysis/03_Image_Fe... beach [0.0006261706037242413, 1244.860437159545, 0.7...

Create NN-CLass Model

In [17]:
x_train = df_train["x"]
x_train = [l.tolist() for l in x_train]
y_train = list(df_train["y"])
In [ ]:
 
In [18]:
model = KNeighborsClassifier(n_neighbors=7)
model.fit(x_train,y_train)
Out[18]:
KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
                     metric_params=None, n_jobs=None, n_neighbors=7, p=2,
                     weights='uniform')
In [19]:
pred = []
for x in df_test["x"]:
    x = x.reshape(1, -1)
    pred.append(model.predict(x))
df_test["y_pred"] = pred
In [20]:
c = 0
for path,y,y_pred in zip(df_test["x_path"],df_test["y"],df_test["y_pred"]):
    plt.imshow(cv2.imread(path))
    plt.title("True: "+y+" Pred: "+str(y_pred[0]))
    plt.show()
    c = c+1
    if c==5:
        break
    
    
In [ ]:
 
In [ ]:
 

Leave a Reply

Your email address will not be published. Required fields are marked *

Further Reading
Recent Updates