如何使用MATLAB查看OV7670输出的YUV422单帧图像数据?
Hey there! Since you've already got a Java program up and running to visualize your OV7670's YUV422 output, let's get MATLAB to do the same for your single-frame data. Here's a step-by-step guide tailored to your setup:
Step 1: Load Your Raw YUV422 Data
First, we need to turn that space-separated numeric data (like "128 74 83 93...") into a usable MATLAB array.
- If your data is saved in a text file:
% Replace 'ov7670_frame.txt' with your actual file path raw_data = readmatrix('ov7670_frame.txt', 'Delimiter', ' '); - If you're pasting the data directly as a string:
raw_data = str2num('128 74 83 93...'); % Paste your full data string here
Step 2: Parse YUV422 into Y, U, V Components
OV7670 typically outputs YUV422 in YUYV format, which follows this repeating pattern:Y0 U0 Y1 V0 Y2 U1 Y3 V1 ...
Every 4 values correspond to 2 Y pixels, 1 U, and 1 V component.
Let's split the raw data into separate channels:
% Reshape data into a column vector for easy indexing yuv_col = reshape(raw_data, [], 1); % Extract individual components Y = yuv_col(1:2:end); % All odd indices: Y0, Y1, Y2, Y3... U = yuv_col(2:4:end); % Indices 2,6,10...: U0, U1, U2... V = yuv_col(4:4:end); % Indices 4,8,12...: V0, V1, V2...
Step 3: Reshape and Upsample to Match Image Resolution
OV7670's common resolutions are 640x480 or 320x240—replace the values below with your sensor's actual output size:
img_width = 640; % Adjust to your sensor's width img_height = 480; % Adjust to your sensor's height % Reshape Y to full image dimensions (transpose fixes MATLAB's column-major order) Y_matrix = reshape(Y, img_width, img_height)'; % Reshape U/V (they're half the width of Y) then upsample to match Y's size U_matrix = reshape(U, img_width/2, img_height)'; U_matrix = imresize(U_matrix, 2, 'nearest'); % Upsample 2x to match Y resolution V_matrix = reshape(V, img_width/2, img_height)'; V_matrix = imresize(V_matrix, 2, 'nearest');
Step 4: Convert YUV to RGB and Display
MATLAB's image tools work best with RGB, so let's convert and show the frame:
% Combine Y/U/V into a 3-channel matrix (cast to uint8 since sensor outputs 0-255 values) YUV_matrix = cat(3, uint8(Y_matrix), uint8(U_matrix), uint8(V_matrix)); % Convert YUV to RGB RGB_matrix = yuv2rgb(YUV_matrix); % Display the final image imshow(RGB_matrix); title('OV7670 YUV422 Frame (Converted to RGB)');
Quick Troubleshooting Tips
- Tinted/wrong colors? Your sensor might be using UYVY format instead of YUYV. Adjust component extraction like this:
U = yuv_col(1:4:end); Y_part1 = yuv_col(2:4:end); Y_part2 = yuv_col(4:4:end); Y = [Y_part1; Y_part2]; % Merge two Y segments into one full vector V = yuv_col(3:4:end); - Distorted image? Double-check that
img_widthandimg_heightmatch your sensor's output—incorrect resolution will mess up the image layout during reshaping.
内容的提问来源于stack exchange,提问作者Jiji Gaming

