2013-11-24 183 views

回答

3
  1. 計算每個輪廓圖像的平均RGB值。

  2. 在Photoshop(或幾乎任何其他圖形編輯軟件),創建一個包含所有這些顏色

  3. 加載要渲染和規模下來,以使照片的自定義調色板的寬度和高度以像素爲單位對應於您想要在每個維度中顯示的配置文件圖像的數量。

  4. 使用您在步驟1中創建的調色板將圖像的位深度縮小到8位或更少。確保在執行此操作時選擇了「抖動」選項。

  5. 編寫一個腳本來讀取下采樣圖像並創建一個更大的圖像,其中下采樣圖像的每個像素都被轉換爲單個配置文件圖像。

可能的增強功能:如果存在超過256個配置文件圖像,則可能會以比單個顏色表中容納的顏色更多的顏色結束。將相似的顏色聚類在一起,並在渲染大圖像時從這些組中隨機選擇圖像。您甚至可以根據光線和陰影的分佈與原始圖像相應部分的分佈相匹配的程度來選擇圖像。

0

我只是寫了一個程序來辦,今天在C#。我看到了一張照片馬賽克,我突然想到你會怎麼做,所以我把它們作爲一種概念驗證。我的第二次嘗試。有點吹我的腦海:

public void BuildMosaic(string srcFolder, string picFileSrc, string mosaicFile, uint mosaicSizeMultiplier, Size numTiles) 
{ 
    // The file we're going to create a mosaic of 
    Image srcPic = Image.FromFile(picFileSrc); 
    int mosaicWidth = srcPic.Width * (int)mosaicSizeMultiplier; 
    int mosaicHeight = srcPic.Height * (int)mosaicSizeMultiplier; 
    int thumbWidth = mosaicWidth/numTiles.Width; 
    int thumbHeight = mosaicHeight/numTiles.Height; 

    List<ImageInfo> imageInfos = new List<ImageInfo>(); 
    foreach (string filename in Directory.GetFiles(srcFolder)) 
    { 
     string ext = Path.GetExtension(filename).ToUpper(); 
     if (ext == ".JPG" || ext == ".PNG" || ext == ".GIF" || ext == ".JPEG") 
     { 
      imageInfos.Add(ImageInfo.FromImage(filename, new Size(thumbWidth, thumbHeight))); 
     } 
    } 

    int segmentWidth = srcPic.Width/numTiles.Width; 
    int segmentHeight = srcPic.Height/numTiles.Height; 

    Image mosaic = new Bitmap(mosaicWidth, mosaicHeight); 
    Bitmap segBmp = new Bitmap(segmentWidth, segmentHeight); 
    for (int tileX = 0; tileX < numTiles.Width; tileX++) 
    for(int tileY = 0; tileY < numTiles.Height; tileY++) 
    { 
     // Create a bitmap from the original image that we'll try to match a tile to. 
     using (Graphics g = Graphics.FromImage(segBmp)) 
     { 
      g.DrawImage(srcPic, new Rectangle(0, 0, segmentWidth, segmentHeight), new Rectangle(tileX * segmentWidth, tileY * segmentHeight, segmentWidth, segmentHeight), GraphicsUnit.Pixel); 
     } 
     ImageInfo segInfo = ImageInfo.FromImage(segBmp); 

     // Find the matching tile and paint it onto our mosaic 
     ImageInfo match = segInfo.FindMatch(imageInfos.ToArray()); 
     using (Graphics g = Graphics.FromImage(mosaic)) 
     { 
      g.DrawImage(match.Thumbnail, tileX * thumbWidth, tileY * thumbHeight); 
     } 
    } 
    segBmp.Dispose(); 
    mosaic.Save(mosaicFile, ImageFormat.Jpeg); 
    mosaic.Dispose(); 
} 

和的imageinfo類:

public class ImageInfo 
{ 
    private ImageInfo() 
    { 
    } 

    public string Filename { get; private set; } 
    public int Blue { get; private set; } 
    public int Green { get; private set; } 
    public int Red { get; private set; } 
    public System.Drawing.Image Thumbnail { get; private set; } 

    // Calculate color distance 
    private float CalcDistance(ImageInfo otherImage) 
    { 
     int blueDiff = Math.Abs(Blue - otherImage.Blue); 
     int greenDiff = Math.Abs(Green - otherImage.Green); 
     int redDiff = Math.Abs(Red - otherImage.Red); 
     return (float) Math.Sqrt(blueDiff * blueDiff + greenDiff * greenDiff + redDiff * redDiff); 
    } 

    // Find the image with the closes matching color average 
    internal ImageInfo FindMatch(ImageInfo[] list) 
    { 
     ImageMatch closest = null; 
     foreach (ImageInfo ii in list) 
     { 
      if (closest == null) 
      { 
       closest = new ImageMatch() 
       { 
        Distance = CalcDistance(ii), 
        Info = ii 
       }; 
       continue; 
      } 
      float dist = CalcDistance(ii); 
      if (dist < closest.Distance) 
      { 
       closest = new ImageMatch() 
       { 
        Distance = CalcDistance(ii), 
        Info = ii 
       }; 
      } 
     } 
     return closest.Info; 
    } 

    internal static ImageInfo FromImage(System.Drawing.Bitmap srcBmp) 
    { 
     ImageStatistics stats = new ImageStatistics(srcBmp); 
     return new ImageInfo() 
     { 
      Blue = (int)stats.Blue.Mean, 
      Green = (int)stats.Green.Mean, 
      Red = (int)stats.Red.Mean 
     }; 
    } 

    internal static ImageInfo FromImage(string filename, System.Drawing.Size thumbSize) 
    { 
     using(System.Drawing.Bitmap bmp = System.Drawing.Bitmap.FromFile(filename) as System.Drawing.Bitmap) 
     { 
      ImageStatistics stats = new ImageStatistics(bmp); 
      return new ImageInfo() 
      { 
       Filename = filename, 
       Blue = (int)stats.Blue.Mean, 
       Green = (int)stats.Green.Mean, 
       Red = (int)stats.Red.Mean, 
       Thumbnail = new System.Drawing.Bitmap(bmp, thumbSize) 
      }; 
     } 
    } 

    internal class ImageMatch 
    { 
     public float Distance { get; set; } 
     public ImageInfo Info { get; set; } 
    } 
} 

第一個參數是與所有的 「平鋪」 圖像的目錄。他們可以是任何大小。 第二個參數是您要變成拼圖的圖像 第三個參數是輸出鑲嵌文件。 四是乘數。它採用參數2中的圖像,並將高度和寬度乘以此值,這就是您的鑲嵌文件的大小。 最後一個參數是組成馬賽克的X和Y圖塊的數量。

它使用AForge圖像處理庫。

相關問題