【问题标题】:classify a large collection of image files对大量图像文件进行分类
【发布时间】:2019-10-06 09:39:22
【问题描述】:

我有一本书的大量图像文件集合,出版商想要一个列表,其中文件按“类型”(灰度图、黑白半色调图像、彩色图像、线条图等)分类。这通常是一个难题,但也许我可以使用图像处理工具自动完成其中的一些工作,例如带有 R magick 包的 ImageMagick。

我认为 ImageMagick 是正确的工具,但我真的不知道如何将它用于此目的。

我所拥有的只是无花果编号和文件名的列表:

1.1 ch01-intro/fig/alcohol-risk.jpg
1.2 ch01-intro/fig/measels.png
1.3 ch01-intro/fig/numbers.png
1.4 ch01-intro/fig/Lascaux-bull-chamber.jpg
...

有人可以帮我入门吗?

编辑:正如最初所说,这可能是一个框架错误或过度拱形的问题。我认为 ImageMagick identify 或 R magick::image_info() 函数可能会有所帮助,所以最初的问题可能应该是:“如何从 [R] 中的文件列表中提取图像信息”。我可以单独提出这个,如果没有被问到的话。

在这方面的初步尝试为我的第一张图片提供了以下信息,

library(magick)

# initialize an empty array to hold the results of `image_info`
figinfo <- data.frame(
  format=character(),
  width=numeric(),
  height=numeric(),
  colorspace=character(),
  matte=logical(),
  filesize=numeric(),
  density=character(), stringsAsFactors = FALSE
)

for (i in seq_along(files)) {
  img <- image_read(files[i])
  info <- image_info(img)
  figinfo[i,] <- info
}

我明白了:

> figinfo
  format width height colorspace matte filesize density
1   JPEG   661    733       sRGB FALSE    41884   72x72
2    PNG   838    591       sRGB  TRUE    98276   38x38
3    PNG   990    721       sRGB  TRUE   427253   38x38
4   JPEG   798    219       sRGB FALSE    99845 300x300

我的结论是,这对于回答我提出的关于如何对这些图像进行分类的问题没有多大帮助。

Edit2 在结束这个问题之前,直接使用 ImageMagick identify 的建议很有帮助。 https://imagemagick.org/script/escape.php 特别是,%[type] 更接近 我需要的。这在magick::image_info() 中没有公开,所以我可能不得不编写一个shell 脚本或循环调用system()

为了记录,这里是我如何直接使用identify提取这些图像文件的相关属性。

# Get image characteristics via ImageMagick identify
# from: https://imagemagick.org/script/escape.php
#
# -format elements:
# %m image file format
# %f filename
# %[type] image type
# %k number of unique colors
# %h image height in pixels
# %r image class and colorspace

identify -format "%m,%f,%[type],%r,%k,%hx%w" imagefile

>identify -format "%m,%f,%[type],%r,%k,%hx%w" Quipu.png
PNG,Quipu.png,GrayscaleAlpha,DirectClass Gray Matte,16,449x299

%[type] 属性将我带向我想要的方向。

【问题讨论】:

  • 我不知道 R 或 Rmagick。但是在命令行 Imagemagick 中,您可以使用颜色空间和类型的字符串格式来获取您想要的大部分信息。 convert image -format "%[colorspace]" info: 例如。见imagemagick.org/script/escape.php

标签: r image-processing imagemagick magick-r-package


【解决方案1】:

结束这个问题:

在 R 上下文中,我成功地将 system(, intern=TRUE) 用于此任务,如下所示,并进行了一些手动修复

# Use identify directly via system()

# function to run identify for one file
get_info <- function(file) {
  cmd <- 'identify -quiet -format "%f,%m,%[type],%r,%h,%w,%x,%y"'
  info <- system(paste(cmd, file), intern=TRUE)
  unlist(strsplit(info, ","))
}

# This doesn't cause coercion to numeric
figinfo <- data.frame(
  filename=character(),
  format=character(),
  type=character(),
  class=character(),
  height=numeric(),
  width=numeric(),
  xres=numeric(),
  yres=numeric(),
  stringsAsFactors = FALSE
)


for (i in seq_along(files)) {
  info <- get_info(files[i])
  info[4] <- sub("DirectClass ", "", info[4])
 figinfo[i,] <- info
}

figinfo$height <- as.numeric(figinfo$height)
figinfo$width <- as.numeric(figinfo$width)
figinfo$xres=round(as.numeric(figinfo$xres))
figinfo$yres=round(as.numeric(figinfo$yres))

那么我或多或少有我想要的:

> str(figinfo)
'data.frame':   161 obs. of  8 variables:
 $ filename: chr  "mileyears4.png" "alcohol-risk.jpg" "measels.png" "numbers.png" ...
 $ format  : chr  "PNG" "JPEG" "PNG" "PNG" ...
 $ type    : chr  "Palette" "TrueColor" "TrueColorAlpha" "TrueColorAlpha" ...
 $ class   : chr  "PseudoClass sRGB " "sRGB " "sRGB Matte" "sRGB Matte" ...
 $ height  : num  500 733 591 721 219 ...
 $ width   : num  720 661 838 990 798 ...
 $ xres    : num  72 72 38 38 300 38 300 38 28 38 ...
 $ yres    : num  72 72 38 38 300 38 300 38 28 38 ...
> 

【讨论】:

    猜你喜欢
    • 2011-05-11
    • 1970-01-01
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多