scientific_comp_projects/CODE/[python]thesis_old_scripts/hough_transforms.py

113 lines
3.5 KiB
Python

# -*- coding: utf-8 -*-
"""
Created on Mon Jun 4 13:31:41 2018
@author: Armando
"""
import ReadIM
import matplotlib.pyplot as plt
import numpy as np
from skimage import data, color, img_as_ubyte
from skimage.feature import canny
from skimage.transform import hough_ellipse
from skimage.draw import ellipse_perimeter
from PIL import Image
# Load picture, convert to grayscale and detect edges
image_rgb = Image.open("sphx_glr_plot_circular_elliptical_hough_transform_002.png")
image_gray = image_rgb.convert('L')
modif_array = np.array(image_gray,dtype = float)
edges = canny(modif_array, sigma=2.0,
low_threshold=0.55, high_threshold=0.8)
# Perform a Hough Transform
# The accuracy corresponds to the bin size of a major axis.
# The value is chosen in order to get a single high accumulator.
# The threshold eliminates low accumulators
result = hough_ellipse(edges, accuracy=20, threshold=250,
min_size=100, max_size=120)
result.sort(order='accumulator')
# Estimated parameters for the ellipse
best = list(result[-1])
yc, xc, a, b = [int(round(x)) for x in best[1:5]]
orientation = best[5]
# Draw the ellipse on the original image
cy, cx = ellipse_perimeter(yc, xc, a, b, orientation)
image_rgb[cy, cx] = (0, 0, 255)
# Draw the edge (white) and the resulting ellipse (red)
edges = color.gray2rgb(img_as_ubyte(edges))
edges[cy, cx] = (250, 0, 0)
fig2, (ax1, ax2) = plt.subplots(ncols=2, nrows=1, figsize=(8, 4),
sharex=True, sharey=True)
ax1.set_title('Original picture')
ax1.imshow(image_rgb)
ax2.set_title('Edge (white) and result (red)')
ax2.imshow(edges)
plt.show()
'''
vbuff, vatts = ReadIM.extra.get_Buffer_andAttributeList(working_dir+ image_to_process)
v_array, vbuff = ReadIM.extra.buffer_as_array(vbuff)
original_array = v_array[0][cut_image_y0:len(v_array[0])-cut_image_yf,cut_image_x0:len(v_array[0][0])-cut_image_xf]
original_array = np.array(original_array,dtype = float)
modif_array = np.array(original_array,dtype = float)
for y in range(len(original_array)-1):
for x in range(len(original_array[0])-1):
if original_array[y][x] >= y_max:
modif_array[y][x] = 12
elif original_array[y][x] <= y_min:
modif_array[y][x] = 4
else:
modif_array[y][x] = inverse_tanhfit(original_array[y][x])
modif_array = 207608319.9386*np.power(modif_array,-9.7399)
modif_array[modif_array<0.1] = 0
modif_array[modif_array>8.0] = 0
'''
#%%
colormap = plt.get_cmap('Greys')
interpolation = 'bicubic'
modif_array = np.array(image_gray,dtype = float)
minima = modif_array.max()
maxima = modif_array.max()
fig = plt.figure()
a1 = fig.add_subplot(1, 2, 1)
#Plotting of the image
im = plt.imshow(image_gray, interpolation=interpolation, cmap=colormap,origin='upper',vmin=minima, vmax=maxima)
a1.set_title('Test image')
plt.colorbar(im, orientation='horizontal')
#norm = colors.LogNorm(vmin=minima, vmax=maxima)
a2 = fig.add_subplot(1, 2, 2)
histo = plt.hist(modif_array, bins=10,range = (minima,maxima), fc='k', ec='k' )
#
#**np.linspace(np.log10(minima), np.log10(maxima), 10)
a2.set_title('Compare and contrast')
'''
a3 = fig.add_subplot(1, 2, 2)
im2 = plt.imshow(modif_array, interpolation='bicubic', cmap=plt.get_cmap('viridis'), \
origin='upper',vmin=0.4, vmax=10)
a3.set_title('No interpolation')
plt.colorbar(im2, orientation='horizontal')
'''
plt.tight_layout()
plt.show()