天天看點

在simulink中實作圖像的讀取和縮放操作

目錄

​​一、理論基礎​​

​​二、核心程式​​

​​三、仿真測試結果​​

作者ID  :fpga和matlab

擅長技術:
1.無線基帶,無線圖傳,編解碼 
2.機器視覺,圖像處理,三維重建 
3.人工智能,深度學習 
4.智能控制,智能優化
5.其他      

一、理論基礎

根據MATLAB中的程式可知,圖像縮放的核心代碼語句如下:

在simulink中實作圖像的讀取和縮放操作

         即,你通過imresize函數,來将圖像縮放為所需要大小的新的圖像。而Simulink沒有這個子產品可以直接使用,是以我們将通過simulink中的子產品化模組化,實作這個函數的基本功能。

        首先是原始圖像的讀取和顯示,具體子產品如下所示:

在simulink中實作圖像的讀取和縮放操作

這個部分是實作原始圖像的讀取和顯示的功能。運作後會自動産生如下的原始圖像:

在simulink中實作圖像的讀取和縮放操作

二、核心程式

讀取Simulink之後,我們需要在simulink中進行圖像的縮放功能的實作:

       MATLAB中的IMRESIZE函數,其圖像縮放的主要實作原理為:

在simulink中實作圖像的讀取和縮放操作

下面介紹一下SIMULINK的設計步驟:

        這裡我之前沒注意到,Simulink裡面有提供的一個resize子產品,但是我們直接使用這個子產品,并無法得到比較好的效果。

        是以,我們還是設計了一組自己建構的Simulink子產品,但是需要使用Simulink的内嵌MATLAB子產品。

整個流程如下所示:

在simulink中實作圖像的讀取和縮放操作

即讀入圖檔,然後獲得RGB的三個通道。然後進行如下的處理:

在simulink中實作圖像的讀取和縮放操作

由于讀入的圖像是UINT8類型的資料,無法直接進行MATLAB的運算,我們需要将資料通過conversion轉換為double類型的資料,然後進行處理。

在simulink中實作圖像的讀取和縮放操作

這三個子產品分别為放大縮小子產品,亮度均衡子產品和濾波子產品,這樣得到放大或者縮小後的子產品就比較清晰了。

function J = func_b2cyv_simulink(I);

Parameter_SetUp;
[nrows,ncols,k]=size(I);

width  = W;                              
height = H;

J = zeros(width,height);
 
widthScale = nrows/width;
heightScale = ncols/height;

for x = 3:width-2                            
   for y = 3:height-2
       xx = x * widthScale;                     
       yy = y * heightScale;
       if (xx/double(uint16(xx)) == 1.0) & (yy/double(uint16(yy)) == 1.0)       
           J(x,y) = I(int16(xx),int16(yy));
       else                                    
            a = double(uint16(xx)); % (a,b) is the base-dot 
            b = double(uint16(yy)); 
            x11 = double(I(a,b)); % x11 <- I(a,b) 
            x12 = double(I(a,b+1)); % x12 <- I(a,b+1) 
            x21 = double(I(a+1,b)); % x21 <- I(a+1,b) 
            x22 = double(I(a+1,b+1)); % x22 <- I(a+1,b+1) 
            J(x,y) = sqrt((x11 + x12)^2/4 + (x21+x22)^2/4);
       end
    end
end
        
function J = func_blinear_simulink(I);

Parameter_SetUp;
[nrows,ncols,k]=size(I);

width  = W;                              
height = H;

J = zeros(width,height);
 
widthScale = nrows/width;
heightScale = ncols/height;

for x = 3:width-2                            
   for y = 3:height-2
       xx = x * widthScale;                     
       yy = y * heightScale;
       if (xx/double(uint16(xx)) == 1.0) & (yy/double(uint16(yy)) == 1.0)       
           J(x,y) = I(int16(xx),int16(yy));
       else                                    
            a = double(uint16(xx)); % (a,b) is the base-dot 
            b = double(uint16(yy)); 
            x11 = double(I(a,b)); % x11 <- I(a,b) 
            x12 = double(I(a,b+1)); % x12 <- I(a,b+1) 
            x21 = double(I(a+1,b)); % x21 <- I(a+1,b) 
            x22 = double(I(a+1,b+1)); % x22 <- I(a+1,b+1) 
            J(x,y) = ( (b+1-yy) * ((xx-a)*x21 + (a+1-xx)*x11) + (yy-b) * ((xx-a)*x22 +(a+1-xx) * x12) ); % calculate J(x,y) 
       end
    end
end
        
function J = func_nearest_simulink(I);

Parameter_SetUp;
[nrows,ncols,k]=size(I);

width  = W;                              
height = H;

J = (zeros(width,height));
 
widthScale = nrows/width;
heightScale = ncols/height;

for x = 3:width-2                            
   for y = 3:height-2
       xx = x * widthScale;                     
       yy = y * heightScale;
       if (xx/double(uint16(xx)) == 1.0) & (yy/double(uint16(yy)) == 1.0)       
           J(x,y) = I(int16(xx),int16(yy));
       else                                    
           a = double(round(xx));              
           b = double(round(yy));
           J(x,y) = I(a,b); 
       end
    end
end
       

三、仿真測試結果

繼續閱讀