原圖git
效果圖ide
files = dir(fullfile('D:\document\GitHub\homework\digital image processing\image_spider\animation\','*.jpg')); target=imread('D:\document\GitHub\homework\digital image processing\image_spider\target.jpg'); lengthFiles = length(files); error=0; data=[]; for i = 1:lengthFiles; try Img = imread(strcat('D:\document\GitHub\homework\digital image processing\image_spider\animation\',files(i).name));%文件所在路徑 %改變大小 newimg=imresize(Img,[10,10]); %獲取三份量 imager=newimg(:,:,1); imageg=newimg(:,:,2); imageb=newimg(:,:,3); %統計平均值 image_r=0; image_g=0; image_b=0; for m=1:10 for j=1:10 image_r=int32(image_r)+int32(imager(m,j)); image_g=int32(image_g)+int32(imager(m,j)); image_b=int32(image_b)+int32(imager(m,j)); %fprintf('i is %d image_r is %d image_g is %d image_b is %d\n',i,image_r,image_g,image_b) end end fprintf('i is %d image_r is %d image_g is %d image_b is %d\n',i,image_r,image_g,image_b) image_r=image_r/100; image_g=image_g/100; image_b=image_b/100; data=[data image_r image_g image_b]; %disp(strcat('D:\document\GitHub\homework\digital image processing\image_spider\animation\',files(i).name)) %打印文件路徑 %imshow(Img) catch disp(strcat('D:\document\GitHub\homework\digital image processing\image_spider\animation\',files(i).name)) %打印文件路徑 error=error+1; end end sizetarget=size(target); x=sizetarget(1,1); y=sizetarget(1,2); t_image_r=0; t_image_g=0; t_image_b=0; for i=1:10:x for j=1:10:y if (i+9>x||j+9>y) continue; end for m=i:i+9 for n=j:j+9 t_image_r=int32(t_image_r)+int32(target(m,n,1)); t_image_g=int32(t_image_g)+int32(target(m,n,2)); t_image_b=int32(t_image_b)+int32(target(m,n,3)); end end t_image_r=t_image_r/100; t_image_g=t_image_g/100; t_image_b=t_image_b/100; %fprintf('r is %d g is %d b is %d\n',t_image_r,t_image_g,t_image_b) cha=765; mini=1; sizedata=size(data); for q=1:3:sizedata(1,2) cha1=abs(t_image_r-data(q))+abs(t_image_g-data(q+1))+abs(t_image_b-data(q+2)); if(cha1<cha) cha=cha1; mini=floor(q/3)+1; end end %disp(mini) Img = imread(strcat('D:\document\GitHub\homework\digital image processing\image_spider\animation\',files(mini).name));%文件所在路徑 newimg=imresize(Img,[10,10]); for q=0:9 for p=0:9 target(i+q,j+p,1)=newimg(q+1,p+1,1); target(i+q,j+p,2)=newimg(q+1,p+1,2); target(i+q,j+p,3)=newimg(q+1,p+1,3); end end fprintf('x is %d y is %d\n',i,j) end end imwrite(target,'target.jpg');
第二部分思路很簡單,其實就是某塊像素區域rgb份量相近的圖片去替換原圖片中對應像素點便可。在上述代碼中,我選擇將其餘圖片先轉化爲1010大小的圖片,分別計算rgb三個分類的平均值,以後,用10 10大小的核掃描原圖像,計算該塊內rgb份量平均值,隨後計算|r-t_r|+|g-t_g|+|b-t_b|之和,統計最小的數值,選取對應的圖片像素替換原位置像素值。3d
從效果圖中咱們能夠發現,其實有不少地方的顏色並非很匹配,這個緣由有兩個,其中一個是備選圖像庫比較少,顏色單調,個人圖像備選庫大小是八百,從網上爬取的一個類型圖片,因此可能顏色大致都是那麼幾種。第二個緣由就是核大小的選擇,選擇小一點,就能顯示得更像一個圖像。其次,這份代碼其實有點過於暴力,徹底匹配每張圖片耗時有點多,尤爲是在備選圖庫很是大的時候,以後有時間再作修改~也歡迎補充建議code