<jupyter_start><jupyter_text>Rebuilding Scenes with Computer Vision  Installing and Loading Packages<jupyter_code># Python Version
from platform import python_version
print('The Python version is:', python_version())
# To update a package, execute the following command in the terminal or command prompt:
# pip install -U pack_name

# To install the exact version of a package, execute the following command in the terminal or command prompt:
# pip install pack_name==desired_version

# After installing or updating the package, restart the jupyter notebook.
# Watermark package 
# !pip install -q -U 

# Install OpenCV
%pip install -q opencv-python==4.5.1.48

# Install Plotly (for browser visualise)
%pip install -q plotly
import plotly as plt

# Install chart-studio to generate 3D graphs
%pip install -q chart-studio
import chart_studio

### Imports
%pylab notebook
import cv2

import numpy as np

# Packages Versions:
%reload_ext watermark
%watermark -a "<NAME>" --iversions
import copy
from mpl_toolkits.axes_grid1 import make_axes_locatable<jupyter_output>Author: <NAME>

chart_studio: 1.1.0
re          : 2.2.1
datetime    : 4.3
logging     : 0.5.1.2
plotly      : 5.3.0
matplotlib  : 3.5.1
numpy       : 1.19.5
sys         : 3.8.12 (default, Oct 12 2021, 03:01:40) [MSC v.1916 64 bit (AMD64)]
cv2         : 4.5.1<jupyter_text>Image Padding Function to add fill to the image in order to render the image boundary pixelArguments:     - img: image to be padded- size: tamanho do kernel kernel size- return: padded image<jupyter_code># Function
def funcPaddingImage(img, size):
    
    # Define K size
    k = int((size-1)/2)
    
    # Pad execution
    paddedImage = np.pad(img, ((k,),(k,)), 'constant')   
   
    return(paddedImage)<jupyter_output><empty_output><jupyter_text>Matrix Normalization Functtion to calculate the mean of the region, subtract the mean of each pixel in the region and return the model of mean zero.Arguments:- img: image location to be processed- i, j: the current pixel location - k: kernel size- return: zero average sub-region<jupyter_code># Function
def Matrix_norm(img, i , j, k):
    
    # Lines and Columns coordinates
    row = list(range(i-k, i+k+1))
    col = list(range(j-k, j+k+1))
    
    # Calculate the average of each extrated submatrix and subtract the matrix
    subMatrix = img[np.ix_(row, col)]
    m = np.mean(subMatrix)
    normSubMatrix = subMatrix - m
    
    return(normSubMatrix)<jupyter_output><empty_output><jupyter_text>Disparity Calculation Function to calculate the disparity between the two images.Arguments:- img1: left image- img2: right image- tamanho: kernel size- return: disparity with a 2d matrix<jupyter_code># Função
def Disparity_calc(img1, img2, size): 

    # Altura e largura da imagem da esquerda
    h,w = img1.shape 
    
    disp = 0

    # Inicializa a matriz de disparidade com zeros
    disparity = np.zeros((h,w))
        
    k = int( (size-1)/2 )
    
    # Loop para atravessar a altura da imagem da esquerda
    for i in range(k, h-k):
        
        # Loop para atravessar a largura da imagem da esquerda
        for j in range(k, w-k):
            
            maxNCC = -2
            
            # Chamada à rotina para extrair submatriz e produzir modelo de média zero
            temp1 = Matrix_norm(img1, i, j, k )
            
            # Para atravessar a linha de varredura correspondente da imagem direita
            for u in range(j-100, j):
                if u < 0:
                    temp = k
                else:
                    temp = u
                
                # Chamada à rotina para extrair submatriz e produzir modelo de média zero para a imagem da direita
                temp2 = Matrix_norm(img2, i , temp, k)
                
                # Correlação cruzada
                num = np.multiply(temp1,temp2)
                num = sum(num)
                den = (np.linalg.norm(temp1)) * (np.linalg.norm(temp2))
                ncc = float(num/den)
                
                # Compara os máximos e armazena o valor máximo
                if(ncc > maxNCC):
                    maxNCC = ncc
                    
                    # Calcula a disparidade como a diferença da localização do pixel em ambas as imagens
                    disp = temp - j
            
            disparity[i][j] = disp

    return disparity<jupyter_output><empty_output><jupyter_text>Process Execution<jupyter_code># Images
imgLeft = cv2.imread("dados/img0.png" , 0)
imgRight = cv2.imread("dados/img1.png" , 0)
imgLeft.shape
imgRight.shape
# Resize
imgL = cv2.resize(imgLeft, (494, 330))   
imgR = cv2.resize(imgRight, (494, 330))
# Window size
size = 11
# Padding
img1 = funcPaddingImage(imgL, size)
img2 = funcPaddingImage(imgR, size)
# Disparity
disparity = Disparity_calc(img1, img2, size)<jupyter_output><empty_output><jupyter_text>Depth Map Normalization<jupyter_code># Calculate the depth of the disparity

focal_length = 3979.911
baseline = 193.001
doffs = 124.343

disparity = np.abs(disparity)
disparity += 1

value  = focal_length * baseline
disparity = disparity + doffs

depth = np.divide(value, disparity)
depth
# Normalizes the depth map 

depthMax = np.max(depth)
depthMin = np.min(depth)
num = 255
den = depthMax - depthMin

normalize_depth = copy.deepcopy(depth)
normalize_depth -= depthMin
normalize_depth *= 255
normalize_depth /= (depthMax - depthMin)
# Plot of the depth map

fig1, ax1 = plt.subplots()
divider = make_axes_locatable(ax1)
cax = divider.append_axes('right', size = '5%', pad = 0.05)

im1 = ax1.imshow(depth, cmap = 'Accent')

fig1.colorbar(im1, cax=cax, orientation = 'vertical')
plt.show()
# Converts the output to a vector
z = depth.flatten()<jupyter_output><empty_output><jupyter_text>Rebuilding Scene and 3D Graph Creation with [Chart-Studio](https://chart-studio.plotly.com/Auth/login)<jupyter_code># Personal Plotpy Access 
chart_studio.tools.set_credentials_file(username = 'seu_usuario', api_key = 'sua_key')

# Depth
inpImg = depth

# Diference
diff = np.amax(depth) - np.amin(depth)

# Nova matriz New Matrix
new = np.ones(depth.shape)
new = new * diff

# Final Plot 
plotImg = np.subtract(depth, new)
plotImg = (plotImg / diff) * 255
finalPlot = np.ones(depth.shape, dtype = np.uint8)
finalPlot = plotImg
# Plot 3D
import chart_studio.plotly as py
import plotly.graph_objs as go
import plotly.io as pio

# Surface Generator
data = [go.Surface(z = finalPlot)]

# Layout
layout = go.Layout(title = 'Mapa Interativo de Profundidade',
                   width = 700,
                   height = 700,
                   margin = dict(l = 65, r = 50, b = 65, t = 90))

# Figure
fig = go.Figure(data = data, layout = layout)

# Recording the Result
pio.write_html(fig, file = 'index.html', auto_open = True)

print(plotImg)<jupyter_output>[[319.62465 319.62465 319.62465 ... 319.62465 319.62465 319.62465]
 [319.62465 319.62465 319.62465 ... 319.62465 319.62465 319.62465]
 [319.62465 319.62465 319.62465 ... 319.62465 319.62465 319.62465]
 ...
 [319.62465 319.62465 319.62465 ... 319.62465 319.62465 319.62465]
 [319.62465 319.62465 319.62465 ... 319.62465 319.62465 319.62465]
 [319.62465 319.62465 319.62465 ... 319.62465 319.62465 319.62465]]
{'fraction_non_alphanumeric': 0.09841973939561963, 'fraction_numerical': 0.06307180482395343, 'mean_word_length': 2.874865735767991, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '551242', 'license': 'MIT', 'n_tokens_mistral': 2779, 'n_tokens_neox': 2409, 'n_words': 725, 'path': 'Computer_Vision/Scene_Recog.ipynb', 'repo_name': 'JoaoSalero-AI/Deep-Learning-Study-Repository', 'size': '7224'}