Can someone please look at this document I attached, and also my code. Im not sure if my code is correct or not. I do get the two pictures with very minimal change at the end. I just want some insight into whether my code looks good or not, it feels like im missing something.
pic=imread('Cat.png')
imshow(pic)
title('original picture')
[nrow ncol]=size(pic)
cpic=pic
Mi=input('Input message:','s')
M=double(Mi)
bin_m=dec2bin(8)
bin_m=bin_m'
bin_m=bin_m(:)
if length(M)>nrow*ncol
error('The message is too long!')
end
k=1
for r=1:nrow
for c=1:ncol
if strcmpi(bin_m(k),'0');
cpic(r,c)=cpic(r,c);
elseif strcmpi(bin_m(k),'1') && cpic(r,c)==255;
cpic(r,c)=cpic(r,c)-1;
else
cpic(r,c)=cpic(r,c)+1;
end
end
end
imshow(pic)
figure;
imshow(cpic)

1 Kommentar

Tommy Halim
Tommy Halim am 9 Apr. 2019
Do you have code to extract hidden message from your code?

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 19 Nov. 2015

1 Stimme

bin_m=dec2bin(8) is almost certainly wrong. Perhaps you meant
bin_m=dec2bin(M, 8);

4 Kommentare

Brian Tiffman
Brian Tiffman am 19 Nov. 2015
Now it looks better, Thanks man. Overall, do you think my code looks good?
No. Many reasons, or ways to improve it.
For example, not having a semicolon after imread() will cause the whole image to spew out into the command window.
Another reason is the lack of comments.
Also you have variable names that are not very descriptive. This and the previous problem would make your code hard to maintain should another individual inherit it.
Next, you're using size() wrong for an image. Why? See this: http://blogs.mathworks.com/steve/2011/03/22/too-much-information-about-the-size-function/ To fix:
[rows, columns, numberOfColorChannels] = size(pic);
Next, you might learn how to use subplot() so that your pictures show up on the same figure instead of stacking up a bunch of overlapping windows.
Next, k does not change in your nested for loop so bin_m(k) is always bin_m(1). What's the point of that?
And the error message could be more informative and friendlier. Instead of this:
if length(M)>nrow*ncol
error('The message is too long!')
end
try it like this:
if length(M) > rows * columns
errorMessage = sprintf('Error: your message is %d characters long\nbut it cannot be longer than %d characters long,\nwhich is the number of pixels in the image', length(M), rows*columns);
uiwait(warndlg(errorMessage));
return;
end
Brian Tiffman
Brian Tiffman am 23 Nov. 2015
I've tried incrementing k by using k=k+1, but I keep getting an error message. I'm not sure whats going on. The link you posted at the end of your last comment did help, and is a good read for me, so thanks.
Image Analyst
Image Analyst am 23 Nov. 2015
After you read that last link, you'll know what to do if you need more help.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by