天天看點

圖像馬賽克的實作

[參考文檔]:

http://dongtingyueh.blog.163.com/blog/static/461945320127217563098/
http://www.cnblogs.com/sndnnlfhvk/archive/2012/03/27/2419801.html
      

[算法說明]:

圖像馬賽克效果其實就是将圖像分成大小一緻的圖像塊,每一個圖像塊都是一個正方形,并且在這個正方形中所有像素值都相等。我們可以将這個正方形看作是一個模闆視窗,模闆中對應的所有圖像像素值都等于該模闆的左上角第一個像素的像素值,這樣的效果就是馬賽克效果,而正方形模闆的大小則決定了馬賽克塊的大小,即圖像馬賽克化的程度。      

[實作代碼]:

Function Mosaic(imgpath, savepath)
On Error Resume Next
    Dim ImgFile
    Dim ImgProcess
    Dim ARGBData
    
    Set ImgFile = CreateObject("WIA.ImageFile")
    Set ImgProcess = CreateObject("WIA.ImageProcess")
    
    Call ImgFile.LoadFile(imgpath)
    Set ARGBData = ImgFile.ARGBData
    
    Width = ImgFile.Width
    Height = ImgFile.Height
    \'here\'s the MOSAIC Processing, and default mosaic-matrix N is 4x4
    N = 4
    
    For y = 1 To Height
        For x = 1 To Width
        Position = (y-1)*Width + x
            If y Mod N = 1 Then
                If x Mod N = 1 Then
                    Color = ARGBData(Position)
                Else
                    ARGBData(Position) = Color
                End If
            Else
                ARGBData(Position) = ARGBData(Position - Width)
            End If
        Next
    Next
    
    Call ImgProcess.Filters.Add(ImgProcess.FilterInfos("ARGB").FilterID)
    Set ImgProcess.Filters(1).Properties("ARGBData") = ARGBData
    Set ImgFile = ImgProcess.Apply(ImgFile)
    
    Call ImgFile.SaveFile(savepath)
Mosaic = Err.Number
End Function

imgpath = "D:\2.jpg"
savepath = "D:\2_mosaiced.jpg"
Call Mosaic(imgpath, savepath)
Wscript.Echo "Mosaic Done"      

PS: VBS+WIA實作的圖檔馬賽克工具

[效果展示]:

圖像馬賽克的實作
圖像馬賽克的實作