生成图像的灰度值 [英] Generating Grayscale Values Of An Image

查看:102
本文介绍了生成图像的灰度值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经训练了一个模型(使用著名的MNIST数据)来学习识别从0到9的数字图像.强度值作为特征集提供.

I have trained a model (on the famous MNIST data) to learn to identify images of digits from 0 to 9. The intensity values were provided as feature sets.

现在,我想亲自测试模型,为此,我想说在MS Paint上写一个数字并遍历模型.

Now I want to test the model myself, for that I want to say write a number on MS Paint and run through the model.

我知道如何使用PNG包将图像转换为灰度值,但是在创建具有类似灰度范围的图像时需要帮助.目前,当我尝试在Paint中进行绘制时,它的范围是0:255,这与训练集中的负值也不同.

I know how to use PNG package to convert the image to grayscale values but I need help in creating the image with similar range of grayscle. Currently when I try to draw in Paint it ranges from 0:255, unlike in the training set which has negative values too.

注意:我不知道要获得相似的强度值需要使用哪种图像格式,我最简单的方法是绘画和绘制数字

Note: I do not know what format of image I need to use to get similar intensity values, my easiest way was to go to paint and draw the digit

问题:

im <- matrix(data=rev(X[567,]), nrow=20, ncol=20)

     [,1] [,2] [,3]          [,4]          [,5]          [,6]          [,7]          [,8]          [,9]         [,10]         [,11]         [,12]         [,13]
 [1,]    0    0    0  0.000000e+00  0.0000000000  0.0000000000  0.000000e+00  0.0000000000  0.000000e+00  0.000000e+00  0.0000000000  0.000000e+00  0.000000e+00
 [2,]    0    0    0  0.000000e+00  0.0000000000  0.0000000000  0.000000e+00  0.0000000000  0.000000e+00  0.000000e+00  0.0000000000  4.306236e-04 -4.538135e-03
 [3,]    0    0    0  0.000000e+00  0.0000000000  0.0000000000  0.000000e+00  0.0000000000  0.000000e+00  0.000000e+00  0.0002343973 -1.140496e-02  2.497616e-02
 [4,]    0    0    0  0.000000e+00  0.0000000000  0.0000000000  0.000000e+00  0.0000000000  0.000000e+00  1.084559e-04 -0.0017490639 -1.345621e-02  4.384232e-01
 [5,]    0    0    0  0.000000e+00  0.0000000000  0.0000000000  0.000000e+00  0.0000000000  2.941176e-05 -4.375000e-04 -0.0261209150  2.488099e-01  9.544290e-01
 [6,]    0    0    0  0.000000e+00  0.0000000000  0.0000000000  0.000000e+00  0.0000000000  2.393280e-04 -2.528663e-02  0.1323503711  8.318632e-01  1.015593e+00
 [7,]    0    0    0  0.000000e+00  0.0000000000  0.0000000000  0.000000e+00  0.0001834414 -6.974929e-03  3.770381e-02  0.6445272331  1.033006e+00  8.613194e-01
 [8,]    0    0    0  0.000000e+00  0.0000000000  0.0000000000  2.445885e-05 -0.0001720764 -2.008902e-02  2.677583e-01  1.0012065346  9.811198e-01  3.359074e-01
 [9,]    0    0    0  0.000000e+00  0.0000000000  0.0000000000  3.487541e-04 -0.0256610004  1.337907e-01  8.190443e-01  1.0119159688  5.299348e-01 -2.273144e-03
[10,]    0    0    0  0.000000e+00  0.0000000000  0.0007352941 -1.754459e-02  0.0444801985  6.604204e-01  1.036383e+00  0.7427878881  6.253465e-02 -1.474359e-02
[11,]    0    0    0  0.000000e+00  0.0000000000 -0.0053142872  3.982375e-02  0.6389624523  1.033114e+00  8.733544e-01  0.1483327546 -1.978789e-02  5.014064e-04
[12,]    0    0    0  0.000000e+00  0.0003370098 -0.0245936309  2.214513e-01  0.9496550623  1.001519e+00  4.032970e-01 -0.0262422045 -1.246885e-03  3.668827e-05
[13,]    0    0    0  2.201296e-05 -0.0071477926  0.0124218676  5.890595e-01  1.0428453590  7.664877e-01  6.238350e-02 -0.0170552566  1.654030e-04  0.000000e+00
[14,]    0    0    0 -1.326593e-04 -0.0214352533  0.1659780263  8.681923e-01  1.0228496087  4.833438e-01 -2.121145e-02 -0.0023410267  1.021242e-06  0.000000e+00
[15,]    0    0    0 -1.412275e-03 -0.0192939474  0.4265679126  1.037142e+00  0.9012997670  9.698972e-02 -1.290765e-02  0.0002604167  0.000000e+00  0.000000e+00
[16,]    0    0    0 -1.593035e-03 -0.0186662922  0.4575771889  1.093103e+00  0.7281629027 -3.015387e-02  7.327410e-04  0.0000000000  0.000000e+00  0.000000e+00
[17,]    0    0    0 -3.634600e-04 -0.0099532952  0.1448730596  4.328676e-01  0.1434386592 -9.253983e-03  3.063725e-05  0.0000000000  0.000000e+00  0.000000e+00
[18,]    0    0    0  4.647181e-05  0.0011291835 -0.0175039746 -5.072072e-02 -0.0191029196  1.039501e-03  1.043178e-17  0.0000000000  0.000000e+00  0.000000e+00
[19,]    0    0    0  0.000000e+00  0.0000000000  0.0000000000  0.000000e+00  0.0000000000  0.000000e+00  0.000000e+00  0.0000000000  0.000000e+00  0.000000e+00
[20,]    0    0    0  0.000000e+00  0.0000000000  0.0000000000  0.000000e+00  0.0000000000  0.000000e+00  0.000000e+00  0.0000000000  0.000000e+00  0.000000e+00
              [,14]         [,15]         [,16]         [,17] [,18] [,19] [,20]
 [1,]  0.0000000000  0.000000e+00  0.000000e+00  0.000000e+00     0     0     0
 [2,] -0.0216402846 -1.239362e-02  2.570125e-05  6.076389e-05     0     0     0
 [3,]  0.6271519370  6.741898e-01  1.490166e-02 -3.797542e-03     0     0     0
 [4,]  1.0680277608  5.460697e-01 -8.837061e-03 -2.389553e-03     0     0     0
 [5,]  0.8673059811  1.769967e-01 -1.832898e-02 -3.108660e-04     0     0     0
 [6,]  0.4489610566 -2.979454e-02 -2.363971e-03  5.238971e-05     0     0     0
 [7,]  0.0371314849 -7.122634e-03  2.040441e-04  0.000000e+00     0     0     0
 [8,] -0.0279941706  7.847214e-04  0.000000e+00  0.000000e+00     0     0     0
 [9,] -0.0058922249  1.244466e-17  0.000000e+00  0.000000e+00     0     0     0
[10,]  0.0003111383  9.320045e-32  0.000000e+00  0.000000e+00     0     0     0
[11,]  0.0000245098  0.000000e+00  0.000000e+00  0.000000e+00     0     0     0
[12,]  0.0000000000  0.000000e+00  0.000000e+00  0.000000e+00     0     0     0
[13,]  0.0000000000  0.000000e+00  0.000000e+00  0.000000e+00     0     0     0
[14,]  0.0000000000  0.000000e+00  0.000000e+00  0.000000e+00     0     0     0
[15,]  0.0000000000  0.000000e+00  0.000000e+00  0.000000e+00     0     0     0
[16,]  0.0000000000  0.000000e+00  0.000000e+00  0.000000e+00     0     0     0
[17,]  0.0000000000  0.000000e+00  0.000000e+00  0.000000e+00     0     0     0
[18,]  0.0000000000  0.000000e+00  0.000000e+00  0.000000e+00     0     0     0
[19,]  0.0000000000  0.000000e+00  0.000000e+00  0.000000e+00     0     0     0
[20,]  0.0000000000  0.000000e+00  0.000000e+00  0.000000e+00     0     0     0

image(1:20, 1:20, im, col=gray((0:255)/255)) #But My Lower Values Extend Past Help Required Here too to set the range (NOT 0:255)

我想自己创建(绘制)某些数字并进行测试,但是我想确保它们属于相同的值范围,我该如何实现?

I want to create (draw) certain digits myself and test, but I want to make sure they belong to the same range of values, how do I achieve that?

推荐答案

您想缩放新数据(来自MSPaint),使其与您用于训练模型的数据相匹配.获取训练数据集的均值和方差,然后使用scale将其应用于新图像.

You want to scale the new data (from MSPaint) so it matches that of the data you used to train your model. Get the mean and variance of the training dataset and use scale to apply it to the new image.

means <- colMeans(training_data)
std <- apply(training_data, 2, FUN = sd, na.rm = T)
new_im <- scale(im, center = means, scale = std)

我还建议您回去创建一个新模型,在此模型中事先适当地缩放训练数据.像@MarkSetchell一样,我对PNG中的负值表示困惑.

I would also suggest going back and creating a new model where the training data is properly scaled beforehand. Like @MarkSetchell I'm confused as to what negative values mean in PNGs.

这篇关于生成图像的灰度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆