小图拼接大图MATLAB实现
生活随笔
收集整理的這篇文章主要介紹了
小图拼接大图MATLAB实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
小圖拼接大圖MATLAB實現
1.實現效果圖
原圖
效果圖
2.代碼
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;tryImg = 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:10for j=1:10image_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)endendfprintf('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)catchdisp(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:xfor j=1:10:yif (i+9>x||j+9>y)continue;endfor m=i:i+9for n=j:j+9t_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));endendt_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;endend%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:9for p=0:9target(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);endendfprintf('x is %d y is %d\n',i,j)end end imwrite(target,'target.jpg');3.思路分析
第二部分思路很簡單,其實就是某塊像素區域rgb分量相近的圖片去替換原圖片中對應像素點即可。在上述代碼中,我選擇將其他圖片先轉化為1010大小的圖片,分別計算rgb三個分類的平均值,之后,用10 10大小的核掃描原圖像,計算該塊內rgb分量平均值,隨后計算|r-t_r|+|g-t_g|+|b-t_b|之和,統計最小的數值,選取對應的圖片像素替換原位置像素值。
4. 總結不足
從效果圖中我們可以發現,其實有很多地方的顏色并不是很匹配,這個原因有兩個,其中一個是備選圖像庫比較少,顏色單調,我的圖像備選庫大小是八百,從網上爬取的一個類型圖片,所以可能顏色大體都是那么幾種。第二個原因就是核大小的選擇,選擇小一點,就能顯示得更像一個圖像。其次,這份代碼其實有點過于暴力,完全匹配每張圖片耗時有點多,尤其是在備選圖庫非常大的時候,之后有時間再做修改~也歡迎補充建議
轉載于:https://www.cnblogs.com/FZfangzheng/p/10852575.html
總結
以上是生活随笔為你收集整理的小图拼接大图MATLAB实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 配置文件application.prop
- 下一篇: Python开发