Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 使用RMagick查找颜色位置_Ruby_Image Processing_Imagemagick_Rmagick - Fatal编程技术网

Ruby 使用RMagick查找颜色位置

Ruby 使用RMagick查找颜色位置,ruby,image-processing,imagemagick,rmagick,Ruby,Image Processing,Imagemagick,Rmagick,我有一张有很多不重叠的彩色矩形的图片。每个矩形都是唯一的颜色,我提前知道颜色。(奇怪的情况,我知道。)我正试图找到每个矩形的像素位置和大小,我需要这个尽可能快。我可以用RMagick或类似的库做些什么有趣的技巧来让这比遍历每个像素更容易 我目前的计划大致如下: for each pixel (moving left-to-right, top-to-bottom): if pixel color's in our list and we haven't seen it yet: sa

我有一张有很多不重叠的彩色矩形的图片。每个矩形都是唯一的颜色,我提前知道颜色。(奇怪的情况,我知道。)我正试图找到每个矩形的像素位置和大小,我需要这个尽可能快。我可以用RMagick或类似的库做些什么有趣的技巧来让这比遍历每个像素更容易

我目前的计划大致如下:

for each pixel (moving left-to-right, top-to-bottom):
  if pixel color's in our list and we haven't seen it yet:
    save pixel location as starting location for that color
  else if pixel color's in our list and we've already seen it:
    save pixel location as ending location for that color
(是的,我们可以优化和跳过某些像素区域,如果我们知道它们在一个矩形中。)在循环结束时,我们应该有每个矩形的第一个和最后一个像素,我们可以使用它们来推断矩形的尺寸。但我觉得这有点难看


我能做得更好吗?

只有当有一个最小的矩形大小,理想情况下至少是3x3,这样额外的复杂性才会得到回报时,这个答案才有效

假设最小矩形大小为(m,n),使用该步长运行建议的算法以获得近似的起点和终点,然后通过逐像素(在两个L形路径中)检查两个相对角的位置来优化这些近似值。您可以显示,与扫描整个图像相比,这将始终检查较少数量的像素

for each pixel (moving left-to-right step m, top-to-bottom step n):
  if pixel color's in our list and we haven't seen it yet:
    save pixel location as rough starting location (xs,ys) for that color
    save pixel location as rough ending location (xe,ye) for that color
  else if pixel color's in our list and we've already seen it:
    save pixel location as rough ending location (xe,ye) for that color
然后优化位置

for each color
  with starting location (xs , ys)
    while color at (xs, ys - 1) is same, update ys = ys - 1
    while color at (xs - 1, ys) is same, update xs = xs - 1
  with ending location (xe , ye)
    while color at (xe, ye + 1) is same, update ye = ye + 1
    while color at (xe + 1, ye) is same, update xe = xe + 1
如果最小尺寸为3x3,则需要查找20个矩形,图像为100x100:

  • 一次完整扫描将读取10000像素
  • 优化后的扫描将读取33x33像素,然后通过读取20x10(平均值)来细化20个矩形中的每一个。总共1289像素

您对最小矩形尺寸有限制吗?“矩形”可以是1x1像素吗?若它们必须更大,你们可以利用这些知识进一步优化。步长的好主意!我很喜欢这个。