Making an image from extracted bit planes:

2 vues (au cours des 30 derniers jours)
Book
Book le 2 Nov 2012
I'm sorry for repeat. But I put 'accepted answer' in last thread by mistake and I've not final answer yet, so... Here it is again. Sorry for inconvenience.
Hi, I'm new here. And beginner for MATLAB.
I've written a code for watermarking in spatial domain. I've applied the technique of replacing LSB of original image with watermark. Now, my question is, how to save these planes in a single image? It gives me error 'Matrix dimensions must match'... Kindly help...
A=imread('original.png'); C=imread('watermark.bmp');
B1 = bitget(A,1)*2^0; B2 = bitget(A,2)*2^1 ; B3 = bitget(A,3)*2^2 ; B4 = bitget(A,4)*2^3 ; B5 = bitget(A,5)*2^4 ; B6 = bitget(A,6)*2^5 ; B7 = bitget(A,7)*2^6 ; B8 = bitget(A,8)*2^7 ;
figure,subplot(3,3,1),imshow(B1); subplot(3,3,2),imshow(B2); subplot(3,3,3),imshow(B3); subplot(3,3,4),imshow(B4); subplot(3,3,5),imshow(B5); subplot(3,3,6),imshow(B6); subplot(3,3,7),imshow(B7); subplot(3,3,8),imshow(B8);
C1 = bitget(C,1)*2^0; C2 = bitget(C,2)*2^1 ; C3 = bitget(C,3)*2^2 ; C4 = bitget(C,4)*2^3 ; C5 = bitget(C,5)*2^4 ; C6 = bitget(C,6)*2^5 ; C7 = bitget(C,7)*2^6 ; C8 = bitget(C,8)*2^7 ;
figure,subplot(3,3,1),imshow(C1); subplot(3,3,2),imshow(C2); subplot(3,3,3),imshow(C3); subplot(3,3,4),imshow(C4); subplot(3,3,5),imshow(C5); subplot(3,3,6),imshow(C6); subplot(3,3,7),imshow(C7); subplot(3,3,8),imshow(C8);
M1 = bitget(C,8)*2^0; M2 = bitget(A,2)*2^1 ; M3 = bitget(A,3)*2^2 ; M4 = bitget(A,4)*2^3 ; M5 = bitget(A,5)*2^4 ; M6 = bitget(A,6)*2^5 ; M7 = bitget(A,7)*2^6 ; M8 = bitget(A,8)*2^7 ;
figure,subplot(3,3,1),imshow(M1); subplot(3,3,2),imshow(M2); subplot(3,3,3),imshow(M3); subplot(3,3,4),imshow(M4); subplot(3,3,5),imshow(M5); subplot(3,3,6),imshow(M6); subplot(3,3,7),imshow(M7); subplot(3,3,8),imshow(M8);
M=M1+M2+M3+M4+M5+M6+M7+M8; figure,imshow(M),title('Final Image');
___Here's the answer by 'Image Analyst', but I'm still getting the same error.
It's because your watermark is not the same size as your base image. Instead of your "M=...." line, replace it with this code:
[rowsC columnsC] = size(C) % Define location of final watermark. topRow = 10; leftColumn = 20; bottomRow = topRow + rowsC - 1; rightColumn = leftColumn + columnsC - 1; % Get an image as big as the original canvass = zeros(size(A), 'uint8'); canvass(topRow:bottomRow, leftColumn:rightColumn) = C8; % Now they're all the same size so you can add them. M = canvass +M2+M3+M4+M5+M6+M7+M8;___
Kindly help...

Réponse acceptée

Image Analyst
Image Analyst le 2 Nov 2012
It worked for me using the standard demo images moon.tif and cameraman.tif. For us to proceed now, you'd have to upload your images so we can figure out why my code didn't work for your images.
  5 commentaires
Image Analyst
Image Analyst le 4 Nov 2012
You probably figured it out by now, but I did it for you anyway:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.
folder = 'C:\Users\Book\Documents\Temporary';
baseFileName = 'original.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
A=imread(fullFileName);
baseFileName = 'watermark.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
C=imread(fullFileName);
% Crop C if needed.
[rowsA columnsA] = size(A)
[rowsC columnsC] = size(C)
if rowsC > rowsA
C = C(1:rowsA, :);
[rowsC columnsC] = size(C)
end
if columnsC > columnsA
C = C(:, 1:rowsA);
[rowsC columnsC] = size(C)
end
B1 = bitget(A,1)*2^0;
B2 = bitget(A,2)*2^1;
B3 = bitget(A,3)*2^2;
B4 = bitget(A,4)*2^3;
B5 = bitget(A,5)*2^4;
B6 = bitget(A,6)*2^5;
B7 = bitget(A,7)*2^6;
B8 = bitget(A,8)*2^7;
figure;
subplot(3,3,1);
imshow(B1);
title('Bit 1 of A (original)', 'FontSize', fontSize);
subplot(3,3,2);
imshow(B2);
title('Bit 2 of A (original)', 'FontSize', fontSize);
subplot(3,3,3);
imshow(B3);
title('Bit 3 of A (original)', 'FontSize', fontSize);
subplot(3,3,4)
imshow(B4);
title('Bit 4 of A (original)', 'FontSize', fontSize);
subplot(3,3,5);
imshow(B5);
title('Bit 5 of A (original)', 'FontSize', fontSize);
subplot(3,3,6);
imshow(B6);
title('Bit 6 of A (original)', 'FontSize', fontSize);
subplot(3,3,7);
imshow(B7);
title('Bit 7 of A (original)', 'FontSize', fontSize);
subplot(3,3,8);
imshow(B8);
title('Bit 8 of A (original)', 'FontSize', fontSize);
subplot(3,3,9);
imshow(A);
title('All bits of A (original)', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
C1 = bitget(C,1)*2^0;
C2 = bitget(C,2)*2^1;
C3 = bitget(C,3)*2^2;
C4 = bitget(C,4)*2^3;
C5 = bitget(C,5)*2^4;
C6 = bitget(C,6)*2^5;
C7 = bitget(C,7)*2^6;
C8 = bitget(C,8)*2^7;
figure;
subplot(3,3,1);
imshow(C1);
title('Bit 1 of C (watermark)', 'FontSize', fontSize);
subplot(3,3,2)
imshow(C2);
title('Bit 2 of C (watermark)', 'FontSize', fontSize);
subplot(3,3,3)
imshow(C3);
title('Bit 3 of C (watermark)', 'FontSize', fontSize);
subplot(3,3,4);
imshow(C4);
title('Bit 4 of C (watermark)', 'FontSize', fontSize);
subplot(3,3,5);
imshow(C5);
title('Bit 5 of C (watermark)', 'FontSize', fontSize);
subplot(3,3,6);
imshow(C6);
title('Bit 6 of C (watermark)', 'FontSize', fontSize);
subplot(3,3,7);
imshow(C7);
title('Bit 7 of C (watermark)', 'FontSize', fontSize);
subplot(3,3,8)
imshow(C8);
title('Bit 8 of C (watermark)', 'FontSize', fontSize);
subplot(3,3,9)
imshow(C);
title('All Bits of C (watermark)', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
M1 = bitget(C,8)*2^0;
M2 = bitget(A,2)*2^1;
M3 = bitget(A,3)*2^2;
M4 = bitget(A,4)*2^3;
M5 = bitget(A,5)*2^4;
M6 = bitget(A,6)*2^5;
M7 = bitget(A,7)*2^6;
M8 = bitget(A,8)*2^7;
figure;
subplot(3,3,1);
imshow(M1);
title('A1', 'FontSize', fontSize);
subplot(3,3,2)
imshow(M2);
title('A2', 'FontSize', fontSize);
subplot(3,3,3);
imshow(M3);
title('A3', 'FontSize', fontSize);
subplot(3,3,4);
imshow(M4);
title('A4', 'FontSize', fontSize);
subplot(3,3,5);
imshow(M5);
title('A5', 'FontSize', fontSize);
subplot(3,3,6)
imshow(M6);
title('A6', 'FontSize', fontSize);
subplot(3,3,7);
imshow(M7);
title('A7', 'FontSize', fontSize);
subplot(3,3,8);
imshow(M8);
title('A8', 'FontSize', fontSize);
subplot(3,3,9);
imshow(A);
title('A', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Define location of final watermark.
topRow = 1;
leftColumn = 1;
bottomRow = topRow + rowsC - 1;
rightColumn = leftColumn + columnsC - 1;
% Get an image as big as the original
canvass = zeros(size(A), 'uint8');
canvass(topRow:bottomRow, leftColumn:rightColumn) = C8;
% Now they're all the same size so you can add them.
M = canvass +M2+M3+M4+M5+M6+M7+M8;
figure;
imshow(M);
title('M, final watermarked image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
Book
Book le 4 Nov 2012
Thank you so much. I greatly appreciate it.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by