Hiding a Colored Image Inside Another Colored Image

36 次查看(过去 30 天)
The following code is for hiding a colored image inside another colored image using a chaotic system, but when executing the program, the image is not completely hidden. Why? What is the error in the code?
% Load the cover image and the hidden image
coverImage = imread('cover_image.jpg');
hiddenImage = imread('hidden_image.jpg');
% Get the dimensions of the images
[r, c, d] = size(coverImage);
[hr, hc, hd] = size(hiddenImage);
% Check if the number of channels matches
if d ~= hd
error('The number of channels in the hidden image must match the number of channels in the cover image.');
end
% Check if the dimensions match
if hr ~= r || hc ~= c
error('The dimensions of the hidden image must match the dimensions of the cover image.');
end
% Initialize parameters for the chaotic system
x0 = 0.1; y0 = 0.5; z0 = 3.5; w0 = 0.6; v0 = 0.4; u0 = 0.1;
n = r * c; % Total number of pixels
% Define chaotic system parameters
a = 10; b = 28; c = 8/3;
dx = 1e-4; % Step size
num_values = 10 * n; % Number of values to generate
% Initialize the chaotic variables
x = x0 * ones(1, num_values + 10000);
y = y0 * ones(1, num_values + 10000);
z = z0 * ones(1, num_values + 10000);
w = w0 * ones(1, num_values + 10000);
v = v0 * ones(1, num_values + 10000);
u = u0 * ones(1, num_values + 10000);
% Generate chaotic values using numerical integration
for ii = 2:num_values + 10001
x(ii) = x(ii-1) + dx * (a * (y(ii-1) - x(ii-1)));
y(ii) = y(ii-1) + dx * (x(ii-1) * (b - z(ii-1)) - y(ii-1));
z(ii) = z(ii-1) + dx * (x(ii-1) * y(ii-1) - c * z(ii-1));
w(ii) = w(ii-1) + dx * (v(ii-1) - w(ii-1));
v(ii) = v(ii-1) + dx * (w(ii-1) * u(ii-1) - v(ii-1));
u(ii) = u(ii-1) + dx * (w(ii-1) * v(ii-1) - c * u(ii-1));
end
% Discard the first 10000 values to avoid transient behavior
x = x(10002:end);
y = y(10002:end);
z = z(10002:end);
w = w(10002:end);
v = v(10002:end);
u = u(10002:end);
% Map chaotic values to pixel positions
x_int = mod(floor(abs(x * 1e15)), n);
y_int = mod(floor(abs(y * 1e15)), n);
% Ensure unique pixel positions
x_int_unique = unique(x_int);
y_int_unique = unique(y_int);
% Attempt to ensure enough unique positions
attempts = 0;
max_attempts = 10; % Maximum attempts to find enough unique positions
while numel(x_int_unique) < n || numel(y_int_unique) < n
attempts = attempts + 1;
if attempts > max_attempts
error('Failed to generate enough unique positions for embedding the hidden image.');
end
num_values = num_values * 2; % Double the number of values
% Reinitialize the chaotic variables
x = x0 * ones(1, num_values + 10000);
y = y0 * ones(1, num_values + 10000);
z = z0 * ones(1, num_values + 10000);
w = w0 * ones(1, num_values + 10000);
v = v0 * ones(1, num_values + 10000);
u = u0 * ones(1, num_values + 10000);
% Regenerate chaotic values
for ii = 2:num_values + 10001
x(ii) = x(ii-1) + dx * (a * (y(ii-1) - x(ii-1)));
y(ii) = y(ii-1) + dx * (x(ii-1) * (b - z(ii-1)) - y(ii-1));
z(ii) = z(ii-1) + dx * (x(ii-1) * y(ii-1) - c * z(ii-1));
w(ii) = w(ii-1) + dx * (v(ii-1) - w(ii-1));
v(ii) = v(ii-1) + dx * (w(ii-1) * u(ii-1) - v(ii-1));
u(ii) = u(ii-1) + dx * (w(ii-1) * v(ii-1) - c * u(ii-1));
end
x = x(10002:end);
y = y(10002:end);
z = z(10002:end);
w = w(10002:end);
v = v(10002:end);
u = u(10002:end);
x_int = mod(floor(abs(x * 1e15)), n);
y_int = mod(floor(abs(y * 1e15)), n);
x_int_unique = unique(x_int);
y_int_unique = unique(y_int);
end
% Generate the keys for embedding
key1 = unique(x_int + 1, 'stable'); % Unique positions for embedding
key2 = unique(y_int + 1, 'stable');
key1 = key1(1:n);
key2 = key2(1:n);
% Embed the hidden image into the cover image
stegoImage = coverImage; % Initialize the stego image
coverImage = uint8(coverImage);
hiddenImage = uint8(hiddenImage);
for i = 1:n
pos = key1(i);
row = mod(pos - 1, r) + 1; % Calculate row from position
col = floor((pos - 1) / r) + 1; % Calculate column from position
for k = 1:d
% Perform XOR to embed the hidden image
stegoImage(row, col, k) = bitxor(coverImage(row, col, k), hiddenImage(row, col, k));
end
end
% Save the stego image
imwrite(stegoImage, 'stego_image.jpg');
figure;
imshow(stegoImage);
title('Stego Image');
% Extract the hidden image from the stego image
extractedImage = uint8(zeros(size(hiddenImage))); % Initialize the extracted image
for i = 1:n
pos = key1(i);
row = mod(pos - 1, r) + 1; % Calculate row from position
col = floor((pos - 1) / r) + 1; % Calculate column from position
for k = 1:d
% Perform XOR to extract the hidden image
extractedImage(row, col, k) = bitxor(stegoImage(row, col, k), coverImage(row, col, k));
end
end
% Convert extracted image to uint8
extractedImage = uint8(extractedImage);
% Check if the extracted image is valid
if all(extractedImage(:) == 0)
error('The extracted image is empty.');
end
% Save the extracted image
imwrite(extractedImage, 'extracted_image.jpg');
figure;
imshow(extractedImage);
title('Extracted Image');

回答(1 个)

Image Analyst
Image Analyst 2024-10-26,16:16
Hard to say without being able to run it and debug it. If you have any more questions, then attach your images with the paperclip icon after you read this:

类别

Help CenterFile Exchange 中查找有关 Get Started with Image Processing Toolbox 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by