Matlab中RGB图像指定像素移除及seam carving接缝移除方法咨询
Hey there! Let's tackle your two MATLAB image processing questions one by one—they're both about pixel removal, but with slightly different contexts.
First, remember that RGB images in MATLAB are 3D arrays (M×N×3, where M is rows, N is columns, and 3 represents the red/green/blue channels). How you remove pixels depends on whether you're targeting scattered pixels or structured rows/columns:
Case A: Removing Scattered/Single Pixels (Non-Rectangular Result)
If you're removing arbitrary, non-contiguous pixels, you can't keep the image as a rectangle—instead, you'll end up with a list of remaining pixels. Here's how to do it with a mask:
% Assume img is your input RGB image (M×N×3) % Create a mask where true = keep pixel, false = remove pixel mask = true(size(img, 1), size(img, 2)); mask(2, 3) = false; % Remove pixel at row 2, column 3 mask(5, 6) = false; % Remove another pixel at row 5, column 6 % Flatten the image into a 2D matrix (each row = one pixel's RGB values) img_flat = reshape(img, [], 3); % Flatten the mask to match mask_flat = reshape(mask, [], 1); % Keep only the pixels marked as true in the mask remaining_pixels = img_flat(mask_flat, :);
remaining_pixels will be a (M*N - K)×3 matrix (K = number of removed pixels) — it's a collection of pixels, not a rectangular image.
Case B: Removing Entire Rows/Columns (Rectangular Result)
If you're removing full rows or columns, you can directly index the 3D array:
% Remove column j from the image img_removed_col = img(:, [1:j-1, j+1:end], :); % Remove row i from the image img_removed_row = img([1:i-1, i+1:end], :, :);
I feel your pain with the dimension error! The issue is that your RGB image is 3D, so image(i, remove) = [] tries to delete a single 3D pixel element, which breaks the array's consistent shape across rows. You don't have to fully rebuild the image from scratch—here are two clean ways to do it:
Method 1: Linear Indexing (Faster, Recommended for Large Images)
MATLAB lets you access array elements with linear indices, which lets you delete all seam pixels in one go:
% Assume img is your input RGB image (M×N×3) % seam_idx is a length-M vector, where seam_idx(i) = column to remove in row i [M, N, ~] = size(img); % Create coordinates for all seam pixels rows = 1:M; cols = seam_idx; % Calculate linear indices for each seam pixel across all 3 channels remove_indices = sub2ind(size(img), rows, cols, ones(1, M)); % Remove the seam pixels and reshape back into a rectangular image (M×(N-1)×3) img_carved = reshape(img(~remove_indices), M, N-1, 3);
This avoids loops and is much faster for big images.
Method 2: Row-by-Row Construction (More Intuitive)
If linear indexing feels confusing, you can build the carved image row by row—this is easier to follow for smaller images:
[M, N, ~] = size(img); % Preallocate memory for the carved image (same data type as input) img_carved = zeros(M, N-1, 3, class(img)); for i = 1:M % For each row, exclude the seam column and assign to the carved image img_carved(i, :, :) = img(i, [1:seam_idx(i)-1, seam_idx(i)+1:end], :); end
The key here is that we're explicitly assigning each modified row to the preallocated 3D array, which keeps the dimensions consistent.
To recap why your original code failed: image(i, remove) targets a 1×1×3 RGB pixel, and setting it to [] would make that row's width shrink by 1—MATLAB can't automatically adjust the entire 3D array's shape to match, so you need to construct the new array explicitly.
内容的提问来源于stack exchange,提问作者Hiya54 Huy

