Certainly! Below is a standalone HTML/CSS/JavaScript page that visualizes the relationship between lens distortion parameters and focal length. The page features a modern GUI with interactive sliders that allow users to adjust parameters and see the effects in real-time.
Live Demo
You can view a live demo of the page here. (Note: Since this is a static example, the live demo link is not functional.)
Code
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Lens Distortion Visualization</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Lens Distortion Visualization</h1>
<div class="controls">
<div class="control-group">
<label for="focalLength">Focal Length:</label>
<input type="range" id="focalLength" min="50" max="150" value="100">
<span id="focalLengthValue">100</span>
</div>
<div class="control-group">
<label for="k1">Distortion Coefficient k1:</label>
<input type="range" id="k1" min="-0.5" max="0.5" step="0.01" value="0">
<span id="k1Value">0.00</span>
</div>
<div class="control-group">
<label for="k2">Distortion Coefficient k2:</label>
<input type="range" id="k2" min="-0.5" max="0.5" step="0.01" value="0">
<span id="k2Value">0.00</span>
</div>
</div>
<canvas id="canvas" width="600" height="600"></canvas>
</div>
<script src="script.js"></script>
</body>
</html>
CSS (styles.css)
body {
margin: 0;
font-family: Arial, sans-serif;
background-color: #f4f4f4;
}
.container {
width: 700px;
margin: 20px auto;
text-align: center;
}
h1 {
margin-bottom: 20px;
}
.controls {
margin-bottom: 20px;
}
.control-group {
margin: 10px 0;
}
.control-group label {
display: inline-block;
width: 200px;
text-align: right;
margin-right: 10px;
}
.control-group input[type="range"] {
width: 300px;
vertical-align: middle;
}
.control-group span {
display: inline-block;
width: 50px;
text-align: left;
margin-left: 10px;
}
#canvas {
border: 1px solid #ccc;
background-color: #fff;
}
JavaScript (script.js)
window.onload = function() {
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const focalLengthSlider = document.getElementById('focalLength');
const focalLengthValue = document.getElementById('focalLengthValue');
const k1Slider = document.getElementById('k1');
const k1Value = document.getElementById('k1Value');
const k2Slider = document.getElementById('k2');
const k2Value = document.getElementById('k2Value');
// Initial parameter values
let focalLength = parseFloat(focalLengthSlider.value);
let k1 = parseFloat(k1Slider.value);
let k2 = parseFloat(k2Slider.value);
// Update displayed values
focalLengthValue.textContent = focalLength;
k1Value.textContent = k1.toFixed(2);
k2Value.textContent = k2.toFixed(2);
// Event listeners for sliders
focalLengthSlider.addEventListener('input', function() {
focalLength = parseFloat(this.value);
focalLengthValue.textContent = focalLength;
drawGrid();
});
k1Slider.addEventListener('input', function() {
k1 = parseFloat(this.value);
k1Value.textContent = k1.toFixed(2);
drawGrid();
});
k2Slider.addEventListener('input', function() {
k2 = parseFloat(this.value);
k2Value.textContent = k2.toFixed(2);
drawGrid();
});
// Function to draw the grid with distortion
function drawGrid() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
const imgWidth = canvas.width;
const imgHeight = canvas.height;
// Parameters for the grid
const numLines = 20;
const spacingX = imgWidth / numLines;
const spacingY = imgHeight / numLines;
const centerX = imgWidth / 2;
const centerY = imgHeight / 2;
ctx.strokeStyle = '#000';
ctx.lineWidth = 1;
// Draw vertical lines
for (let i = 0; i <= numLines; i++) {
const x = i * spacingX;
drawDistortedLine(x, 0, x, imgHeight);
}
// Draw horizontal lines
for (let i = 0; i <= numLines; i++) {
const y = i * spacingY;
drawDistortedLine(0, y, imgWidth, y);
}
}
// Function to draw a line with distortion
function drawDistortedLine(x0, y0, x1, y1) {
ctx.beginPath();
const steps = 100;
for (let i = 0; i <= steps; i++) {
const t = i / steps;
const x = x0 + (x1 - x0) * t;
const y = y0 + (y1 - y0) * t;
// Normalize coordinates to center of canvas and apply focal length
let xn = (x - canvas.width / 2) / focalLength;
let yn = (y - canvas.height / 2) / focalLength;
// Calculate radius squared
const r2 = xn * xn + yn * yn;
// Apply radial distortion
const radialDistortion = 1 + k1 * r2 + k2 * r2 * r2;
const xd = xn * radialDistortion;
const yd = yn * radialDistortion;
// Convert back to pixel coordinates
const xdPixel = xd * focalLength + canvas.width / 2;
const ydPixel = yd * focalLength + canvas.height / 2;
if (i === 0) {
ctx.moveTo(xdPixel, ydPixel);
} else {
ctx.lineTo(xdPixel, ydPixel);
}
}
ctx.stroke();
}
// Initial draw
drawGrid();
};
How It Works
-
HTML Structure: The page contains a container with a title, control sliders for adjusting the focal length and distortion coefficients (k1 and k2), and a canvas element where the grid is drawn.
-
CSS Styling: A clean and modern look is achieved using basic CSS styling, centering the content and styling the controls for better usability.
-
JavaScript Functionality:
- Event Listeners: Attached to each slider to update the corresponding parameter and redraw the grid whenever a slider is moved.
- drawGrid() Function: Clears the canvas and redraws the grid lines with the current parameters.
- drawDistortedLine() Function: Breaks down each grid line into small segments and applies the distortion equations to each point.
- Normalization: Coordinates are normalized relative to the center of the canvas and scaled by the focal length.
- Radial Distortion: Applies the radial distortion formula using coefficients k1 and k2.
- Re-mapping: Distorted coordinates are mapped back to pixel coordinates for drawing.
Usage
-
Open the HTML File: Save the provided code into three files (index.html, styles.css, and script.js) in the same directory.
-
Run Locally: Open index.html in a modern web browser (e.g., Chrome, Firefox, Edge).
-
Adjust Parameters:
- Focal Length: Move the slider to adjust the focal length. This simulates zooming in and out.
- Distortion Coefficients (k1 and k2): Adjust these sliders to see how radial distortion affects the grid. Positive values simulate pincushion distortion, and negative values simulate barrel distortion.
Explanation of Lens Distortion Parameters
-
Focal Length: Determines the magnification of the image. A shorter focal length results in a wider field of view, while a longer focal length narrows the field of view.
-
Radial Distortion Coefficients (k1, k2): Parameters that model radial distortion caused by the lens.
- k1: The primary coefficient affecting distortion. Negative values cause barrel distortion (image bulges outwards), and positive values cause pincushion distortion (image pinches inward).
- k2: Higher-order coefficient for modeling more complex distortion patterns.
Customization
- Extend Distortion Model: You can add more coefficients (e.g., k3) for higher-order distortion effects.
- Change the Grid: Replace the grid with an image by drawing the image onto the canvas and applying the distortion to each pixel (note that this is more computationally intensive).
- Enhance the UI: Incorporate additional UI frameworks or libraries for a more sophisticated interface.
Conclusion
This visualization tool demonstrates the impact of lens distortion parameters and focal length on images. By adjusting the sliders, users can intuitively understand how different values affect the distortion, providing insight into lens behavior in optical systems.
Feel free to ask if you need further assistance or additional features!