Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.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 on rails 如何比较rails3中的两个图像_Ruby On Rails_Ruby On Rails 3_Image_Comparison - Fatal编程技术网

Ruby on rails 如何比较rails3中的两个图像

Ruby on rails 如何比较rails3中的两个图像,ruby-on-rails,ruby-on-rails-3,image,comparison,Ruby On Rails,Ruby On Rails 3,Image,Comparison,如何将本地目录中的图像与下载的图像进行比较。比较应基于图像内容和大小 如何在ruby中做到这一点?有几种方法可以做到这一点 1) 我认为最好的方法是使用Rmagick签名(感谢): 或 2) 您还可以使用光栅图形库(): require 'RMagick' new_image = Magick::Image.read(new_photo)[0] ## new_photo = "/your/dir/file.jpg" expected_image = Magick::Image.r

如何将本地目录中的图像与下载的图像进行比较。比较应基于图像内容和大小


如何在ruby中做到这一点?

有几种方法可以做到这一点

1) 我认为最好的方法是使用Rmagick签名(感谢):

2) 您还可以使用光栅图形库():

require 'RMagick'

new_image = Magick::Image.read(new_photo)[0]        ## new_photo = "/your/dir/file.jpg"
expected_image = Magick::Image.read(expected_photo)[0]

new_image.signature.should eql expected_image.signature 
diff_img, diff_metric  = img1[0].compare_channel( img2[0], Magick::MeanSquaredErrorMetric )
require 'raster_graphics'

class RGBColour

 # the difference between two colours
 def -(a_colour)
   (@red - a_colour.red).abs +
   (@green - a_colour.green).abs +
   (@blue - a_colour.blue).abs
 end
end

class Pixmap

# the difference between two images
  def -(a_pixmap)
    if @width != a_pixmap.width or @height != a_pixmap.height
      raise ArgumentError, "can't compare images with different sizes"
    end
    sum = 0
    each_pixel {|x,y| sum += self[x,y] - a_pixmap[x,y]}
    Float(sum) / (@width * @height * 255 * 3)
  end
end

lenna50 = Pixmap.open_from_jpeg('Lenna50.jpg')
lenna100 = Pixmap.open_from_jpeg('Lenna100.jpg')

puts "difference: %.5f%%" % (100.0 * (lenna50 - lenna100))

#=>:
#difference: 1.62559%