Open Shahryar3079 opened 6 months ago
Also there are many issues with plotting. Should we modify the whole code according to new python libraries?
import cv2 import numpy as np from matplotlib import pyplot as plt import glob import matplotlib.cm as cm import matplotlib.colors as mcolors import matplotlib.colorbar as mcolorbar from scipy import signal as sg import os from PyQt5.QtCore import pyqtRemoveInputHook import time import pdb
def template_match(img_master, img_slave, method='cv2.TM_CCOEFF_NORMED', mlx=1, mly=1, show=True):
img_master = cv2.resize(img_master, None, fx=mlx, fy=mly, interpolation=cv2.INTER_CUBIC)
img_slave = cv2.resize(img_slave, None, fx=mlx, fy=mly, interpolation=cv2.INTER_CUBIC)
res = cv2.matchTemplate(img_slave, img_master, eval(method))
w, h = img_master.shape[::-1]
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
# Control if the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum value
if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
top_left = min_loc
else:
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
# Retrieve center coordinates
px = (top_left[0] + bottom_right[0]) / (2.0 * mlx)
py = (top_left[1] + bottom_right[1]) / (2.0 * mly)
# Visualization of matching results
if show:
# Scale images for visualization
img_master_scaled = cv2.convertScaleAbs(img_master, alpha=(255.0 / 500))
img_slave_scaled = cv2.convertScaleAbs(img_slave, alpha=(255.0 / 500))
cv2.rectangle(img_slave_scaled, top_left, bottom_right, 255, int(2 * mlx))
plt.figure(figsize=(20, 10))
plt.subplot(131), plt.imshow(res, cmap='gray')
plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
plt.subplot(132), plt.imshow(img_master_scaled, cmap='gray')
plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
plt.subplot(133), plt.imshow(img_slave_scaled, cmap='gray')
plt.suptitle(method)
plt.show()
return px, py, max_val
def template_match_cv_OCL_transp_API(img_master, img_slave, method='cv2.TM_CCOEFF_NORMED', mlx=1, mly=1, show=True):
w = img_master.shape[1] * mlx
h = img_master.shape[0] * mly
# Convert the input images in UMat images (GPU) thanks to Open Computing Language (OpenCL)
img_slave = cv2.UMat(img_slave)
img_master = cv2.UMat(img_master)
# Apply image oversampling
img_master = cv2.resize(img_master, None, fx=mlx, fy=mly, interpolation=cv2.INTER_CUBIC)
img_slave = cv2.resize(img_slave, None, fx=mlx, fy=mly, interpolation=cv2.INTER_CUBIC)
res = cv2.matchTemplate(img_slave, img_master, eval(method))
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
# Control if the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum value
if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
top_left = min_loc
else:
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
# Retrieve center coordinates
px = (top_left[0] + bottom_right[0]) / (2.0 * mlx)
py = (top_left[1] + bottom_right[1]) / (2.0 * mly)
# Visualization of matching results
if show:
# Scale images for visualization
img_master_scaled = cv2.convertScaleAbs(img_master, alpha=(255.0 / 500))
img_slave_scaled = cv2.convertScaleAbs(img_slave, alpha=(255.0 / 500))
cv2.rectangle(img_slave_scaled, top_left, bottom_right, 255, int(2 * mlx))
# convert back from Umat (GPU) to numpy array for matplotlib
img_master_scaled = cv2.UMat.get(img_master_scaled)
img_slave_scaled = cv2.UMat.get(img_slave_scaled)
plt.figure(figsize=(20, 10))
plt.subplot(131), plt.imshow(res, cmap='gray')
plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
plt.subplot(132), plt.imshow(img_master_scaled, cmap='gray')
plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
plt.subplot(133), plt.imshow(img_slave_scaled, cmap='gray')
plt.suptitle(method)
plt.show()
return px, py, max_val
def DIC(images_absolute_path, format, vel, dim_pixel, frame_rate, start_index, levels, image_time_sampling, temp_dim, b, d, recty1, recty2, rectx1, rectx2, c=0): start_time = time.time() ml_x = 10 ml_y = 10
# Threshold values set for the Plate Hole DIC Challenge image collection
dx_thresh_inf = -1.35
dx_thresh_sup = 0
dy_thresh_inf = -12.4
dy_thresh_sup = -4.5
# Checking that the template dimension is odd
if int(temp_dim) % 2 == 0:
temp_dim += 1
plt.close("all")
plt.switch_backend("Qt5Agg")
# Find the last folder, which we assume being the name of the test
test_name = os.path.basename(os.path.normpath(images_absolute_path))
initial_start_index = start_index
print('dentro DIC', recty1, recty2, rectx1, rectx2, images_absolute_path)
print('Backend: {}'.format(plt.get_backend()))
print('Test:', test_name)
plt.rc('text', usetex=False)
msg = test_name + "\n"
msg += '-----------\n'
msg += "Imposed deformation velocity\n"
msg += str(vel) + " mm/min\n"
# Convert velocity units from mm/min to mm/s
vel = vel / 60
format = '.' + format
img_names = sorted(glob.glob(os.path.join(images_absolute_path, "*" + format)))
img1 = cv2.imread(img_names[0], cv2.IMREAD_GRAYSCALE)
if recty1 is not None and recty2 is not None and rectx1 is not None and rectx2 is not None:
# otherwise the AOI is all the image
print('p2', rectx2, recty2)
print('p1', rectx1, recty1)
Dim_y = recty2 - recty1
Dim_x = rectx2 - rectx1
else:
Dim_x = np.shape(img1)[1] # width
Dim_y = np.shape(img1)[0] # height
if np.shape(img1)[1] > np.shape(img1)[0]:
# In this case the images have a horizontal layout
# The x axis corresponds to the image columns
# The y axis corresponds to the image rows
boh = d
d = b
b = boh
boh = ml_x
ml_x = ml_y
ml_y = boh
dy_thresh_inf = -1.35
dy_thresh_sup = 0
dx_thresh_inf = 4.5 # np.min(dx)#- 1.35
dx_thresh_sup = 12.4 # np.max(dx)#0
print("Dim x:", Dim_x)
print("Dim y:", Dim_y)
# Dimensions of the matching grid
h = int(Dim_y / (temp_dim + 2 * b + c)) # rows
w = int(Dim_x / (temp_dim + 2 * d + c)) # columns
print('h grid rows', h)
print('w grid cols', w)
# Array where to store the results
# we cumulate the displacements computed for each level
results = np.zeros((h * w, 6))
results_mm = np.zeros((h * w, 6))
for l in range(levels):
stop_index = start_index + image_time_sampling
print(stop_index, start_index, image_time_sampling, initial_start_index)
msg = ''
print("level", l + 1)
msg = msg + '-----------'
msg = msg + "\nAnalysed images"
msg = msg + "\n1: " + os.path.basename(str(img_names[start_index]))
msg = msg + "\n2: " + os.path.basename(str(img_names[stop_index]))
print(msg)
delta_index = stop_index - start_index
tempo_passato = delta_index / frame_rate # seconds
spost_atteso_mm = vel * tempo_passato # mm
spost_atteso_pixel = round(spost_atteso_mm / dim_pixel, 0) # pixel
print('-----------')
print("Expected displacement")
print(spost_atteso_mm, 'mm ', spost_atteso_pixel, ' pixel')
msg = msg + '\n-----------\n'
msg = msg + "Expected displacement\n"
msg = msg + str(spost_atteso_mm) + ' mm ' + str(spost_atteso_pixel) + ' pixel\n'
img1 = cv2.imread(img_names[start_index], cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread(img_names[stop_index], cv2.IMREAD_GRAYSCALE)
if recty1 is None:
crop_img1 = img1
crop_img2 = img2
else:
crop_img1 = img1[recty1:recty2, rectx1:rectx2]
crop_img2 = img2[recty1:recty2, rectx1:rectx2]
print('-----------')
print("Camera resolution")
print(Dim_x, "x", Dim_y)
print('-----------')
print("Camera frame rate")
print(frame_rate, "fps (one shot every", 1 / frame_rate, "seconds)")
msg = msg + '\n-----------\n'
msg = msg + "Camera resolution\n"
msg = msg + str(Dim_x) + "x" + str(Dim_y)
msg = msg + '\n-----------\n'
msg = msg + "Camera frame rate\n"
msg = msg + str(frame_rate) + " fps (one shot every " + str(1 / frame_rate) + " seconds)"
########### TEMPLATE PARAMETERS ################
# matchZoneWidth should be greater than the maximum displacement expected
k = 0
img1r = crop_img1.copy()
img2r = crop_img2.copy()
for j in range(h): # loop on rows (Y)
for i in range(w): # loop on columns (X)
Delta_X = i * (c + 2 * d + temp_dim)
Delta_Y = j * (c + 2 * b + temp_dim)
TP_temp_x = Delta_X + c + d + (temp_dim - 1) / 2.0 # x start
TP_temp_y = Delta_Y + c + b + (temp_dim - 1) / 2.0 # y start
start_x_template_slice = c + d + Delta_X
stop_x_template_slice = c + d + Delta_X + temp_dim
start_y_template_slice = c + b + Delta_Y
stop_y_template_slice = c + b + Delta_Y + temp_dim
shape_template = np.shape(img1r[start_y_template_slice:stop_y_template_slice, start_x_template_slice:stop_x_template_slice])
# checking the template dimensions
assert np.allclose(shape_template[0], temp_dim)
assert np.allclose(shape_template[1], temp_dim)
assert np.allclose(stop_y_template_slice - start_y_template_slice, temp_dim)
assert np.allclose(stop_x_template_slice - start_x_template_slice, temp_dim)
assert np.allclose(TP_temp_x, (start_x_template_slice + stop_x_template_slice) // 2.0)
assert np.allclose(TP_temp_y, (start_y_template_slice + stop_y_template_slice) // 2.0)
start_x_search_slice = c + Delta_X
stop_x_search_slice = c + Delta_X + 2 * d + temp_dim
start_y_search_slice = c + Delta_Y
stop_y_search_slice = c + Delta_Y + 2 * b + temp_dim
shape_search = np.shape(img2r[start_y_search_slice:stop_y_search_slice, start_x_search_slice:stop_x_search_slice])
# checking the search area dimensions
assert np.allclose((shape_search[0] - temp_dim) / 2.0, b)
assert np.allclose((shape_search[1] - temp_dim) / 2.0, d)
assert np.allclose(TP_temp_x, (start_x_search_slice + stop_x_search_slice) // 2.0)
assert np.allclose(TP_temp_y, (start_y_search_slice + stop_y_search_slice) // 2.0)
temp = img1r[start_y_template_slice:stop_y_template_slice, start_x_template_slice:stop_x_template_slice]
search_area = img2r[start_y_search_slice:stop_y_search_slice, start_x_search_slice:stop_x_search_slice]
indx, indy, maxcc = template_match(temp.astype('uint8'), search_area.astype('uint8'), mlx=ml_x, mly=ml_y, show=False)
# indx, indy, maxcc = template_match_cv_OCL_transp_API(temp, search_area, mlx=ml_x, mly=ml_y, show=False)
TP_search_x = Delta_X + c + indx - 0.5 # end point x
TP_search_y = Delta_Y + c + indy - 0.5 # end point y
# Convert the pixel results into millimeters
results_mm[k, 0] = TP_temp_x # start point x [pixel]
results_mm[k, 1] = TP_temp_y # start point y [pixel]
results_mm[k, 2] = TP_search_x * dim_pixel - TP_temp_x * dim_pixel + results_mm[k, 2] # dx [mm]
results_mm[k, 3] = TP_search_y * dim_pixel - TP_temp_y * dim_pixel + results_mm[k, 3] # dy [mm]
results_mm[k, 4] = np.sqrt((results_mm[k, 3]) ** 2 + (results_mm[k, 2]) ** 2) # displ. modulo [mm]
results_mm[k, 5] = maxcc + results_mm[k, 5]
k = k + 1
start_index = stop_index
dx = results_mm[:, 2].copy()
dy = results_mm[:, 3].copy()
dx.shape = (h, w) # the computed displacements have the dimensions of the matching grid
dy.shape = (h, w)
# Derivative kernel
operatore = np.array([-1., 0, 1.])
print('-----------')
print("Used derivative kernel")
print("dx ", operatore)
print("dy ", operatore.T)
print('-----------')
msg = msg + '-----------\n'
msg = msg + "Used derivative kernel\n"
msg = msg + "dx " + str(operatore) + "\n"
msg = msg + "dy " + str(operatore.T)
msg = msg + '\n-----------\n'
# Convolution for derivative computation
# Divide by the grid step
# The grid (dx-dy) comes from a bigger grid
# http://docs.scipy.org/doc/scipy-0.16.1/reference/generated/scipy.signal.convolve.html
# Gx = sg.convolve(dx, -operatore/[(2*(2*d+temp_dim+c))*dim_pixel], mode='same')
# Gy = sg.convolve(dy, -operatore.T/[(2*(2*b+temp_dim+c))*dim_pixel], mode='same')
Gx = sg.convolve(dx, -operatore / (2 * (2 * d + temp_dim + c) * dim_pixel), mode='same')
Gy = sg.convolve(dy, -operatore.T / (2 * (2 * b + temp_dim + c) * dim_pixel), mode='same') # Multiply by the pixel dimensions to have a millimeters value
# Printing the files for the single level, before adding the NaNs for visualization purposes
np.savetxt("OutputPlots/" + test_name + "_results_mm_dx_" + str(initial_start_index) + "_" + str(stop_index) + ".txt", dx, fmt='%.5f')
np.savetxt("OutputPlots/" + test_name + "_results_mm_dy_" + str(initial_start_index) + "_" + str(stop_index) + ".txt", dy, fmt='%.5f')
np.savetxt("OutputPlots/" + test_name + "_results_mm_Gy_" + str(initial_start_index) + "_" + str(stop_index) + ".txt", Gy, fmt='%.7f')
np.savetxt("OutputPlots/" + test_name + "_results_mm_Gx_" + str(initial_start_index) + "_" + str(stop_index) + ".txt", Gx, fmt='%.7f')
# PLOTS
# threshold values set for the Plate Hole DIC Challenge image collection
soglia_inf_y = dy_thresh_inf # np.min(dy)#-12.4
soglia_sup_mm_y = dy_thresh_sup # np.max(dy)#- 4.5#np.inf#levels*spost_atteso_pixel*dim_pixel*1.1
soglia_inf_x = dx_thresh_inf # np.min(dx)#- 1.35
soglia_sup_mm_x = dx_thresh_sup # np.max(dx)#0
soglia_inf_Gx = -4 / 100.0
soglia_sup_Gx = 4.5 / 100.0
soglia_inf_Gy = -4 / 100.0
soglia_sup_Gy = 4 / 100.0
indici_inf = np.where(dy < soglia_inf_y) # Consider only positive dy
ix = indici_inf[1] # x index
iy = indici_inf[0] # y index
dy[iy, ix] = np.nan
indici_sup = np.where(dy > soglia_sup_mm_y) # Consider only positive dy
ix = indici_sup[1] # x index
iy = indici_sup[0] # y index
dy[iy, ix] = np.nan
indici_inf = np.where(dx < soglia_inf_x) # Consider only positive dy
ix = indici_inf[1] # x index
iy = indici_inf[0] # y index
dx[iy, ix] = np.nan
indici_sup = np.where(dx > soglia_sup_mm_x) # Consider only positive dy
ix = indici_sup[1] # x index
iy = indici_sup[0] # y index
dx[iy, ix] = np.nan
fig = plt.figure("Displacements dx between img " + str(initial_start_index) + " and img " + str(stop_index))
plt.title("$Horizontal\;displacements: u$")
plt.gca().set_aspect('equal', adjustable='box')
plt.imshow(dx, cmap=cm.jet, norm=mcolors.Normalize(vmin=soglia_inf_x, vmax=soglia_sup_mm_x), interpolation='None')
cb = plt.colorbar()
cb.set_clim(soglia_inf_x, soglia_sup_mm_x)
cb.set_label('$mm$')
plt.savefig("OutputPlots/" + test_name + "_displacements_dx_img_" + str(initial_start_index) + "_img_" + str(stop_index) + ".png")
fig = plt.figure("Displacements dy between img " + str(initial_start_index) + " and img " + str(stop_index))
plt.title("$Vertical\;displacements: v$")
plt.gca().set_aspect('equal', adjustable='box')
plt.imshow(dy, cmap=cm.jet, norm=mcolors.Normalize(vmin=soglia_inf_y, vmax=soglia_sup_mm_y), interpolation='None')
cb2 = plt.colorbar()
cb2.set_clim(soglia_inf_y, soglia_sup_mm_y)
cb2.set_label('$mm$')
plt.savefig("OutputPlots/" + test_name + "_displacements_img_" + str(initial_start_index) + "_img_" + str(stop_index) + ".png")
plt.savefig("GIF/" + test_name + "_displacements_dy_img_" + str(initial_start_index) + "_img_" + str(stop_index) + ".png")
# Remove the absolute value of strains greater than 4%
indici_sup_def = np.where(Gy > soglia_sup_Gy)
ix = indici_sup_def[1] # x index
iy = indici_sup_def[0] # y index
Gy[iy, ix] = np.nan
indici_inf_def = np.where(Gy < soglia_inf_Gy)
ix = indici_inf_def[1] # x index
iy = indici_inf_def[0] # y index
Gy[iy, ix] = np.nan
# Remove the absolute value of strains greater than 4%
indici_sup_def = np.where(Gx > soglia_sup_Gx)
ix = indici_sup_def[1] # x index
iy = indici_sup_def[0] # y index
Gx[iy, ix] = np.nan
indici_inf_def = np.where(Gx < soglia_inf_Gx)
ix = indici_inf_def[1] # x index
iy = indici_inf_def[0] # y index
Gx[iy, ix] = np.nan
fig = plt.figure("Gx between img " + str(initial_start_index) + " and img " + str(stop_index))
plt.title("{\partial u \over \partial x}")
plt.imshow(Gx, cmap=cm.jet, interpolation='None')
plt.colorbar()
fig = plt.figure("Gy between img " + str(initial_start_index) + " and img " + str(stop_index))
plt.title("{\partial v \over \partial y}")
plt.imshow(Gy, cmap=cm.jet, interpolation='None')
plt.colorbar()
plt.show()
print(np.max(results_mm[:, 3]), np.min(results_mm[:, 3]))
# Remove strains whose absolute value is greater than soglia_sup and lower than soglia_inf
# limite_inf = np.where(results_mm2[:,3]<soglia_inf)[0]
# results_mm2 = np.delete(results_mm2, limite_inf, axis=0)
# limite_sup = np.where(results_mm2[:,3]>soglia_sup_mm)[0]
# results_mm2 = np.delete(results_mm2, limite_sup, axis=0)
print(np.nanmax(results_mm[:, 3]), np.nanmin(results_mm[:, 3]), np.nanmedian(results_mm[:, 3]), np.nanmean(results_mm[:, 3]))
# http://stackoverflow.com/questions/11970186/matplotlib-quiver-and-imshow-superimposed-how-can-i-set-two-colorbars
# http://stackoverflow.com/questions/23964856/plotting-streamlines-with-matplotlib-python
nz = mcolors.Normalize()
nz.autoscale(results_mm[:, 4])
fig = plt.figure("img " + str(initial_start_index) + " - img " + str(stop_index))
ax = fig.add_subplot(111)
plt.imshow(crop_img1, cmap=plt.cm.gray, origin='upper')
plt.title("img " + str(initial_start_index) + " - img " + str(stop_index))
ax.set_prop_cycle(color=['red', 'black', 'yellow'])
# Same scale on the x and y axes
plt.gca().set_aspect('equal', adjustable='box')
plt.ylabel('pixels')
plt.xlabel('pixels')
# Plot quiver
plt.quiver(results_mm[:, 0], # start x [pixel]
results_mm[:, 1], # start y [pixel]
results_mm[:, 2] / results_mm[:, 4], # dx / |displ| [mm/mm]
results_mm[:, 3] / results_mm[:, 4], # dy / |displ| [mm/mm]
angles='xy',
scale=30,
color=cm.jet(nz(results_mm[:, 4])),
edgecolor='k', # edge color of the quivers
linewidth=.2)
# Colorbar
cax, _ = mcolorbar.make_axes(plt.gca())
soglia_sup_prova_mm = np.nanmax(results_mm[:, 4])
soglia_inf_prova_mm = np.nanmin(results_mm[:, 4])
# vmin and vmax should be symmetric? ex: - 6 ,6
cb = mcolorbar.ColorbarBase(cax, cmap=cm.jet, norm=mcolors.Normalize(vmin=soglia_inf_prova_mm, vmax=soglia_sup_prova_mm))
cb.set_clim(soglia_inf_prova_mm, soglia_sup_prova_mm)
# cb = mcolorbar.ColorbarBase(cax, cmap=cm.jet, norm=nz)
# cb = mcolorbar.ColorbarBase(cax, cmap=cm.jet)#, norm=mcolors.Normalize(vmin=soglia_inf, vmax=soglia_sup_mm))
# cb.set_clim(soglia_inf, soglia_sup_mm)# it doesn't work
cb.set_label('mm')
plt.savefig("GIFfrec/" + test_name + "_freccette_img_" + str(initial_start_index) + "_img_" + str(stop_index) + ".png")
mng = plt.get_current_fig_manager()
# mng.window.showMaximized()
########### SECTION PLOT ###########
plt.figure("Sample central section")
plt.plot(dy[:, np.shape(dy)[1]//2]) # Ensure integer division in Python 3
plt.ylabel('y displacements (mm)')
plt.xlabel('y axis on the grid nodes (mm)')
plt.savefig("OutputPlots/" + test_name + "_sezione_img_" + str(initial_start_index) + "_img_" + str(stop_index) + ".png")
print("py2DIC execution time: %s s" % (time.time() - start_time))
plt.show()
return msg
def gif(path1, filename): imgs = [] img_names = glob.glob(path1 + "/*.png") img_names.sort() for img_name in img_names: img = cv2.imread(img_name) imgs.append(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
writeGif(path1 + filename, imgs, duration=1)
ciao = 5
import sys import os from PyQt5.QtCore import Qt from PyQt5.QtGui import from PyQt5.QtWidgets import import DIC_for_GUI as DIC_roby from matplotlib import pyplot as plt import pdb import numpy as np path = os.path.dirname(os.path.abspath(file))+'/'
results_directory = 'OutputPlots' if not os.path.exists(os.path.join(path, results_directory)): os.makedirs(os.path.join(path, results_directory))
results_directory = 'GIF' if not os.path.exists(os.path.join(path, results_directory)): os.makedirs(os.path.join(path, results_directory))
results_directory = 'GIFfrec' if not os.path.exists(os.path.join(path, results_directory)): os.makedirs(os.path.join(path, results_directory))
rectangley1 = None rectangley2 = None rectanglex1 = None rectanglex2 = None absolute_path_of_images = None
class ImageDrawPanel(QGraphicsPixmapItem): def init(self, window_width=None, window_height=None, pixmap=None, parent=None, scene=None, width_scale_ratio=1, height_scale_ratio=1): super(ImageDrawPanel, self).init() self.cont = 0 self.x, self.y = -1, -1 self.radius = 10 self.pen = QPen(Qt.SolidLine) self.pen.setColor(Qt.black) self.pen.setWidth(2) self.brush = QBrush(Qt.yellow) self.width_scale_ratio = width_scale_ratio self.height_scale_ratio = height_scale_ratio self.rect = np.zeros((2, 2)) self.window_width = window_width self.window_height = window_height
def paint(self, painter, option, widget=None):
global rectangley1
global rectangley2
global rectanglex1
global rectanglex2
rectangley1 = None
rectangley2 = None
rectanglex1 = None
rectanglex2 = None
painter.drawPixmap(0, 0, self.pixmap())
painter.setPen(self.pen)
painter.setBrush(self.brush)
if self.x >= 0 and self.y >= 0 and self.x < self.window_width and self.y < self.window_height:
painter.drawEllipse(int(self.x - self.radius), int(self.y - self.radius), int(2 * self.radius), int(2 * self.radius))
print(self.cont, self.x, self.y)
self.rect[self.cont, 0] = self.x
self.rect[self.cont, 1] = self.y
self.x, self.y = -1, -1
self.cont = self.cont + 1
if self.cont == 2:
print(self.rect)
painter.drawRect(int(self.rect[0, 0]), int(self.rect[0, 1]), int(self.rect[1, 0] - self.rect[0, 0]), int(self.rect[1, 1] - self.rect[0, 1]))
self.cont = 0
rectangley1 = int(self.rect[0, 1] / self.height_scale_ratio)
rectangley2 = int(self.rect[1, 1] / self.height_scale_ratio)
rectanglex1 = int(self.rect[0, 0] / self.width_scale_ratio)
rectanglex2 = int(self.rect[1, 0] / self.width_scale_ratio)
def mousePressEvent(self, event):
self.x = event.pos().x()
self.y = event.pos().y()
self.update()
def mouseMoveEvent(self, event):
self.x = event.pos().x()
self.y = event.pos().y()
self.update()
class Second(QMainWindow): def init(self, parent=None): super(Second, self).init(parent)
pixmap = self.openImage()
original_width = pixmap.width()
original_height = pixmap.height()
if pixmap.height() >= pixmap.width():
window_width = 500
window_height = 900
else:
window_width = 900
window_height = 500
print(window_width, window_height)
self.scene = QGraphicsScene()
self.scene.setSceneRect(0, 0, window_width, window_height)
self.imagePanel = ImageDrawPanel(scene=self.scene, window_width=window_width, window_height=window_height)
self.imagePanel.setPixmap(pixmap)
self.scene.addItem(self.imagePanel)
self.width_scale_ratio = float(window_width) / original_width
self.height_scale_ratio = float(window_height) / original_height
print('**** py2DIC by Geodesy and Geomatics Division ****')
print('Image original width', original_width, '\nwidth ratio', self.width_scale_ratio)
print('Image original height', original_height, '\nheight ratio', self.height_scale_ratio)
pixmap = pixmap.scaled(window_width, window_height)
self.imagePanel = ImageDrawPanel(scene=self.scene, window_width=window_width, window_height=window_height, width_scale_ratio=self.width_scale_ratio, height_scale_ratio=self.height_scale_ratio)
self.imagePanel.setPixmap(pixmap)
self.scene.addItem(self.imagePanel)
self.view = QGraphicsView(self.scene)
layout = QHBoxLayout()
layout.addWidget(self.view)
self.widget = QWidget()
self.widget.setLayout(layout)
self.setCentralWidget(self.widget)
self.setWindowTitle("Draw the Area of Interest (AOI)")
def openImage(self):
global absolute_path_of_images
fname = QFileDialog.getOpenFileName(self, "Open image", ".", "Image Files (*.JPG *.png *TIF)")
absolute_path_of_images = str(fname[0][:-1 * len(os.path.basename(str(fname[0])))])
if len(absolute_path_of_images) == 0:
return None
if fname[0][-3:] == 'TIF' or fname[0][-3:] == 'tif':
import cv2
tiff_img = cv2.imread(fname[0], 1)
imageQt = QImage(tiff_img, tiff_img.shape[1], tiff_img.shape[0], tiff_img.shape[1] * 3, QImage.Format_RGB888)
pxmap = QPixmap(imageQt)
else:
pxmap = QPixmap(fname[0])
return pxmap
class First(QDialog):
def __init__(self, parent=None):
super(First, self).__init__(parent)
global path
self.setWindowTitle("py2DIC by AGG")
self.scrollArea = QScrollArea(self)
self.scrollArea.setWidgetResizable(True)
self.scrollAreaWidgetContents = QWidget(self.scrollArea)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.verticalLayoutScroll = QVBoxLayout(self.scrollAreaWidgetContents)
verticalLayout = QVBoxLayout(self)
verticalLayout.addWidget(self.scrollArea)
label_image = QLabel(self.scrollAreaWidgetContents)
pixmap = QPixmap(path + '../logo_sapienza.jpg')
if pixmap.width() == 0:
path = path[:-1] + '/'
pixmap = QPixmap(path + '../logo_sapienza.jpg')
pixmap = pixmap.scaled(445, 90)
label_image.setPixmap(pixmap)
self.AOIbutton = QPushButton("Select AOI")
self.l1 = QLabel(self.scrollAreaWidgetContents)
self.l1.setText("Pixel dimension [mm]")
self.le_pixel_dimension = QLineEdit(self.scrollAreaWidgetContents)
self.le_pixel_dimension.setObjectName("DimPi")
self.le_pixel_dimension.setText("1")
self.l_frame_rate = QLabel(self.scrollAreaWidgetContents)
self.l_frame_rate.setText("Camera acquisition time [s]")
self.le_frame_rate = QLineEdit(self.scrollAreaWidgetContents)
self.le_frame_rate.setObjectName("time")
self.le_frame_rate.setText("5")
self.labformat = QLabel(self.scrollAreaWidgetContents)
self.labformat.setText("Image format (jpg, png...) - case sensitive -")
self.lformat = QLineEdit(self.scrollAreaWidgetContents)
self.lformat.setObjectName("Imformat")
self.lformat.setText("JPG")
self.l_prova = QLabel(self.scrollAreaWidgetContents)
self.l_prova.setText("Test path")
self.le_prova = QLineEdit(self.scrollAreaWidgetContents)
self.le_prova.setObjectName("prova")
self.le_prova.setText(absolute_path_of_images)
self.l2 = QLabel(self.scrollAreaWidgetContents)
self.l2.setText("Imposed deformation velocity [mm/m]")
self.le2 = QLineEdit(self.scrollAreaWidgetContents)
self.le2.setObjectName("vel def macchina")
self.le2.setText("0.5")
self.lsi = QLabel(self.scrollAreaWidgetContents)
self.lsi.setText("Start index")
self.lesi = QLineEdit(self.scrollAreaWidgetContents)
self.lesi.setObjectName("start_index")
self.lesi.setText("0")
self.lstopi = QLabel(self.scrollAreaWidgetContents)
self.lstopi.setText("Levels")
self.lestopi = QLineEdit(self.scrollAreaWidgetContents)
self.lestopi.setObjectName("levels")
self.lestopi.setText("1")
self.lsamp = QLabel(self.scrollAreaWidgetContents)
self.lsamp.setText("Image time sampling")
self.lesamp = QLineEdit(self.scrollAreaWidgetContents)
self.lesamp.setObjectName("image_time_sampling")
self.lesamp.setText("1")
self.pb = QPushButton(self.scrollAreaWidgetContents)
self.pb.setObjectName("Run")
self.pb.setText("Run")
self.l_tem_width = QLabel(self.scrollAreaWidgetContents)
self.l_tem_width.setText("Template width [pixel]")
self.le_tem_width = QLineEdit(self.scrollAreaWidgetContents)
self.le_tem_width.setObjectName("templateWidth")
self.le_tem_width.setText("9")
self.l_b = QLabel(self.scrollAreaWidgetContents)
self.l_b.setText("Edge y [pixel]")
self.le_b = QLineEdit(self.scrollAreaWidgetContents)
self.le_b.setObjectName("bordoy")
self.le_b.setText("12")
self.l_b1 = QLabel(self.scrollAreaWidgetContents)
self.l_b1.setText("Edge x [pixel]")
self.le_b1 = QLineEdit(self.scrollAreaWidgetContents)
self.le_b1.setObjectName("bordox")
self.le_b1.setText("2")
self.display = QTextBrowser(self.scrollAreaWidgetContents)
self.display.verticalScrollBar().setValue(0)
self.display.verticalScrollBar().maximum()
self.gif = QLabel(self.scrollAreaWidgetContents)
self.gif.setText("GIF")
self.rdbUno = QCheckBox("", self)
self.rdbUno.setChecked(False)
self.verticalLayoutScroll.addWidget(label_image)
self.verticalLayoutScroll.addWidget(self.AOIbutton)
self.verticalLayoutScroll.addWidget(self.l_prova)
self.verticalLayoutScroll.addWidget(self.le_prova)
self.verticalLayoutScroll.addWidget(self.labformat)
self.verticalLayoutScroll.addWidget(self.lformat)
self.verticalLayoutScroll.addWidget(self.l1)
self.verticalLayoutScroll.addWidget(self.le_pixel_dimension)
self.verticalLayoutScroll.addWidget(self.l2)
self.verticalLayoutScroll.addWidget(self.le2)
self.verticalLayoutScroll.addWidget(self.l_frame_rate)
self.verticalLayoutScroll.addWidget(self.le_frame_rate)
self.verticalLayoutScroll.addWidget(self.lsi)
self.verticalLayoutScroll.addWidget(self.lesi)
self.verticalLayoutScroll.addWidget(self.lstopi)
self.verticalLayoutScroll.addWidget(self.lestopi)
self.verticalLayoutScroll.addWidget(self.lsamp)
self.verticalLayoutScroll.addWidget(self.lesamp)
self.verticalLayoutScroll.addWidget(self.l_tem_width)
self.verticalLayoutScroll.addWidget(self.le_tem_width)
self.verticalLayoutScroll.addWidget(self.l_b)
self.verticalLayoutScroll.addWidget(self.le_b)
self.verticalLayoutScroll.addWidget(self.l_b1)
self.verticalLayoutScroll.addWidget(self.le_b1)
self.verticalLayoutScroll.addWidget(self.gif)
self.verticalLayoutScroll.addWidget(self.rdbUno)
self.verticalLayoutScroll.addWidget(self.pb)
self.verticalLayoutScroll.addWidget(self.display)
self.dialog = Second(self)
print('images path', absolute_path_of_images)
self.le_prova.setText(absolute_path_of_images)
self.AOIbutton.clicked.connect(self.on_pushButton_clicked)
self.pb.clicked.connect(self.button_click)
def on_pushButton_clicked(self):
self.dialog.show()
def appExit(self):
app.quit()
def button_click(self):
try:
global dim_pixel, rectangley1, rectangley2, rectanglex1, rectanglex2 # Accessing global variables
self.dim_pixel = float(self.le_pixel_dimension.text())
img_format = str(self.lformat.text())
vel = float(self.le2.text())
start_index = int(self.lesi.text())
levels = int(self.lestopi.text())
image_time_sampling = int(self.lesamp.text())
templateWidth = int(self.le_tem_width.text())
b = int(self.le_b.text())
d = int(self.le_b1.text())
frame_rate = 1.0 / float(self.le_frame_rate.text())
prova = str(self.le_prova.text())
print("Selected parameters:")
print(self.dim_pixel, img_format, vel, start_index, levels, templateWidth, image_time_sampling, b, frame_rate)
self.display.setText(self.display.toPlainText() +
"SELECTED PARAMETERS:" +
"\nTest name = " + prova +
"\nImage Format = " + img_format +
"\nPixel Dimension = " + str(self.dim_pixel) + ' mm'
'\nImposed deformation velocity = ' + str(vel) + ' mm/s' +
'\nCamera acquisition time = ' + str(1.0 / frame_rate) + ' s'
'\nStart index = ' + str(start_index) +
'\nLevels= ' + str(levels) +
'\nImage time sampling=' + str(image_time_sampling) +
'\nTemplate width = ' + str(templateWidth) + ' pixel' +
'\nSearch zone width = ' + str(b) + ' pixel\n\n')
except:
print("Error: input must be numbers")
self.display.setText(self.display.toPlainText() + "Errore: gli input devono essere un numero\n")
return False
try:
log_message = DIC_roby.DIC(prova, img_format, vel, self.dim_pixel, frame_rate, start_index, levels, image_time_sampling, templateWidth, b, d, rectangley1, rectangley2, rectanglex1, rectanglex2)
if self.rdbUno.isChecked():
DIC_roby.gif(os.path.join(path, "GIF"), "gif.GIF")
else:
print('no gif')
self.display.setText(self.display.toPlainText() + "RESULTS\n" + log_message)
plt.ioff()
return True
except:
print("Error during cross correlation computation")
self.display.setText(self.display.toPlainText() + "Error during cross correlation computation\n")
print("Unexpected error:", sys.exc_info()[0])
raise
return False
if name == 'main': app = QApplication(sys.argv) main = First() app.setActiveWindow(main) main.show() sys.exit(app.exec_())
I changed the code according to python3 but I am getting following error now
Hello,
I am using this software for DIC and when I start to draw AOI it crashes and following error appears in command prompt.
File "C:\Users\Shahryar\py2DIC-master\sources\DIC_UI.py", line 96, in paint painter.drawEllipse(self.x-self.radius, self.y-self.radius, 2self.radius, 2self.radius) TypeError: arguments did not match any overloaded call: drawEllipse(self, r: QRectF): argument 1 has unexpected type 'float' drawEllipse(self, r: QRect): argument 1 has unexpected type 'float' drawEllipse(self, x: int, y: int, w: int, h: int): argument 1 has unexpected type 'float' drawEllipse(self, center: Union[QPointF, QPoint], rx: float, ry: float): argument 1 has unexpected type 'float' drawEllipse(self, center: QPoint, rx: int, ry: int): argument 1 has unexpected type 'float'
Please let me know why its happening.