代码之家  ›  专栏  ›  技术社区  ›  Darshan Patel

ImageMagick命令慢度问题

  •  2
  • Darshan Patel  · 技术社区  · 6 年前

    我在我的web应用程序中使用ImageMagick进行一些图像处理(在Ubuntu操作系统中使用Java作为后端),从图像中识别CMYK颜色。

    enter image description here

    整个图像的步骤如下:

    首先,我在CMYK的灰度中分离四个图像:

    convert IMG_1732.jpg -colorspace CMYK -negate -separate IMG_1732-sep.jpg
    

    四幅图片的结果如下:

    图1:

    enter image description here

    图2:

    enter image description here

    图3:

    enter image description here

    enter image description here

    然后我执行以下命令将每个图像从灰度转换为C、M、Y、K色图像:

    convert IMG_1732-sep-0.jpg -channel C -combine IMG_1732-sep-00.jpg
    

    结果1:

    enter image description here

    convert IMG_1732-sep-1.jpg -channel M -combine IMG_1732-sep-11.jpg
    

    结果2:

    enter image description here

    convert IMG_1732-sep-2.jpg -channel Y -combine IMG_1732-sep-22.jpg
    

    结果3:

    enter image description here

    convert IMG_1732-sep-3.jpg -channel K -combine IMG_1732-sep-33.jpg
    

    结果4:

    enter image description here

    问题是,它占用了太多时间,图像大小大约超过2-3 MB。同时,也会消耗更多的RAM和CPU。

    • 如何优化它?ImageMagick或 还有别的办法吗?
    3 回复  |  直到 6 年前
        1
  •  3
  •   Darshan Patel    6 年前

    如果您的号码4是与CMYK分离的频道,则不可能有彩色图像。所有单通道将为灰度。我也不能复制你的确切颜色。所以要么是图像不同或改变了,要么你没有告诉我们你的所有处理步骤。

    与marksetchell的命令类似,我可以在一个命令行中编写您的命令,处理时间大约为0.63秒。

    time convert image.jpg -colorspace cmyk -negate -separate \
    \( -clone 0 -channel C -combine +write C.jpg \) \
    \( -clone 1 -channel M -combine +write M.jpg \) \
    \( -clone 2 -channel Y -combine +write Y.jpg \) \
    \( -clone 3 -channel K -combine +write K.jpg \) \
    null:
    
    0.626s
    


    C.jpg公司 enter image description here

    enter image description here

    enter image description here

    K.jpg公司 enter image description here

    如果我包括-normalize以增加对比度,我可以更接近您的结果,但处理时间将上升到大约0.83秒。

    convert image.jpg -colorspace cmyk -negate -separate -normalize \
    \( -clone 0 -channel C -combine +write C.jpg \) \
    \( -clone 1 -channel M -combine +write M.jpg \) \
    \( -clone 2 -channel Y -combine +write Y.jpg \) \
    \( -clone 3 -channel K -combine +write K.jpg \) \
    null:
    

    enter image description here

    enter image description here

    enter image description here

    enter image description here

    convert image.jpg -colorspace CMYK -format "%[fx:mean.c], %[fx:mean.m], %[fx:mean.y], %[fx:mean.k]\n" info: 
    

    0.0606194, 0.0598201, 0.0300933, 0.0748128

    这些值是0到1之间的分数。对于百分比,将每个平均值乘以100。请注意结尾的冒号是必需的。

        2
  •  3
  •   Mark Setchell    6 年前

    我不确定这一点,但已经做了一些工作,并希望与大家分享。一、 或者我的一个朋友可能会想出更好的东西,当我们更好地了解,所以我会显示我到目前为止。

    以下是我所拥有的:

    convert image.jpg -colorspace cmyk -separate -write MPR:orig                                             \
       +delete -colorspace gray -threshold 101% -write MPR:blackx3 -delete 0,1,2                             \
       \( MPR:orig -delete 1,2,3 MPR:blackx3           -set colorspace CMYK -combine -write C.jpg +delete \) \
       \( MPR:orig -delete 0,2,3 MPR:blackx3 -swap 0,1 -set colorspace CMYK -combine -write M.jpg +delete \) \
       \( MPR:orig -delete 0,1,3 MPR:blackx3 -swap 0,2 -set colorspace CMYK -combine -write Y.jpg +delete \) \
          MPR:orig -delete 0,1,2 K.jpg
    

    我确信这有点令人生畏,所以我会一次解释每一行。。。

    第1行: "orig" .

    删除堆栈上的最后一个通道,即K,留下C、M和Y。使所有这3个图像完全变黑,并保存在名为 "blackx3" 所以我有三个空的黑色通道供以后使用。

    第3行: "C.jpg" . 清理干净。

    第4行: 从RAM重新加载CMYK,删除CYK,留下M,然后加载3个空的黑色通道。我们现在有M,黑,黑,黑在我们的堆栈上。交换顺序为黑色,M,黑色,黑色。告诉IM我们堆栈上的4个图像是CMYK通道,并将它们组合起来编写为 "M.jpg" . 清理干净。

    第5行: 从RAM重新加载CMYK,删除CMK,留下Y,然后加载3个空的黑色通道。我们现在有Y,黑,黑,黑在我们的堆栈上。交换顺序为黑色,黑色,Y,黑色。告诉IM我们堆栈上的4个图像是CMYK通道,并将它们组合起来编写为 "Y.jpg" . 清理干净。

    从RAM加载CMYK。删除CMY,保留黑色,另存为 "K.jpg" .

    因此:

    enter image description here enter image description here

    enter image description here enter image description here

        3
  •  1
  •   fmw42    6 年前

    如果您想找到RGB输入中任何像素的CMYK颜色等效值,则可以执行以下操作,例如,对于像素(10,10)。这里我用的是ImageMagick特别版的rose:image。但你可以把任何真实的图像(图像.后缀)代替玫瑰:。对于真实图像,不要包含最后一个冒号。

    convert rose: -colorspace CMYK -format "%[pixel:u.p{10,10}]\n" info:
    
    cmyk(0,28,53,183)
    


    如果希望整个图像或其任何部分显示CMYK值,请使用txt:output。例如,这里是rose:image的左上2x2部分。

    convert rose:[2x2+0+0] -colorspace CMYK txt:
    
    # ImageMagick pixel enumeration: 2,2,65535,cmyk
    0,0: (0,1365,4096,53199)  #000510CF  cmyk(0,5,16,207)
    1,0: (0,2621,5243,52685)  #000A14CD  cmyk(0,10,20,205)
    0,1: (0,1394,4183,53456)  #000510D0  cmyk(0,5,16,208)
    1,1: (0,1365,4096,53199)  #000510CF  cmyk(0,5,16,207)
    

    如果要将每个C、M、Y、K通道分离为单独的图像,则第一个命令仅执行以下操作:

    convert IMG_1732.jpg -colorspace CMYK -negate -separate IMG_1732-sep.jpg
    


    每个结果图像IMG_1732-sep-0.jpg、IMG_1732-sep-1.jpg、IMG_1732-sep-2.jpg、IMG_1732-sep-3.jpg表示相应的否定(反转)C、M、Y、K通道。无需进一步处理。ImageMagick identify会说它们是灰度的,但是这些值表示每个图像的反转C、M、Y、K。