2012-03-16 26 views
3

如何用Mathematica中的自定義圖像替換二值化圖像的像素? 我想,一旦我有一個矩陣M 0或1個條目取決於像素是白色還是黑色(我可以通過使用Binarize和操縱輸出一點點),我可以使用Graphics []來放置一個自定義圖像當網格中存在1時,方格邊界在一個網格中,而在0時爲平坦背景,但我不確定如何執行此操作。 預先感謝您:)用Mathematica中的自定義圖像替換像素?

回答

3

如果M是包含0和1的和image0/image1是要顯示的圖像的矩陣:

image0 = Graphics[{Red, Disk[]}, ImageSize -> 10]; 
image1 = Graphics[{Blue, Rectangle[]}, ImageSize -> 10]; 
M = {{0, 1, 0}, {1, 1, 1}, {1, 0, 0}}; 

你可以只是這樣做:

GraphicsGrid[M /. {0 -> image0, 1 -> image1}] 

enter image description here

或者,如果您希望0爲空:

GraphicsGrid[M /. {0 -> "", 1 -> image1}] 

enter image description here

4

這裏有一種方法:

mat = RandomInteger[1, {10, 10}]; 
Graphics[MapIndexed[If[#1 == 1, Disk, Circle][#2, 0.4] &, mat, {2}]] 

Mathematica graphics

我喜歡用MapIndexed各種版本這一點。您可以使用任何其他圖形對象而不是DiskCircle。只要創建一個函數,以一個位置作爲其參數併產生該對象。