GithubHelp home page GithubHelp logo

convert-matlab-matrix-number-to-png-image-with-python's Introduction

(Experiment) Convert matlab's matrix number into png image with python

dataset from http://make3d.cs.cornell.edu/

Import Library

import pathlib
import IPython.display as display
import scipy.io as sio 
import cv2
from matplotlib import pyplot as plt
import numpy as np
%matplotlib inline 
import random
import os
import time, sys
from IPython.display import clear_output

Setting

IN = "dataset/depthMat/"
groudTruth = "dataset/image/"
OUT= "dataset/depth/"
width,height = (1704,2272)

Load ALL

def update_progress(progress):
    bar_length = 20
    if isinstance(progress, int):
        progress = float(progress)
    if not isinstance(progress, float):
        progress = 0
    if progress < 0:
        progress = 0
    if progress >= 1:
        progress = 1
    block = int(round(10 * progress))
    clear_output(wait = True)
    text = "Progress: [{0}] {1:.1f}%".format( "#" * block + "-" * (bar_length - block), progress * 100)
    print(text)

    

root = pathlib.Path(IN)
all_image_paths = list(root.glob('*.mat'))
all_image_paths = [str(path) for path in all_image_paths]
print("Loaded " +str(len(all_image_paths))+" Images.\n "+str(all_image_paths[:3])+"\n...")
Loaded 400 Images.
 ['dataset\\depthMat\\depth_sph_corr-060705-17.10.14-p-018t000.mat', 'dataset\\depthMat\\depth_sph_corr-060705-17.10.14-p-111t000.mat', 'dataset\\depthMat\\depth_sph_corr-060705-17.10.14-p-142t000.mat']
...
def MatDepth2Gray(path):
    mat = sio.loadmat(path, appendmat=True)
    img = mat['Position3DGrid'][:,:,:]
    img = np.int32(img)
    img = cv2.normalize(img,None,alpha=0, beta=255, norm_type=cv2.NORM_MINMAX , dtype=cv2.CV_8UC1  )
    img = cv2.resize(img, (width,height),interpolation = cv2.INTER_AREA)
    return img

Random Sample 4 Images

plt.figure(figsize=(15,15))
col = 1
for n in range(4):
    image_path = random.choice(all_image_paths)
    filename =  os.path.splitext(os.path.basename(image_path))[0].replace("depth_sph_corr","img")
    
    img = MatDepth2Gray(image_path)

    plt.subplot(2,4,col)
    
    gt = cv2.imread(groudTruth+filename+".jpg" )
    gt = cv2.cvtColor(gt, cv2.COLOR_BGR2RGB)

    plt.imshow(gt)
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])
    plt.xlabel("original")
    
    col=col+1
    
    plt.subplot(2,4,col)
    plt.imshow(img,cmap="gray" )
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])
    plt.xlabel("Depth")
    img.shape
    
    col=col+1

png

Batch Convert

for n,image in enumerate(all_image_paths):
    filename = os.path.splitext(os.path.basename(image))[0]
     
    im = MatDepth2Gray(image)
    update_progress(n / len(all_image_paths))
    plt.imsave(OUT+filename+".png", im,format="png")
Progress: [##########----------] 99.8%

convert-matlab-matrix-number-to-png-image-with-python's People

Contributors

telexine avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.