Skip to content

Commit 40b813e

Browse files
committed
Release of Python source code and modifications in Matlab source code
Release of Python source code for PST. Modifications in Matlab source code are the following: 1) Added imwrite functionality to save generated edge map and overlay output. 2) The Test_Images directory is created to load example images and run PST.
1 parent 5740b6d commit 40b813e

16 files changed

+419
-27
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Mac generated files
2+
._*
3+
# Matlab generated files
4+
*.asv
5+
*.m~
6+
# Python generated directory
7+
__pycache__

Matlab/.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Mac generated files
2+
._*
3+
# Matlab generated files
4+
*.asv
5+
*.m~
6+
# Python generated directory
7+
__pycache__

PST.m renamed to Matlab/PST.m

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
% Implementation of Phase Stretch Transform (PST) in Matlab
2+
% author: M. Asghari, Jalali Lab, Department of Electrical and Computer Engineering, UCLA
13
function [out PST_Kernel]= PST(I,handles,Morph_flag)
24
%PST or Phase Stretch Transform is an operator that finds features in an
35
% image. PST takes an intensity image I as its input, and returns a
@@ -54,7 +56,7 @@
5456
% figure, imshow(out)
5557
%
5658
% Example 2: Find the features in the Lena image. See the attached test script.
57-
%
59+
%
5860
% Copyright
5961
% ---------
6062
% PST function is developed in Jalali Lab at University of California,
@@ -67,7 +69,8 @@
6769
% Citations:
6870
% 1. M. H. Asghari, and B. Jalali, "Edge detection in digital images using dispersive phase stretch," International Journal of Biomedical Imaging, Vol. 2015, Article ID 687819, pp. 1-6 (2015).
6971
% 2. M. H. Asghari, and B. Jalali, "Physics-inspired image edge detection," IEEE Global Signal and Information Processing Symposium (GlobalSIP 2014), paper: WdBD-L.1, Atlanta, December 2014.
70-
%
72+
% 3. M. Suthar, H. Asghari, and B. Jalali, "Feature Enhancement in Visually Impaired Images", IEEE Access 6 (2018): 1407-1415.
73+
% 4. Y. Han, and B. Jalali, "Photonic time-stretched analog-to-digital converter: Fundamental concepts and practical considerations", Journal of Lightwave Technology 21, no. 12 (2003): 3085.
7174
% Copyright 1992-2016 The MathWorks, Inc.
7275
% $Revision: 0.0.0.1 $ $Date: 2016/02/09 13:20:56 $
7376

Matlab/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Instructions
2+
#
3+
## Remarks
4+
##
5+
Image processing toolbox is needed to run this function, function has been tested on MATLAB R2013a on a computer with Windows 7, 64 bits operating system. The code uses IMOVERLAY function developed by Steven L. Eddins for visualization of detected features.
6+
The test image is loaded in line #8 in the test_script_PST.m file. The test images can be called from the Test_Images directory and then, set the file name in the code on line #8.
7+
The images can be color or greyscale. However, PST operation occurs on color images only after converting them to greyscale.
8+
9+
## Copyright
10+
##
11+
PST function using MATLAB is developed in Jalali Lab at University of California, Los Angeles (UCLA). PST is a spin-off from research on the photonic time stretch technique in Jalali lab at UCLA. More information about the technique can be found on our group website: http://www.photonics.ucla.edu
12+
13+
This function is provided for research purposes only. A license must be obtained from the University of California, Los Angeles for any commercial applications. The software is protected under a US patent.
14+
15+
## Citations
16+
##
17+
1. M. H. Asghari, and B. Jalali, "Edge detection in digital images using dispersive phase stretch," International Journal of Biomedical Imaging, Vol. 2015, Article ID 687819, pp. 1-6 (2015).
18+
2. M. H. Asghari, and B. Jalali, "Physics-inspired image edge detection," IEEE Global Signal and Information Processing Symposium (GlobalSIP 2014), paper: WdBD-L.1, Atlanta, December 2014.
19+
3. M. Suthar, H. Asghari, and B. Jalali, "Feature Enhancement in Visually Impaired Images", IEEE Access 6 (2018): 1407-1415.
20+
4. Y. Han, and B. Jalali, "Photonic time-stretched analog-to-digital converter: Fundamental concepts and practical considerations", Journal of Lightwave Technology 21, no. 12 (2003): 3085.
21+
22+
Copyright (c) 2016, Jalali Lab All rights reserved.
File renamed without changes.

test_script_PST_feb_02_2016.m renamed to Matlab/test_script_PST.m

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
% test script to test PST function
1+
% Implementation of Phase Stretch Transform (PST) in Matlab
2+
% author: M. Asghari and Madhuri Suthar, Jalali Lab, Department of Electrical and Computer Engineering, UCLA
23

34
clc % clear screen
45
clear all % clear all variables
56
close all % close all figures
67

78
% import original image
8-
Image_orig=imread('lena_gray_512.tif');
9+
filename = 'cameraman.tif';
10+
filepath =strcat('../Test_Images/',filename);
11+
Image_orig=imread(filepath);
912

1013
% if image is a color image, convert it to grayscale
1114
try
@@ -43,17 +46,26 @@
4346
subplot(1,2,2)
4447
imshow(Edge/max(max(Edge))*3)
4548
title('Detected features using PST')
46-
49+
% save the image
50+
file_and_extension=strsplit(filename,'.');
51+
output_path=char(strcat('../Test_Images/',file_and_extension(1),'_edge.tif'));
52+
imwrite(Edge/max(max(Edge))*3,output_path);
4753
else
4854
subplot(1,2,2)
4955
imshow(Edge)
5056
title('Detected features using PST')
51-
57+
% save the image
58+
file_and_extension=strsplit(filename,'.');
59+
output_path=char(strcat('../Test_Images/',file_and_extension(1),'_edge.tif'));
60+
imwrite(Edge,output_path);
5261
% overlay original image with detected features
5362
overlay = double(imoverlay(Image_orig, Edge/1000000, [1 0 0]));
5463
figure
5564
imshow(overlay/max(max(max(overlay))));
5665
title('Detected features using PST overlaid with original image')
66+
% save the image
67+
output_path=char(strcat('../Test_Images/',file_and_extension(1),'_overlay.tif'));
68+
imwrite(overlay/max(max(max(overlay))),output_path);
5769
end
5870

5971
% show the PST phase kernel gradient

Python/.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Mac generated files
2+
._*
3+
# Matlab generated files
4+
*.asv
5+
*.m~
6+
# Python generated directory
7+
__pycache__

Python/PST_function.py

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""
4+
Implementation of Phase Stretch Transform (PST) in Python
5+
@author: Madhuri Suthar, Ph.D. candidate, Jalali Lab, Department of Electrical and Computer Engineering, UCLA
6+
7+
PST or Phase Stretch Transform is a physics-inspired edge detection algorithm that detects intensity variations in an image [1,2].
8+
PST operates on an input greyscale image and outputs an edge map. The output egde map, same as the size of the input image, is binary with
9+
pixel value equal to 1 where the PST operator finds sharp transitions in intensity and 0 elsewhere. The PST operator can also return
10+
a continous level edge map (i.e. without thresholding and morphological operations)
11+
12+
The PST operator cascades Gaussian smoothing, application of a nonlinear frequency-dependent phase kernel in frequency domain and a phase detection in spatial domain.
13+
To implement the first step, an isotropic gaussian filter with a user defined scale (LPF) is designed and operated on the image.
14+
Next, a 2D PST phase kernel is designed in frequency domain and applied to the spectrum of the input image. The output of the transform is the
15+
phase in the spatial domain. The amount of phase applied to the image is frequency dependent with higher amount of phase applied to higher frequency features of the
16+
image. Since sharp transitions, such as edges and corners, contain higher frequencies, PST emphasizes the edge information. Features can
17+
be further enhanced by applying thresholding and morphological operations.
18+
For more information please visit: https://en.wikipedia.org/wiki/Phase_stretch_transform
19+
20+
[out PST_Kernel]= PST(Image,LPF,Phase_strength,Warp_strength, Threshold_min, Threshold_max, Morph_flag) takes the image I and applies
21+
PST phase kernel parameters are described as follows:
22+
23+
Parameters
24+
----------
25+
LPF : Isotropic Gaussian localization filter Full Width at Half Maximum (FWHM) (min : 0, max : 1)
26+
Phase_strength : PST Kernel Phase Strength (min : 0, max : 1)
27+
Warp_strength : PST Kernel Warp Strength (min : 0, max : 1)
28+
Threshold_min : minimum threshold (min : -1, max : 0)
29+
Threshold_max : maximum threshold (min : 0, max : 1)
30+
Morph_flag allows user to compute the analog edge (if Morph_flag=0) or the digital edge (analog edge followed
31+
by thresholding and morphological operations, if Morph_flag=1).
32+
33+
Copyright
34+
---------
35+
PST function is developed in Jalali Lab at University of California,
36+
Los Angeles (UCLA). PST is a spin-off from research on the photonic time stretch technique in Jalali lab at UCLA.
37+
More information about the technique can be found in our group
38+
website: http://www.photonics.ucla.edu
39+
This function is provided for research purposes only. A license must be
40+
obtained from the University of California, Los Angeles for any commercial
41+
applications. The software is protected under a US patent.
42+
43+
Citations
44+
---------
45+
1. M. H. Asghari, and B. Jalali, "Edge detection in digital images using dispersive phase stretch," International Journal of Biomedical Imaging, Vol. 2015, Article ID 687819, pp. 1-6 (2015).
46+
2. M. H. Asghari, and B. Jalali, "Physics-inspired image edge detection," IEEE Global Signal and Information Processing Symposium (GlobalSIP 2014), paper: WdBD-L.1, Atlanta, December 2014.
47+
3. M. Suthar, H. Asghari, and B. Jalali, "Feature Enhancement in Visually Impaired Images", IEEE Access 6 (2018): 1407-1415.
48+
4. Y. Han, and B. Jalali, "Photonic time-stretched analog-to-digital converter: Fundamental concepts and practical considerations", Journal of Lightwave Technology 21, no. 12 (2003): 3085.
49+
"""
50+
# Imports
51+
# [] Need to install mahotas library for morphological operations
52+
import math
53+
import numpy as np
54+
import mahotas as mh
55+
56+
# Define functions
57+
def cart2pol(x, y):
58+
theta = np.arctan2(y, x)
59+
rho = np.hypot(x, y)
60+
return (theta, rho)
61+
def PST(I,LPF,Phase_strength,Warp_strength, Threshold_min, Threshold_max, Morph_flag):
62+
L=0.5
63+
x = np.linspace(-L, L, I.shape[0])
64+
y = np.linspace(-L, L, I.shape[1])
65+
[X1, Y1] =(np.meshgrid(x, y))
66+
X=X1.T
67+
Y=Y1.T
68+
[THETA,RHO] = cart2pol(X,Y)
69+
70+
# Apply localization kernel to the original image to reduce noise
71+
Image_orig_f=((np.fft.fft2(I)))
72+
expo = np.fft.fftshift(np.exp(-np.power((np.divide(RHO, math.sqrt((LPF**2)/np.log(2)))),2)))
73+
Image_orig_filtered=np.real(np.fft.ifft2((np.multiply(Image_orig_f,expo))))
74+
# Constructing the PST Kernel
75+
PST_Kernel_1=np.multiply(np.dot(RHO,Warp_strength), np.arctan(np.dot(RHO,Warp_strength)))-0.5*np.log(1+np.power(np.dot(RHO,Warp_strength),2))
76+
PST_Kernel=PST_Kernel_1/np.max(PST_Kernel_1)*Phase_strength
77+
# Apply the PST Kernel
78+
temp=np.multiply(np.fft.fftshift(np.exp(-1j*PST_Kernel)),np.fft.fft2(Image_orig_filtered))
79+
Image_orig_filtered_PST=np.fft.ifft2(temp)
80+
81+
# Calculate phase of the transformed image
82+
PHI_features=np.angle(Image_orig_filtered_PST)
83+
84+
if Morph_flag ==0:
85+
out=PHI_features
86+
else:
87+
# find image sharp transitions by thresholding the phase
88+
features = np.zeros((PHI_features.shape[0],PHI_features.shape[1]))
89+
features[PHI_features> Threshold_max] = 1 # Bi-threshold decision
90+
features[PHI_features< Threshold_min] = 1 # as the output phase has both positive and negative values
91+
features[I<(np.amax(I)/20)]=0 # Removing edges in the very dark areas of the image (noise)
92+
93+
# apply binary morphological operations to clean the transformed image
94+
out = features
95+
out = mh.thin(out, 1)
96+
out = mh.bwperim(out, 4)
97+
out = mh.thin(out, 1)
98+
out = mh.erode(out, np.ones((1, 1)));
99+
100+
return (out, PST_Kernel)

Python/README.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Instructions for Python 3.x
2+
#
3+
## Prerequisites
4+
##
5+
You can install most of the following packages using pip.
6+
* NumPy
7+
* Python Imaging Library (PIL)
8+
* Matplotlib
9+
* Itertools
10+
* Mahotas
11+
12+
## Remarks
13+
##
14+
Mahotas library is needed to implement morphological operations and display overlay in case of digital edge detection (if Morph_flag==1).
15+
16+
## Testing
17+
##
18+
Put your test images in the Test_Images directory and then, set the file path in the code on line 70. You can also pass the filename using the command line [Uncomment lines 64 and 72 and comment line 70].
19+
The images can be color or greyscale. However, PST operation occurs on color images only after converting them to greyscale. You can change the filename in the test_script_PST.py code to run the algorithm on the test image.
20+
21+
## Visualization
22+
##
23+
The code uses matplotlib and mahouts to visualize PST edge map and overlay (in case of digital edge). [See section # Display results in test_script_PST.py]
24+
25+
## Test Results
26+
The PST edge map and overlay (in case of digital edge when Morph_flag==1) are saved in the Test_Images folder. [See section # Save results test_script_PST.py]
27+
28+
## Copyright
29+
##
30+
PST function is developed in Jalali Lab at University of California, Los Angeles (UCLA). PST is a spin-off from research on the photonic time stretch technique in Jalali lab at UCLA. More information about the technique can be found on our group website: http://www.photonics.ucla.edu
31+
This function is provided for research purposes only. A license must be obtained from the University of California, Los Angeles for any commercial applications. The software is protected under a US patent.
32+
33+
34+
## Citations
35+
##
36+
1. M. H. Asghari, and B. Jalali, "Edge detection in digital images using dispersive phase stretch," International Journal of Biomedical Imaging, Vol. 2015, Article ID 687819, pp. 1-6 (2015).
37+
2. M. H. Asghari, and B. Jalali, "Physics-inspired image edge detection," IEEE Global Signal and Information Processing Symposium (GlobalSIP 2014), paper: WdBD-L.1, Atlanta, December 2014.
38+
3. M. Suthar, H. Asghari, and B. Jalali, "Feature Enhancement in Visually Impaired Images", IEEE Access 6 (2018): 1407-1415.
39+
4. Y. Han, and B. Jalali, "Photonic time-stretched analog-to-digital converter: Fundamental concepts and practical considerations", Journal of Lightwave Technology 21, no. 12 (2003): 3085.
40+
41+
Copyright (c) 2016, Jalali Lab All rights reserved.

0 commit comments

Comments
 (0)