MATLAB如何保存当前视角与光照下的曲面着色对应矩阵?
Got it, let's break down how to capture the shaded intensity values matching your surf plot's lighting and view, then convert it into a 161×161 matrix ready for photometric stereo work. This works for MATLAB, which I assume you're using given the peaks, surf, and light functions mentioned.
Step 1: Generate Your 161×161 Surface Data
First, create the high-resolution Z matrix using peaks (we'll scale it to 161×161 directly):
% Create 161x161 surface grid [X, Y, Z] = peaks(161);
Step 2: Set Up Plot with Custom Lighting and View
Replicate your existing plot setup, making sure to use smooth shading for accurate intensity capture:
figure; surf(X, Y, Z); shading interp; % Critical for smooth, continuous intensity values % Add your specific lighting conditions (adjust parameters to match your setup) light('Position', [5 5 10], 'Color', [1 1 1], 'Style', 'infinite'); % Example directional light lighting phong; % Use the same lighting mode you originally used (phong/gouraud/flat) view(2); % Set top-down 2D view axis tight; % Ensure the plot fills the axis area to avoid empty borders axis off; % Optional: hide axes to prevent artifacts in the captured image
Step 3: Capture and Map Rendered Intensities to 161×161 Matrix
The key is to capture the rendered image and map it back to your original Z matrix dimensions. Here are two methods, with the second being more precise:
Method 1: Quick Resize (Simple but Approximate)
If your plot is tightly aligned to the data, you can directly resize the captured grayscale image to match Z's size:
% Capture the axis content frame = getframe(gca); rendered_img = frame.cdata; % Convert to grayscale (photometric stereo uses single-channel intensity) gray_intensity = rgb2gray(rendered_img); % Resize to match 161x161 Z matrix shaded_intensity_matrix = imresize(gray_intensity, size(Z));
Method 2: Precise Interpolation (Avoids Resizing Artifacts)
For better accuracy, map the image pixels to your original X/Y grid using interpolation:
% Capture the axis content frame = getframe(gca); rendered_img = frame.cdata; gray_intensity = rgb2gray(rendered_img); % Get axis limits to map image coordinates to data coordinates [x_min, x_max] = xlim(gca); [y_min, y_max] = ylim(gca); % Create image coordinate grid [img_cols, img_rows] = meshgrid(1:size(rendered_img, 2), 1:size(rendered_img, 1)); % Normalize image coordinates to match your surface's X/Y range norm_x = (img_cols - 1)/(size(rendered_img, 2)-1)*(x_max - x_min) + x_min; norm_y = (img_rows - 1)/(size(rendered_img, 1)-1)*(y_max - y_min) + y_min; % Interpolate the intensity values to your original 161x161 grid shaded_intensity_matrix = interp2(norm_x, norm_y, gray_intensity, X, Y);
Step 4: Save the Matrix
Store the final intensity matrix for your photometric stereo work:
% Save as MAT file (preserves matrix dimensions and precision) save('photometric_stereo_intensity.mat', 'shaded_intensity_matrix');
Key Notes
- Shading Mode: Always use
shading interpif you want continuous intensity values—flatorfacetedwill give blocky results that aren't useful for photometric stereo. - Lighting Consistency: Make sure the
lightparameters (position, color, style) exactly match what you used in your original plot. This ensures the captured intensity matches your intended lighting setup. - View Alignment:
view(2)gives a true top-down orthographic view, which is essential for photometric stereo (avoid perspective distortion). Pair it withaxis tightto ensure no empty space in the captured image.
内容的提问来源于stack exchange,提问作者SabreSword

