将文本字符插入图像像素

时间:2013-04-07 07:28:27

标签: matlab

我正在寻找一个用matlab将字符插入图像的代码。

img是我的封面图片,msg是我的文字。我收到此错误

Error using ==> bitset
Operands to BIT Ops must be numeric.

但我的是数字。它不是?我该如何说清楚?

clear all;
img=imread('img.jpg','jpg');
msgopen=fopen('ramz.txt');
msg=fread(msgopen);
msgbin=dec2bin(msg);
msgsize=size(msg);
x=msgsize(1);
msgsizebin=dec2bin(x,8);
imgh=size(img,1);
imgw=size(img,2);
for i=1:imgh;
  for j=1:imgw;
    img(i,j)=bitset(img(i,j),msgbin(i,1));
  end
end

1 个答案:

答案 0 :(得分:0)

您可以使用text功能,然后使用hardcopy功能创建图像,并将文本编码到数据中;

img=imread('img.jpg','jpg');
msgopen=fopen('ramz.txt');
msg=fread(msgopen);
hfig=figure;
imshow(img);
hold on;
text(x_loc,y_loc,msg); %x_loc and y_loc are the locations where you want the text to   
                       % appear. msg is assumed to be a string.
orig_mode = get(hfig, 'PaperPositionMode');
set(hfig, 'PaperPositionMode', 'auto');
img_with_text = hardcopy(hfig, '-Dzbuffer', '-r0');