Can anybody help me to execute this code......​..........​.....Messa​ge too large to fit in Cover Object..............I am getting this error,

1 次查看(过去 30 天)
clear all
close all
clc
[filename, pathname] = uigetfile('*.bmp', 'Pick an M-file');
if isequal(filename,0) || isequal(pathname,0)
warndlg('Image is not selected');
else
a=imread(filename);
figure(1)
imshow(a)
a=rgb2gray(a);
figure(2);
imshow(a)
title('converted image from rgb to gray')
a=imresize(a,[512,512]);
figure(3)
imshow(a,[])
title('cover image')
a=im2double(a);
%imshow(a);
end
[filename, pathname] = uigetfile('*.bmp', 'Pick an M-file');
if isequal(filename,0) || isequal(pathname,0)
warndlg('Image is not selected');
else
secret=imread(filename);
%imshow(secret);
figure(4)
imshow(secret,[])
title('Watermark Image')
end
m=1;
% k=30;
for i=1:8:256
for j=1:8:256
for x=0:7
for y=0:7
img(x+1,y+1)=a(i+x,j+y);
end
end
k=0;
for l=1:8
img_expect{k+1}=img(:,l)*img(:,l)';
k=k+1;
end
imgexp=zeros(8:8);
for l=1:8
imgexp=imgexp+(1/8)*img_expect{l};%expectation of E[xx']
end
img_mean=zeros(8,1);
for l=1:8
img_mean=img_mean+(1/8)*img(:,l);
end
img_mean_trans=img_mean*img_mean';
img_covariance=imgexp - img_mean_trans;
[v{m},d{m}]=eig(img_covariance);
temp=v{m};
m=m+1;
for l=1:8
v{m-1}(:,l)=temp(:,8-(l-1));
end
for l=1:8
trans_img1(:,l)=v{m-1}*img(:,l);
end
for x=0:7
for y=0:7
transformed_img(i+x,j+y)=trans_img1(x+1,y+1);
end
end
end
end
cover_object=double(transformed_img);
Mc=size(cover_object,1); %Height
Nc=size(cover_object,2); %Width
% determine maximum message size based on cover object, and blocksize
max_message=Mc*Nc/(512^2);
% read in the message image
message=double(secret);
Mm=size(message,1); %Height
Nm=size(message,2); %Width
% reshape the message to a vector
message=round(reshape(message,Mm*Nm,1)./256);
% check that the message isn't too large for cover
if (length(message) >= max_message)
error('Message too large to fit in Cover Object')
end
% pad the message out to the maximum message size with ones's
message_vector=ones(1,max_message);
message_vector(1:length(message))=message;
% generate shell of watermarked image
watermarked_image=cover_object;
% read in key for PN generator
file_name='_key.bmp';
% key=double(imread(file_name))./256;
% reset MATLAB's PN generator to state "key"
rand('state',35);
% generate PN sequence
pn_sequence_zero=round(2*(rand(1,sum(sum(512)))-0.5));
pn_sequence_one=round(2*(rand(1,sum(sum(512)))-0.5));
% process the image in blocks
x=1;
y=1;
for kk = 1:length(message_vector)
% transform block using DCT
dct_block=(cover_object(y:y+blocksize-1,x:x+blocksize-1));
% if message bit contains zero then embed pn_sequence_zero into the mid-band
% componants of the dct_block
ll=1;
if (message_vector(kk)==0)
for ii=1:512
for jj=1:512
if (dct_block(jj,ii)==1)
dct_block(jj,ii)=(dct_block(jj,ii))+k*pn_sequence_zero(ll);
ll=ll+1;
end
end
end
else
for ii=1:512
for jj=1:512
if (dct_block(jj,ii)==1)
dct_block(jj,ii)=(dct_block(jj,ii))+k*pn_sequence_one(ll);
ll=ll+1;
end
end
end
end
% transform block back into spatial domain
watermarked_image(y:y+512-1,x:x+512-1)=((dct_block));
% move on to next block. At and of row move to next row
if (x+512) >= Nc
x=1;
y=y+512;
else
x=x+512;
end
end
trans_img=dct_block.*mask;
for l=1:4
inv_trans_img(:,l)=v{m-1}'*dct_block(:,l);
end
for x=0:3
for y=0:3
inv_transformed_img(i+x,j+y)=inv_trans_img(x+1,y+1);
imwrite(uint8(inv_transformed_img),'Lena_wtermarked_image.bmp','bmp');
end
end
figure(5)
imshow(transformed_img);
title('transformed image')
figure(6)
imshow(inv_transformed_img);
title('inverse transformed image')
% imwrite(uint8(inv_transformed_img),'Lena_wtermarked_image.bmp','bmp');
figure(7)
LWI=imread('Lena_wtermarked_image.bmp');
imshow(LWI);
title('Watermarked Image')
  1 个评论
Rik
Rik 2018-9-25
Today I formatted your code, next time, use the {}Code button. See here for a GIF showing how to do it. You should also read this tutorial for asking questions.
There are 9 warnings from m-lint when I paste this into Matlab. Sometimes warnings are unavoidable, but you should have a look at them.
For posting a question you should try reducing your code to a MWE which makes sure we can reproduce the problem with a self-contained block of code.

请先登录,再进行评论。

回答(1 个)

Guillaume
Guillaume 2018-9-25
There are many things wrong with your question. You didn't bother formatting your post properly, thankfully Rik did that for you. You give us an error message but don't include the full error message leaving it up to us to figure out which line could possibly generate the error. You dump a huge amount of uncommented and badly written code with no explanation of what it's supposed to do, again leaving it up to us to figure it out.
But I think the biggest problem is that you don't appear to know yourself what the code does and what are its prerequisite. Had you spent time to understand it you would know the cause of the error and would know there's nothing we can do about it.
Clearly, the code is meant to embend a secret image into another image (the cover image). Of course, the cover image need to be big enough that you can fit the secret image in it. The code checks for that and errors if that is not the case, which is exactly what you got.
Solution: choose a cover image big enough for your secret. You should have figured that out yourself.

标签

Community Treasure Hunt

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

Start Hunting!

Translated by