是否需要帮助将BMP图像转换为[R]中的矩阵? [英] Need help converting a BMP image to a matrix in [R]?

查看:93
本文介绍了是否需要帮助将BMP图像转换为[R]中的矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对R非常陌生,我想知道是否有一种简单的方法可以将BMP图像转换为R中的矩阵.主要是,我正在寻找可以提供帮助的软件包.矩阵中每个元素的值都将对应于颜色.

I'm very new to R, and I was wondering if there was a simple way to convert a BMP image to a matrix in R. Mainly, I'm looking for any package that can help. The values of each element in the matrix would correspond to colors.

推荐答案

在CRAN软件包列表中搜索"bmp"会拉出bmp和其他一些内容,为简便起见,我将仅使用此软件包.

A search for "bmp" on the CRAN packages list pulls up bmp and some others, for brevity I will just use this package.

library(bmp)
fl <- system.file("images", "5HT1bMARCM-F000001_seg001_lsm.bmp",  package = "bmp")
b <- read.bmp(fl)

此对象是一个数组,其中包含有关文件的一些信息:

This object is an array, with some information about the file:

 str(b)
 int [1:206, 1:206, 1:3] 107 111 119 123 115 119 119 139 143 143 ...
 - attr(*, "header")=List of 13
  ..$ filesize     : num 127774
  ..$ offset       : num 54

这是一个3D阵列:

dim(b)
[1] 206 206   3

有一个as.raster函数,它带有一个可选的max参数:

There's an as.raster function, which takes an optional max argument:

m <- as.raster(b, max = 255)

此矩阵m现在是2D颜色矩阵(十六进制).

This matrix m is now a 2D matrix of colours (hex).

str(m)
 'raster' chr [1:206, 1:206] "#6B0303" "#6F0303" "#770303" ...


dim(m)
[1] 206 206

让我们绘制该图,我们需要建立一个图,然后找出它的范围,以便我们用图像填充设备.

Let's plot this thing, we need to set up a plot and then find out it's range so we can fill the device with our image.

plot(1, type = "n", axes = FALSE, xlab = "", ylab = "")
usr <- par("usr")
rasterImage(m, usr[1], usr[3], usr[2], usr[4])

您的需求将取决于BMP文件使用的存储选项以及用于读取该文件的软件.

Your needs will depend on the storage options used by your BMP file and on which software you use to read it.

还有其他选项,带有readbitmap软件包和rgdal(也许可以通过raster进行选择),但这取决于您可以在计算机上安装的内容.

There are other options, with the readbitmap package and with rgdal (and perhaps that via raster), but it will depend on what you can install on your machine.

这篇关于是否需要帮助将BMP图像转换为[R]中的矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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