similie 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.5
1
+ 0.2.6
data/ext/similie.c CHANGED
@@ -56,17 +56,14 @@ int popcount(uint64_t x) {
56
56
 
57
57
  uint64_t image_phash(IplImage *img) {
58
58
  int x, y;
59
- double avg;
60
- uint64_t phash = 0;
59
+ double avg = 0;
60
+ uint64_t phash = 0, phash_mask = 1;
61
61
 
62
- IplImage *small = cvCreateImage(cvSize(64, 64), img->depth, img->nChannels);
63
- IplImage *mono = cvCreateImage(cvSize(64, 64), img->depth, 1);
62
+ IplImage *small = cvCreateImage(cvSize(DCT_SIZE, DCT_SIZE), img->depth, img->nChannels);
63
+ IplImage *mono = cvCreateImage(cvSize(DCT_SIZE, DCT_SIZE), img->depth, 1);
64
64
 
65
65
  cvResize(img, small, CV_INTER_CUBIC);
66
- if (img->nChannels == 1)
67
- cvCopy(small, mono, 0);
68
- else
69
- cvCvtColor(small, mono, CV_RGB2GRAY);
66
+ img->nChannels == 1 ? cvCopy(small, mono, 0) : cvCvtColor(small, mono, CV_RGB2GRAY);
70
67
 
71
68
  CvMat *dct = cvCreateMat(DCT_SIZE, DCT_SIZE, CV_32FC1);
72
69
  for (x = 0; x < DCT_SIZE; x++) {
@@ -78,13 +75,18 @@ uint64_t image_phash(IplImage *img) {
78
75
  cvDCT(dct, dct, CV_DXT_ROWS);
79
76
  cvSet2D(dct, 0, 0, cvScalarAll(0));
80
77
 
81
- avg = (double)cvAvg(dct, 0).val[0] * 64.0 / 63.0;
78
+ for (y = 0; y < 8; y++) {
79
+ for (x = 0; x < 8; x++) {
80
+ avg += cvGet2D(dct, x, y).val[0];
81
+ }
82
+ }
83
+
84
+ avg /= 63.0;
82
85
 
83
- uint64_t mask = 1;
84
86
  for (x = 7; x >= 0; x--) {
85
87
  for (y = 7; y >= 0; y--) {
86
- if (cvGet2D(dct, x, y).val[0] > avg) phash |= mask;
87
- mask = mask << 1;
88
+ if (cvGet2D(dct, x, y).val[0] > avg) phash |= phash_mask;
89
+ phash_mask = phash_mask << 1;
88
90
  }
89
91
  }
90
92
 
data/test/test_basic.rb CHANGED
@@ -12,12 +12,12 @@ describe 'Similie image load' do
12
12
  it 'should hash image' do
13
13
  img = Similie.new(File.join($testdir, 'lena1.png'))
14
14
  assert img
15
- assert_equal 36205409308016768, img.hash
15
+ assert_equal 36170087496991428, img.hash
16
16
  end
17
17
 
18
18
  it 'should hash image using class method' do
19
19
  hash = Similie.phash(File.join($testdir, 'lena1.png'))
20
20
  assert hash
21
- assert_equal 36205409308016768, hash
21
+ assert_equal 36170087496991428, hash
22
22
  end
23
23
  end
@@ -3,15 +3,16 @@ require_relative 'helper'
3
3
  describe 'Similie image distance' do
4
4
  it 'should work for similar images' do
5
5
  images = (1..5).map {|n| Similie.new(File.join($testdir, 'lena%d.png' % n ))}
6
+ images.unshift nil
6
7
 
7
- assert_equal 2, images[0].distance(images[1])
8
- assert_equal 3, images[1].distance(images[2])
9
- assert_equal 2, images[2].distance(images[3])
10
- assert_equal 3, images[0].distance(images[3])
11
- assert_equal 18, images[3].distance(images[4])
8
+ assert_equal 2, images[1].distance(images[2])
9
+ assert_equal 9, images[2].distance(images[3])
10
+ assert_equal 8, images[3].distance(images[4])
11
+ assert_equal 1, images[1].distance(images[4])
12
+ assert_equal 12, images[1].distance(images[5])
12
13
  end
13
14
 
14
15
  it 'should work using the singleton method' do
15
- assert_equal 18, Similie.distance(File.join($testdir, 'lena4.png'), File.join($testdir, 'lena5.png'))
16
+ assert_equal 12, Similie.distance(File.join($testdir, 'lena1.png'), File.join($testdir, 'lena5.png'))
16
17
  end
17
18
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 5
9
- version: 0.2.5
8
+ - 6
9
+ version: 0.2.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Bharanee Rathna
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-06-07 00:00:00 +10:00
17
+ date: 2011-06-08 00:00:00 +10:00
18
18
  default_executable:
19
19
  dependencies: []
20
20