scientific_comp_projects/CODE/[python]thesis_old_scripts/functions_utile.py
2021-10-29 15:16:40 +02:00

142 lines
5.0 KiB
Python

# -*- coding: utf-8 -*-
"""
Created on Fri Mar 30 11:20:36 2018
@author: Armando
"""
"""
Defined functions (go to the function to learn more about it)
1.- image_correlation(img_left, img_right)
2.- pick_corner(action, x, y, flags,img)
3.- mouse_handler(action, x, y, flags,img)
4.- draw_rectangle(img)
5.- test_homography(h, status, img1, img2)
6.- mouse_handler2(action, x, y, flags,img)
7.- magnification(img)
"""
import cv2
import numpy as np
from skimage import exposure
#data['img'] = src_img_grey.copy()
''''This function takes in two images and gives as a result the (h-matrix for homography, status).
The procedure is to take 4 points in each image and obtain the h-matrix to be applied to future images
It needs the function pick_corner()'''
def image_correlation(img_left, img_right):
global point
point = []
cv2.namedWindow("Image",cv2.WINDOW_NORMAL) #Create the window to show image
imgcopy1 = img_left.copy()
cv2.setMouseCallback("Image", pick_corner, imgcopy1)
cv2.imshow("Image", imgcopy1)
cv2.waitKey(0)
cv2.destroyAllWindows()
pts_dst = np.asarray(point)
point = []
cv2.namedWindow("Image",cv2.WINDOW_NORMAL) #Create the window to show image
imgcopy2 = img_right.copy()
cv2.setMouseCallback("Image", pick_corner, imgcopy2)
cv2.imshow("Image", imgcopy2)
cv2.waitKey(0)
cv2.destroyAllWindows()
pts_src = np.asarray(point)
h, status = cv2.findHomography(pts_src,pts_dst)
return h, status
''' This function is needed to use image_correlation. It takes as imput a image and the actions from the mouse
It gives back the position of pixels as a list.'''
def pick_corner(action, x, y, flags,img):
if action==cv2.EVENT_LBUTTONDOWN:
if len(point) < 4 :
point.append([x,y])
cv2.circle(img, (x,y),2, (255,255,255), 5, 16)
cv2.imshow("Image", img)
elif len(point) >= 4:
cv2.putText(img, "Click any key to exit", (int(img.shape[1]/2), int(img.shape[0]/2)), cv2.FONT_HERSHEY_SIMPLEX, 3, (0, 0, 0), 3)
cv2.imshow("Image", img)
'''This functions is needed for draw_rectangle(). It takes as input a image and the actions from the mouse.
It gives back the corner of the rectangle made by the click and un-click of the mouse.
'''
def mouse_handler(action, x, y, flags,img) :
# Action to be taken when left mouse button is pressed
if action==cv2.EVENT_LBUTTONDOWN:
rectangle.append([x,y])
# Action to be taken when left mouse button is released
elif action==cv2.EVENT_LBUTTONUP:
rectangle.append([x,y])
'''This function takes as input a image and crops out the rectangle chosen for the image.
It needs the function mouse_handler() to be used
'''
def draw_rectangle(img):
global rectangle
rectangle = []
cv2.namedWindow("Image",cv2.WINDOW_NORMAL) #Create the window to show image
cv2.setMouseCallback("Image", mouse_handler, img)
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = img[rectangle[0][1]:rectangle[1][1],rectangle[0][0]:rectangle[1][0]]
return img, rectangle
'''
'''
def test_homography(h, status, img1, img2):
img2_test = cv2.warpPerspective(img2, h, (img1.shape[1],img1.shape[0]))
img1_test = img1.copy()
img2_test = cv2.cvtColor(img2_test, cv2.COLOR_GRAY2BGR)
img1_test = cv2.cvtColor(img1_test, cv2.COLOR_GRAY2BGR)
img2_test[0:len(img2_test),0:len(img2_test[0]),2] = 35535
img1_test[0:len(img1_test),0:len(img1_test[0]),0] = 65535
img_final = img2_test + img1_test
img_final = exposure.rescale_intensity(img_final)
return img_final
def mouse_handler2(action, x, y, flags,img) :
# Action to be taken when left mouse button is pressed
if action==cv2.EVENT_LBUTTONDOWN:
coordinates.append([x,y])
# Action to be taken when left mouse button is released
elif action==cv2.EVENT_LBUTTONUP:
coordinates.append([x,y])
def magnification(img):
global coordinates
coordinates = []
imgcopy = img.copy()
cv2.namedWindow("Image",cv2.WINDOW_NORMAL) #Create the window to show image
cv2.setMouseCallback("Image", mouse_handler2, imgcopy)
print(coordinates)
cv2.putText(imgcopy, "Click/unclick", (int(imgcopy.shape[1]/4), int(imgcopy.shape[0]/2)), cv2.FONT_HERSHEY_SIMPLEX, 3, (0, 0, 0), 3)
cv2.imshow("Image", imgcopy)
cv2.waitKey(0)
cv2.destroyAllWindows()
magnification = 10/np.sqrt( np.power((coordinates[0][1]-coordinates[1][1]),2)+ np.power((coordinates[0][0]-coordinates[1][0]),2))
return magnification