Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Capturing and Editing Video > VapourSynth

Reply
 
Thread Tools Search this Thread Display Modes
Old 13th November 2023, 20:57   #4841  |  Link
cubicibo
Registered User
 
Join Date: Feb 2022
Posts: 96
Anythig numpy can go turbo with Numba njit. And in this case even benefit of parallelism thanks to independant processing of the planes. However, muvsfunc would have to be rewritten extensively to only access Python and numpy primitives in its core muvsfunc_numpy module.

Is the sample you posted incomplete? There are no acccess to cv2 and the import is unused.

Last edited by cubicibo; 13th November 2023 at 21:02.
cubicibo is offline   Reply With Quote
Old 13th November 2023, 21:05   #4842  |  Link
Selur
Registered User
 
Selur's Avatar
 
Join Date: Oct 2001
Location: Germany
Posts: 7,239
You are right, I don't need openCV, I first had it in there for some image handling, but I noticed that just using numpy was faster.
Okay, so the next question is: is numpy really needed?
__________________
Hybrid here in the forum, homepage

Last edited by Selur; 13th November 2023 at 21:08.
Selur is offline   Reply With Quote
Old 13th November 2023, 21:31   #4843  |  Link
cubicibo
Registered User
 
Join Date: Feb 2022
Posts: 96
You can drop Pillow, not numpy.

Code:
def convert_temp(frame: np.ndarray, temp: int) -> np.ndarray:
    return frame*(np.array(kelvin_table[temp], dtype=float)/255.0)
You may additionally want to np.round() before casting back to np.uint8.

Last edited by cubicibo; 13th November 2023 at 21:33.
cubicibo is offline   Reply With Quote
Old 14th November 2023, 00:09   #4844  |  Link
WolframRhodium
Registered User
 
Join Date: Jan 2016
Posts: 162
colortemperature can be implemented using only vanilla std.Expr + std.ShufflePlanes.
WolframRhodium is offline   Reply With Quote
Old 14th November 2023, 03:14   #4845  |  Link
_Al_
Registered User
 
Join Date: May 2011
Posts: 317
Quote:
Originally Posted by cubicibo View Post
You can drop Pillow, not numpy.

Code:
def convert_temp(frame: np.ndarray, temp: int) -> np.ndarray:
    return frame*(np.array(kelvin_table[temp], dtype=float)/255.0)
You may additionally want to np.round() before casting back to np.uint8.
I tried this and it seams to work, more testing maybe needed. Basics are used, just numpy and vapoursynth, no muvsfunc_numpy , no PIL:
Code:
import vapoursynth as vs
from vapoursynth import core
import numpy as np

class Temperature:
    KELVIN_TABLE = {
        1000: (255,56,0),
        1500: (255,109,0),
        2000: (255,137,18),
        2500: (255,161,72),
        3000: (255,180,107),
        3500: (255,196,137),
        4000: (255,209,163),
        4500: (255,219,186),
        5000: (255,228,206),
        5500: (255,236,224),
        6000: (255,243,239),
        6500: (255,249,253),
        7000: (245,243,255),
        7500: (235,238,255),
        8000: (227,233,255),
        8500: (220,229,255),
        9000: (214,225,255),
        9500: (208,222,255),
        10000: (204,219,255)}
        
    def __init__(self, temp):
        self.rgb = self.KELVIN_TABLE[temp]

    def change(self, n, f):
        f_out = f.copy()
        for p in range(3):
            npArray = np.asarray(f[p])
            npArray = npArray*(np.array(self.rgb[p], dtype=float)/255.0)
            np.copyto(np.asarray(f_out[p]), npArray[:,:])
        return f_out

clip = core.lsmas.LibavSMASHSource('video.mp4')
clip = clip.resize.Point(format=vs.RGBS, matrix_in_s = '709')
clip = core.std.ModifyFrame(clip, clip, Temperature(4000).change)
clip = clip.resize.Point(format=vs.YUV420P8, matrix_s = '709')
clip.set_output(0)
_Al_ is offline   Reply With Quote
Old 14th November 2023, 05:29   #4846  |  Link
_Al_
Registered User
 
Join Date: May 2011
Posts: 317
Quote:
Originally Posted by WolframRhodium View Post
colortemperature can be implemented using only vanilla std.Expr + std.ShufflePlanes.
this seams to perform about the same speed as example above:
Code:
import vapoursynth as vs
from vapoursynth import core

KELVIN_TABLE = {
    1000: (255,56,0),
    1500: (255,109,0),
    2000: (255,137,18),
    2500: (255,161,72),
    3000: (255,180,107),
    3500: (255,196,137),
    4000: (255,209,163),
    4500: (255,219,186),
    5000: (255,228,206),
    5500: (255,236,224),
    6000: (255,243,239),
    6500: (255,249,253),
    7000: (245,243,255),
    7500: (235,238,255),
    8000: (227,233,255),
    8500: (220,229,255),
    9000: (214,225,255),
    9500: (208,222,255),
    10000: (204,219,255)}

def change_temperature(clip, temp):
    rgb = KELVIN_TABLE[temp]
    rgb = [value/255.0 for value in rgb]
    planes = core.std.SplitPlanes(clip)
    planes = [core.std.Expr(plane, expr=[f"x {rgb[i]} *"]) for i, plane in enumerate(planes)]
    return core.std.ShufflePlanes(clips=planes, planes=[0], colorfamily=vs.RGB)

clip = core.lsmas.LibavSMASHSource('video.mp4')
clip = clip.resize.Point(format=vs.RGBS, matrix_in_s = '709')
clip = change_temperature(clip, 4000)
clip = clip.resize.Point(format=vs.YUV420P8, matrix_s = '709')
clip.set_output(0)

Last edited by _Al_; 14th November 2023 at 05:31.
_Al_ is offline   Reply With Quote
Old 14th November 2023, 06:06   #4847  |  Link
_Al_
Registered User
 
Join Date: May 2011
Posts: 317
to not use KELVIN_TABLE, and using Selur's code in python to use any temperature, not just selected from a table:
Code:
import vapoursynth as vs
from vapoursynth import core
import math

def get_rgb(temp):
    temp = temp / 100
    if temp <= 66:
        r = 255
    else:
        r = temp - 60
        r = 329.698727466 * math.pow(r, -0.1332047592)
        r = min(max(0, r), 255)

    if temp <= 66:
        g = temp
        g = 99.4708025861 * math.log(g) - 161.1195681661
    else:
        g = temp - 60
        g = 288.1221695283 * math.pow(g, -0.0755148492)
    g = min(max(0, g), 255)

    if temp >= 66:
        b = 255
    else:
        if temp <= 19:
            b = 0
        else:
            b = temp - 10
            b = 138.5177312231 * math.log(b) - 305.0447927307
            b = min(max(0, b), 255)

    return round(r), round(g), round(b)

def change_temperature(clip, temp):
    rgb = get_rgb(temp)
    r, g, b = [value/255.0 for value in rgb]
    return core.std.Expr([clip], expr=[f"x {r} *", f"x {g} *", f"x {b} *"])

clip = core.lsmas.LibavSMASHSource('video.mp4')
clip = clip.resize.Point(format=vs.RGBS, matrix_in_s = '709')
clip = change_temperature(clip, 4345)
clip = clip.resize.Point(format=vs.YUV420P8, matrix_s = '709')
clip.set_output(0)
Expr seams to perform faster than working with numpy arrays.

Last edited by _Al_; 14th November 2023 at 06:39.
_Al_ is offline   Reply With Quote
Old 14th November 2023, 06:46   #4848  |  Link
Selur
Registered User
 
Selur's Avatar
 
Join Date: Oct 2001
Location: Germany
Posts: 7,239
Nice, thanks.
__________________
Hybrid here in the forum, homepage
Selur is offline   Reply With Quote
Old 23rd November 2023, 10:56   #4849  |  Link
rgr
Registered User
 
Join Date: Jun 2022
Posts: 19
Vapoursynth needs a specific version of Python (3.11)? I have 3.13 and it doesn't want to install.
rgr is offline   Reply With Quote
Old 23rd November 2023, 11:06   #4850  |  Link
cubicibo
Registered User
 
Join Date: Feb 2022
Posts: 96
You need 3.11.
Why would you even use Python 3.13? It is in an early alpha and the Python fundation strongly advise to not use it [for production].
cubicibo is offline   Reply With Quote
Old 19th December 2023, 23:00   #4851  |  Link
Selur
Registered User
 
Selur's Avatar
 
Join Date: Oct 2001
Location: Germany
Posts: 7,239
Are there color constants that can be used with AddBorders?
'float[] color=<black>' seems to suggest they are, if there are, is there a list somewhere?
__________________
Hybrid here in the forum, homepage
Selur is offline   Reply With Quote
Old 20th December 2023, 09:16   #4852  |  Link
Myrsloik
Professional Code Monkey
 
Myrsloik's Avatar
 
Join Date: Jun 2003
Location: Kinnarps Chair
Posts: 2,541
Quote:
Originally Posted by Selur View Post
Are there color constants that can be used with AddBorders?
'float[] color=<black>' seems to suggest they are, if there are, is there a list somewhere?
No, it's just a simpler way than trying to express black in all different formats and bitdepths.

I've chosen to directly pass through values in all functions unlike avisynth which usually takes 8 bit RGB values and converts.
__________________
VapourSynth - proving that scripting languages and video processing isn't dead yet
Myrsloik is offline   Reply With Quote
Old 20th December 2023, 09:17   #4853  |  Link
Selur
Registered User
 
Selur's Avatar
 
Join Date: Oct 2001
Location: Germany
Posts: 7,239
Thanks for clearing that up.

Cu Selur
__________________
Hybrid here in the forum, homepage
Selur is offline   Reply With Quote
Reply

Tags
speed, vaporware, vapoursynth

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:49.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.