BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Image Scaling - 2020

ffmpeg_new_logo_161_42.png




Bookmark and Share





bogotobogo.com site search:






libswscale

FFMpeg is using the libswscale library to resize the input. The libswscale library contains video image scaling and colorspace/pixelformat conversion routines.





Base Image

Here is our base image (Einstein_500_459.jpg):


Einstein_500_459.jpg

When we scale an image without specifying the size, the input dimension is used as the default value.





Resizing an Image

If we want to resize our image to a specific size (e.g 250x150), we can use the scale filter in its most basic form where v: verbose and f: format in "-vf":

ffmpeg -i Einstein_500_459.jpg -vf scale=250:150 Einstein_250_150.png
Einstein_250_150.png

There are couple of ways to set the scale:

1. scale=w=250:h=150
	
2. scale=250:150

3. scale=250x150





Keep width or heigh of an Image

If we want to keep the aspect ratio, we need to specify either width or height, and set the one not specified to -1:

ffmpeg -i Einstein_500_459.jpg -vf scale=250:-1 Einstein_250.png
Einstein_250.png

We set the width of the output image to 250 pixels. The ffmpeg will calculate the height of the output image from the aspect ratio of the input image so that output image can have a dimension of 250x230 pixels which has the same aspect ratio of the input.





Stretching an in one direction

If we want to stretch the image in such a way to only double the width:

ffmpeg -i Einstein_500_459.jpg -vf scale=iw*2:ih Einstein_double_width.png

In the above command, iw is input width and ih is input height.

Einstein_double_width.png




Fitting an image to a placeholder

Sometimes we need to scale the input image in such way it fits into a specified width x height while keeping the aspect reatio. Suppose we have a placeholder in which we want to scale any given image. This is a little bit tricky, since we need to check the original aspect ratio, in order to decide which component to specify and to set the other component to -1 (to keep the aspect ratio). For example, if we would like to scale our input image into a rectangle with dimensions of 320x240, we could use something like this:

ffmpeg -i Einstein_500_459.jpg -vf scale="'if(gt(a,320/240),320,-1)':'if(gt(a,320/240),-1,240)'" Einstein_320x240_fit.png
Einstein_320x240_fitted.png

Note that we have additional space left on the right side due to the original image not having the same aspect ratio as the placeholder, in which the image was supposed to fit.





Greek harmony
ffmpeg -i Einstein_500_459.jpg -vf scale=iw:1/PHI*iw Einstein_greek_harmony.png
Einstein_greek_harmony.png



References
  1. FFmpeg Filters Documentation.
  2. wiki: Image scaling.








Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







FFmpeg image & video processing



Image/video scaling

Image/video cropping

Cropdetect and ffplay

Speeding-up & slowing-down video

Basic slide show from images

Advanced slide show from images

Thumbnails -Selecting specific frames : I-frame extraction etc.

Creating a mosaic/tile of screenshots from a movie

Seeking and cutting sections of a video & audio

Concatenating two video files or two audio files

Transitions : fade-in & fade-out for 1 slide

Transitions : python script for fade-in & fade-out with two slides

Concatenate slides

Creating test videos

Screen Recording on Ubuntu A

Active window capture with Python on Ubuntu B




Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







OpenCV 3 -

image & video processing



Installing on Ubuntu 13

Mat(rix) object (Image Container)

Creating Mat objects

The core : Image - load, convert, and save

Smoothing Filters A - Average, Gaussian

Smoothing Filters B - Median, Bilateral






OpenCV 3 image and video processing with Python



OpenCV 3 with Python

Image - OpenCV BGR : Matplotlib RGB

Basic image operations - pixel access

iPython - Signal Processing with NumPy

Signal Processing with NumPy I - FFT and DFT for sine, square waves, unitpulse, and random signal

Signal Processing with NumPy II - Image Fourier Transform : FFT & DFT

Inverse Fourier Transform of an Image with low pass filter: cv2.idft()

Image Histogram

Video Capture and Switching colorspaces - RGB / HSV

Adaptive Thresholding - Otsu's clustering-based image thresholding

Edge Detection - Sobel and Laplacian Kernels

Canny Edge Detection

Hough Transform - Circles

Watershed Algorithm : Marker-based Segmentation I

Watershed Algorithm : Marker-based Segmentation II

Image noise reduction : Non-local Means denoising algorithm

Image object detection : Face detection using Haar Cascade Classifiers

Image segmentation - Foreground extraction Grabcut algorithm based on graph cuts

Image Reconstruction - Inpainting (Interpolation) - Fast Marching Methods

Video : Mean shift object tracking

Machine Learning : Clustering - K-Means clustering I

Machine Learning : Clustering - K-Means clustering II

Machine Learning : Classification - k-nearest neighbors (k-NN) algorithm



Matlab Image and Video Processing



Vectors and Matrices

m-Files (Scripts)

For loop

Indexing and masking

Vectors and arrays with audio files

Manipulating Audio I

Manipulating Audio II

Introduction to FFT & DFT

Discrete Fourier Transform (DFT)



Digital Image Processing 2 - RGB image & indexed image

Digital Image Processing 3 - Grayscale image I

Digital Image Processing 4 - Grayscale image II (image data type and bit-plane)

Digital Image Processing 5 - Histogram equalization

Digital Image Processing 6 - Image Filter (Low pass filters)

Video Processing 1 - Object detection (tagging cars) by thresholding color

Video Processing 2 - Face Detection and CAMShift Tracking












Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master