图像马赛克的实现

[参考文档]:

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实现的图片马赛克工具

 

[效果展示]:

   

 

posted @ 2015-06-25 21:05  lichmama  阅读(890)  评论(0编辑  收藏  举报