Home Web Front-end JS Tutorial Implement bilinear interpolation and bicubic interpolation in js

Implement bilinear interpolation and bicubic interpolation in js

Jan 25, 2021 am 09:39 AM
javascript

Implement bilinear interpolation and bicubic interpolation in js

Free learning recommendation: js video tutorial

  • Introduction
  • Bilinear interpolation
    • Principle
  • Bicubic interpolation method
    • Principle
  • js implementation

Introduction

When using canvas to draw on a web page, I encountered a problem, original The data resolution is very small, and the image needs to be enlarged to the entire web page, so the data needs to be enlarged by interpolation. I learned bilinear interpolation and cubic interpolation. The two methods have different effects. They are both implemented with js code. Let me share with you

bilinear interpolation

Principle

Bilinear interpolation performs linear interpolation on the data in each of the x and y directions.
The matrix of the original data, that is, a two-dimensional array, the size is a*b, the target matrix size is m*n, m, n can be larger (enlarged) or smaller (reduced) than a, b, of course the ratio It can also be different, depending on how big your interpolated data needs to be.
The basic idea is to traverse the coordinates of the target matrix, such as the x*y point, find the corresponding position of this point in the original matrix, which is called a mapping point, and then find the four surrounding mapping point P in the original matrix. point, and then perform linear interpolation twice based on the distance from the mapping point P to the coordinates in the x and y directions of the four points to obtain the value of the mapping point.
Implement bilinear interpolation and bicubic interpolation in js
As shown in the figure above, point p is the position where the x*y point in the target matrix is ​​mapped in the original matrix. The nearest four points around it are Q12, Q11, Q21, and Q22. Now x Linear interpolation is performed in the y direction to obtain the values ​​of the two points R1 and R2, and then linear interpolation is performed in the y direction to obtain the value of point P.
Note: After using bilinear interpolation to amplify the data, if the magnification is too large, there will be obvious mosaic phenomenon after generating the image.
For the implementation code, please refer to the following js code

Bicubic interpolation Method

Principle

Bicubic interpolation is also called cubic convolution interpolation. Cubic convolution interpolation is a more complex interpolation method. This algorithm uses the gray value of 16 points around the point to be sampled for cubic interpolation, taking into account not only the gray influence of the four directly adjacent points, but also the influence of the change rate of the gray value between adjacent points. For the specific principle, please refer to the following blog:
Refer to the blog here
The basic principle is to first find the mapping point P of the target matrix midpoint in the source data matrix, then find the 16 points around the P point, and then according to P The distance between the point coordinates and the 16 points in the x and y directions is used to calculate the weight of each point using the BiCubic function. Finally, after multiplying each point by the weight, the value of P can be obtained by adding up.
Implement bilinear interpolation and bicubic interpolation in js

BiCubic function:
Implement bilinear interpolation and bicubic interpolation in js
Among them, when a is -0.5, the BiCubic function has the following shape:
Implement bilinear interpolation and bicubic interpolation in js
Take a= At -0.5, the enlarged data is quite good, the generated image is very smooth and retains a lot of details.
I haven’t studied in depth why this function should be used, but after using this method to amplify the data, the generated picture effect is very good, without mosaic phenomenon

js implementation

/**
 * 数据处理工具类(也可以自己直接定义方法,不用class)
 */class DataUtil {
	constructor() {}}/**
 * 数据插值
 * @param w 目标矩阵宽度
 * @param h 目标矩阵高度
 * @param data 源数据矩阵(二维数组)
 * @param type 插值方式,1:双线性插值,2:双三次插值法
 */DataUtil.scaleData = function(w, h, data, type = 2) {
	let t1 = new Date().getTime();
	let dw = data[0].length;
	let dh = data.length;
	
	let resData = new Array(h);
	
	for (let j = 0; j  h - 1 ? h - 1 : py;
		for (let i = 0; i  w - 1 ? w - 1 : px;
			// 该点的值
			let dv = data[py][px];
			// 该点的权重
			let w_x = wx[i];
			let w_y = wy[j];
			// 根据加权加起来
			val += (dv * w_x * w_y);
		}
	}
	
	return val;}/**
 * 双三次插值法中,基于BiCubic基函数,计算源坐标v,最近的4*4的坐标和坐标对应的权重
 * @param v 目标矩阵中坐标对应在源矩阵中坐标值
 */DataUtil.getCubicWeight = function (v){
	let a = -0.5;
	
	// 取整
	let nv = Math.floor(v);
	
	// 坐标差值集合
	let xList = new Array(4);
	// 坐标集合
	let xs = new Array(4);
	
	// 最近的4个坐标差值
	xList[0] = nv - v - 1;
	xList[1] = nv - v
	xList[2] = nv - v + 1;
	xList[3] = nv - v + 2;
	// 
	xs[0] = nv - 1;
	xs[1] = nv;
	xs[2] = nv + 1;
	xs[3] = nv + 2;
	
	// 计算权重
	let ws = new Array(4);
	for (let i = 0; i <p><strong>Related free learning recommendations: </strong><a href="https://www.php.cn/course/list/17.html" target="_blank" textvalue="javascript"><strong>javascript</strong></a><strong>(Video)</strong></p>
Copy after login

The above is the detailed content of Implement bilinear interpolation and bicubic interpolation in js. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

See all articles