2014-02-06 35 views
1

我试图将导入的彩色图片转换为灰度。Mathematica图片导入灰度错误

这是我尝试过,到目前为止,但简单的数学崩溃这段代码执行后,也许你可以找到错误,无法识别我做错了什么:

SetDirectory[NotebookDirectory[]] 
testimage = Import["test.jpg"] 
matrixpic = getMatrix[testimage] 

matrixpic = getMatrix[testimage] 
greypic = 
    Graphics[ 
    Raster[ 
     matrixpic, {{0, 0}, {sizeX[matrixpic], sizeY[matrixpic]}}, {0, 
     255}, ColorFunction -> (GrayLevel[#[[1]]*0.3 + #[[2]]*0.5 + #[[ 
     3]]*0.2] &) 
    ], 
    ImageSize -> {sizeX[matrixpic], sizeY[matrixpic]}, 
    AspectRation -> Automatic 
] 

Show[greypic] 
+0

'ColorConvert [testimage,“Grayscale”]'有什么问题? –

+0

想要指定值,你可以看到? –

回答

1

这个工程,是更多的Mathematica风格的代码。

SetDirectory[NotebookDirectory[]]; 
img = Import["55th-All-Japan-Kendo-Champ2007-4.jpg"]; 
colorXform[p_] := p[[1]]*0.3 + p[[2]]*0.5 + p[[3]]*0.2; 
newImg = Image[Map[colorXform, ImageData[img], {2}]]; 
Show[newImg] 
2

我认为进行这种转换的最佳方法是使用ImageApplyDot

img = Import["ExampleData/lena.tif"] 

enter image description here

ImageApply[{.3, .5, .2}.# &, img] 

enter image description here


请向在专用数学 StackExchange网站你的未来的问题:

enter image description here

1

你的代码可以在简化为

img = Import["ExampleData/lena.tif"]; 
matrixpic = ImageData[img, DataReversed -> True]; 
Graphics[Raster[matrixpic, 
    ColorFunction -> (GrayLevel[{.3, .5, .2}.#] &)]] 

这工作没有错误的Mathematica 8.0.4。