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

Tommy Halim
Tommy Halim 2019년 4월 9일
Do you have code to extract hidden message from your code?

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2015년 11월 19일

1 개 추천

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

댓글 수: 4

Brian Tiffman
Brian Tiffman 2015년 11월 19일
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 2015년 11월 23일
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 2015년 11월 23일
After you read that last link, you'll know what to do if you need more help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

태그

질문:

2015년 11월 19일

댓글:

2019년 4월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by