欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

图片处理的一些记录(matlab)

程序员文章站 2022-04-09 09:15:36
...

主要的任务是将人从背景里面提取出来然后放进新背景里面。

clear;
close all;

orl_img=imread('C:\图像库\人脸库\ORL\faceJpg\s2\1.jpg');
bg_img=imread('C:\图像库\backgrounds\10.tiff');
% imshow(bg_img);

%orl_img = imresize(orl_img,[56,46]);
% orl_img = imresize(orl_img,[84,69]);

[a b] = size(orl_img);
temp_r =zeros(a,b);
u = 60;
l = 200;
temp3 = bg_img(u:u+139,l:l+139);
%temp2 = temp3(29:140,25:116);
temp2 = temp3(1:112,1:92);%11-1
% temp2 = temp3(1:112,49:140);%11-2
% temp2 = temp3(29:140,1:92);%11-3
% temp2 = temp3(29:140,49:140);%11-4
% temp2 = temp3(1:56,1:46);%12-1
% temp2=temp3(1:84,1:69);%34-1
% temp2 = temp3(85:140,95:140);%12-4
% temp2 = temp3(1:56,95:140);%12-2
%temp2 = temp3(1:84,72:140);%34-2
% temp2 = temp3(85:140,1:46);%12-3
% temp2=temp3(57:140,1:69);%34-3
% temp2=temp3(57:140,72:140);%34-4

% figure(6);
% imshow(temp3);
%imwrite(temp3,'C:\Users\GUOPENG\Desktop\fixed1_1.tiff','tiff');

Px=[-1 0 1;-2 0 2;-1 0 1];  %sobel算子
Py=[-1 -2 -1;0 0 0;1 2 1];
for i= 1:1
    Ip=abs(filter2(Px,orl_img))+abs(filter2(Py,orl_img));
end
[m n]=size(Ip);
weight_r=zeros(m,n);

for i=1:(m-1)
    tempa=Ip(i,:);
    k=find(tempa>50);
    weight_r(i,k(2):k(length(k)-1))=1;
end
% for i = 1:m
%     temp = orl_img(i,:);
%     find_edge = find(temp>20);
%     temp_weight = weight_r(i,:);
%     find_weight = find(temp_weight);
%     [o p]=size(find_edge);
%     [f g]=size(find_weight);
%     if p>0&&g>0
%         if(find_edge(1)>find_weight(1))
%             weight_r(i,find_weight(1):find_edge(1))=0;
%         end
%         if(find_edge(p)<find_weight(g))
%             weight_r(i,find_edge(p):find_weight(g))=0;
%         end
%     end
% end
%     
for i = 1:6
    weight_r = medfilt2(weight_r,[7 7]);
end

orl_img = uint8(orl_img);
temp2 = uint8(temp2);
temp_r = uint8(temp_r);
weight_r = uint8(weight_r);
temp_r=orl_img.*weight_r+temp2.*abs(1-weight_r);
%temp = medfilt2(temp);


fin_img = uint8(temp3);
fill=find(temp_r<100);
for i = 1:length(fill)
    if(~weight_r(fill(i)))
        temp1 = reshape(temp2,[1,a*b]);
        temp_r(fill(i))=temp1(fill(i));
    end
end
%fin_img(29:140,25:116) = temp_r;
%fin_img(1:56,1:46)=temp_r;%12-1
% fin_img(1:84,1:69)=temp_r;%34-1
% fin_img(1:56,95:140)=temp_r;%12-2
%fin_img(85:140,95:140)=temp_r;%12-4
% fin_img(85:140,1:46)=temp_r;%12-3
% fin_img(1:84,72:140)=temp_r;%34-2
% fin_img(57:140,1:69)=temp_r;%34-3
% fin_img(57:140,72:140)=temp_r;%34-4

fin_img(1:112,1:92) = temp_r;%11-1
% fin_img(1:112,49:140) = temp_r;%11-2
% fin_img(29:140,49:140) = temp_r;%11-4
% fin_img(29:140,1:92) = temp_r;%11-3


fill2=find(fin_img<48);
for i = 1:length(fill2)
    if(fill2(i)<5600)
        fin_img(fill2(i))=temp3(fill2(i));
    end
    if(fill2(i)>10000)
        fin_img(fill2(i))=temp3(fill2(i));
    end
end


imwrite(fin_img,'C:\Users\GUOPENG\Desktop\11_1.tiff','tiff');
figure(1);
imshow(fin_img);
%A2=fspecial('gaussian');      %生成高斯序列
%fin_img=filter2(A2,fin_img)/255;  
%fin_img = medfilt2(fin_img);
%fin_img=adp_median(fin_img);


% Ip = uint8(Ip);
% figure(3);
% imshow(200*weight_r);
% figure(4);
% imshow(orl_img);