image_svd 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8c7595bcaa0980e88306b17c334eb96e97cc9d03
4
+ data.tar.gz: e5fd5e67bd0e4ae9138d736640aff240ea6d1e28
5
+ SHA512:
6
+ metadata.gz: c5c5d15ea75980e4c0a785695e9c84909b6c8ae6796b18d0ee7484c76961e72cc8ded4c6b6261058058a125b88a3054a290652c35076a97739ce3c03ca7e5eef
7
+ data.tar.gz: 065244d4de86a95ea5e8cb6da141f177b23d94480a817f6a6a8b7eaa9e3ec0839284ce2dae41684efb1cb9a4d1ee573b2aced5cbc7abf3926e6c43e246b6cdc8
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in image_svd.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ilya Kavalerov
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # ImageSvd
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'image_svd'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install image_svd
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/image_svd/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler/gem_tasks'
5
+ require 'rspec/core/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new
8
+
9
+ task test: :spec
10
+ task default: :spec
data/bin/image_svd ADDED
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ if RUBY_VERSION >= '1.9.2'
5
+ $LOAD_PATH.unshift(File.dirname(File.realpath(__FILE__)) + '/../lib')
6
+
7
+ # test for presence of imagemagick dependency
8
+ imagemagick_location = `which convert`
9
+ if imagemagick_location == ''
10
+ puts 'Image Svd requires the cmd line utility imagemagick.\
11
+ Install it with: brew install imagemagick'
12
+ exit(-1)
13
+ end
14
+
15
+ require 'image_svd'
16
+ require 'benchmark'
17
+ require 'trollop'
18
+
19
+ cli = ImageSvd::CLI.new
20
+
21
+ time = Benchmark.realtime do
22
+ opts = Trollop.options(&ImageSvd::Options.get)
23
+ cli.run(opts)
24
+ end
25
+
26
+ puts "Image svd finished in #{time} seconds"
27
+ exit(0)
28
+ else
29
+ puts 'Image svd only supports Ruby versions 1.9.2 and above'
30
+ exit(-1)
31
+ end
data/image_svd.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'image_svd/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'image_svd'
8
+ spec.version = ImageSvd::VERSION
9
+ spec.authors = ['Ilya Kavalerov']
10
+ spec.email = ['ilya@artsymail.com']
11
+ spec.summary = 'Compress images with Linear Algebra.'
12
+ spec.description = <<-EOF
13
+ Break down grayscale image matricies into their
14
+ singular value decomposition. Space saving, but CPU intensive.
15
+ EOF
16
+ spec.homepage = 'https://github.com/ilyakava/image_svd'
17
+ spec.license = 'MIT'
18
+
19
+ spec.files = `git ls-files -z`.split("\x0")
20
+ spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
21
+ spec.test_files = spec.files.grep(/^(test|spec|features)\//)
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_runtime_dependency 'pnm'
25
+ spec.add_runtime_dependency 'trollop', '~> 2.0'
26
+ spec.add_development_dependency 'bundler', '~> 1.5'
27
+ spec.add_development_dependency 'rake'
28
+ spec.add_development_dependency 'pry'
29
+ spec.add_development_dependency 'rspec'
30
+ end
data/lib/image_svd.rb ADDED
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+
3
+ require 'image_svd/version'
4
+ require 'image_svd/cli'
5
+ require 'image_svd/image_matrix'
@@ -0,0 +1,74 @@
1
+ # encoding: utf-8
2
+
3
+ module ImageSvd
4
+ # This class is responsible for handling the command line interface
5
+ class CLI
6
+ # The entry point for the application logic
7
+ # rubocop:disable MethodLength
8
+ def run(opts)
9
+ if opts[:read] == true
10
+ app = ImageSvd::ImageMatrix.new_from_svd_savefile(opts[:input_file])
11
+ app.to_image(opts[:output_name])
12
+ else
13
+ app = ImageSvd::ImageMatrix.new(opts[:num_singular_values])
14
+ app.read_image(opts[:input_file])
15
+ if opts[:archive] == true
16
+ app.save_svd(opts[:output_name])
17
+ elsif opts[:convert] == true
18
+ app.to_image(opts[:output_name])
19
+ end
20
+ end
21
+ end
22
+ end
23
+ # rubocop:enable MethodLength
24
+
25
+ # This module holds custom behavior for dealing with the gem trollop
26
+ module Options
27
+ # rubocop:disable MethodLength
28
+ def self.get
29
+ proc do
30
+ version "Image Svd #{ImageSvd::VERSION} (c) 2014 Ilya Kavalerov"
31
+ banner <<-EOS
32
+ Image Svd is a utilty that performs Singular Value Decomposition
33
+ on a grayscale image. It can be useful for compressing images, or
34
+ creating interesting visual effects to distort images when
35
+ compression is set very high.
36
+
37
+ Usage:
38
+ image_svd [options]
39
+ where [options] are:
40
+ EOS
41
+ opt :input_file,
42
+ 'An input file (Preferably a jpg).',
43
+ type: :io,
44
+ required: true
45
+ opt :num_singular_values,
46
+ 'The number of singular values to keep for an image. Lower'\
47
+ ' numbers mean lossier compression; smaller files and more'\
48
+ ' distorted images.',
49
+ default: 50,
50
+ short: '-n'
51
+ opt :output_name,
52
+ 'A path/name for an output file (Extension will be ignored).'\
53
+ ' If no path/name is provided, a file will be written in'\
54
+ ' the current directory',
55
+ default: 'svd_image_output',
56
+ short: '-o'
57
+ opt :convert,
58
+ 'Convert the input file now.',
59
+ default: true,
60
+ short: '-c'
61
+ opt :archive,
62
+ 'Save the Image Svd archive without converting the input image.',
63
+ default: false,
64
+ short: '-a'
65
+ opt :read,
66
+ 'Read an Image Svd archive (*.svdim) and output the image'\
67
+ ' it contains.',
68
+ default: false,
69
+ short: '-r'
70
+ end
71
+ end
72
+ # rubocop:enable MethodLength
73
+ end
74
+ end
@@ -0,0 +1,127 @@
1
+ require 'matrix'
2
+ require 'json'
3
+ require 'pnm'
4
+
5
+ module ImageSvd
6
+ # This class is responsible for almost everything :(
7
+ # Reading an image or archive to a matrix
8
+ # Saving a matrix to an image
9
+ # Performing Singular Value Decomposition on a matrix
10
+ class ImageMatrix
11
+ # rubocop:disable SymbolName
12
+ # rubocop:disable VariableName
13
+ attr_reader :num_singular_values
14
+ attr_accessor :sigma_vTs, :us, :m, :n
15
+
16
+ def initialize(num_singular_values)
17
+ @num_singular_values = num_singular_values
18
+ end
19
+
20
+ def read_image(image_path)
21
+ puts 'Reading image and converting to matrix...'
22
+ intermediate = extension_swap(image_path.path, 'pgm')
23
+ %x(convert #{image_path.path} #{intermediate})
24
+ image = PNM.read intermediate
25
+ decompose Matrix[*image.pixels]
26
+ %x(rm #{intermediate})
27
+ self
28
+ end
29
+
30
+ # @todo abstract this to another class
31
+ # always place the new extension, even if there is nothing to swap out
32
+ def extension_swap(path, new_ext, suffix = '')
33
+ head = path.gsub(/\..{1,5}$/, '')
34
+ "#{head}#{suffix}.#{new_ext}"
35
+ end
36
+
37
+ # The most time consuming method
38
+ # Launches the decomposition and saves the two lists
39
+ # of vectors needed to reconstruct the image
40
+ # rubocop:disable MethodLength
41
+ def decompose(m_A)
42
+ m_AT = m_A.transpose
43
+ @m, @n = m_A.to_a.length, m_A.to_a.first.length
44
+ m_ATA = m_AT * m_A
45
+ # linear regression from several images over 200px wide
46
+ eta = (0 < (t = (0.0003541 * (@m * @n) - 10.541)) ? t : 0)
47
+ puts "Searching for eigenvalues... Estimated Time: #{eta.floor} seconds"
48
+ dcmp = Matrix::EigenvalueDecomposition.new(m_ATA)
49
+ evs = dcmp.eigenvalues
50
+ # eigenvectors are already normalized and in same order as eigenvalues
51
+ sorted_eigenvectors = dcmp.eigenvectors.each_with_index
52
+ .sort_by { |_v, i| -evs[i] }
53
+ .map { |v, _idx| v }
54
+ both = (0...@num_singular_values).map do |idx|
55
+ u = sorted_eigenvectors[idx]
56
+ sigma_vT = (m_A * u).covector
57
+ [u, sigma_vT]
58
+ end
59
+ @sigma_vTs = both.map { |p| p.last }
60
+ @us = both.map { |p| p.first }
61
+ end
62
+ # rubocop:enable MethodLength
63
+
64
+ def reconstruct_matrix
65
+ zero_matrix = Matrix[*Array.new(@n) { Array.new(@m) { 0 } }]
66
+ (0...@num_singular_values).reduce(zero_matrix) do |acc, idx|
67
+ acc + (@us[idx] * @sigma_vTs[idx])
68
+ end.transpose
69
+ end
70
+
71
+ def to_image(path)
72
+ out_path = extension_swap(path, 'jpg')
73
+ intermediate = extension_swap(path, 'pgm', '_tmp_outfile')
74
+ cleansed_matrix = matrix_to_valid_pixels(reconstruct_matrix)
75
+ PNM::Image.new(cleansed_matrix).write(intermediate)
76
+ %x(convert #{intermediate} #{out_path})
77
+ %x(rm #{intermediate})
78
+ true
79
+ end
80
+
81
+ # conforms a matrix to pnm requirements for pixels: positive integers
82
+ # rubocop:disable MethodLength
83
+ def matrix_to_valid_pixels(matrix)
84
+ matrix.to_a.map do |row|
85
+ row.map do |number|
86
+ rounded = number.round
87
+ if rounded > 255
88
+ 255
89
+ elsif rounded < 0
90
+ 0
91
+ else
92
+ rounded
93
+ end
94
+ end
95
+ end
96
+ end
97
+ # rubocop:enable MethodLength
98
+
99
+ def save_svd(path)
100
+ out_path = extension_swap(path, 'svdim')
101
+ string = {
102
+ 'sigma_vTs' => @sigma_vTs.map(&:to_a),
103
+ 'us' => @us.map(&:to_a),
104
+ 'm' => @m,
105
+ 'n' => @n
106
+ }.to_json
107
+ File.open(out_path, 'w') do |f|
108
+ f.puts string
109
+ end
110
+ end
111
+
112
+ # @todo error handling code here
113
+ # @todo serialization is kind of silly as is
114
+ def self.new_from_svd_savefile(path)
115
+ h = JSON.parse(File.open(path, &:readline))
116
+ instance = new(h['sigma_vTs'].length)
117
+ instance.sigma_vTs = h['sigma_vTs']
118
+ .map { |arr| Vector[*arr.flatten].covector }
119
+ instance.us = h['us'].map { |arr| Vector[*arr.flatten] }
120
+ instance.n = h['n']
121
+ instance.m = h['m']
122
+ instance
123
+ end
124
+ # rubocop:enable SymbolName
125
+ # rubocop:enable VariableName
126
+ end
127
+ end
@@ -0,0 +1,4 @@
1
+ # This module holds the gem's code
2
+ module ImageSvd
3
+ VERSION = '0.0.1'
4
+ end
data/spec/cli_spec.rb ADDED
@@ -0,0 +1,58 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'CLI' do
6
+ describe 'integration spec' do
7
+ let(:cli) { ImageSvd::CLI.new }
8
+ let(:orig) { File.new('./spec/fixtures/2x2.jpg') }
9
+
10
+ it 'converts an image without too great errors' do
11
+ conv = './spec/fixtures/svd_image_output'
12
+ cli.run(
13
+ input_file: orig,
14
+ convert: true,
15
+ num_singular_values: 2,
16
+ output_name: conv
17
+ )
18
+ i = ImageSvd::ImageMatrix.new(2)
19
+ i.read_image(orig)
20
+ i2 = ImageSvd::ImageMatrix.new(2)
21
+ i2.read_image(File.new("#{conv}.jpg"))
22
+ diff_matrix = i.reconstruct_matrix - i2.reconstruct_matrix
23
+ diff_matrix.to_a.flatten.each do |diff_component|
24
+ diff_component.abs.should be < 5
25
+ end
26
+
27
+ # cleanup
28
+ %x(rm #{conv}.jpg)
29
+ end
30
+
31
+ it 'archives, reads, and converts an image without too great errors' do
32
+ conv = './spec/fixtures/svd_image_output'
33
+ # archive
34
+ cli.run(
35
+ input_file: orig,
36
+ archive: true,
37
+ num_singular_values: 2,
38
+ output_name: conv
39
+ )
40
+ # read archive and write and image
41
+ cli.run(
42
+ input_file: "#{conv}.svdim",
43
+ read: true,
44
+ output_name: "#{conv}_2"
45
+ )
46
+ i = ImageSvd::ImageMatrix.new(2)
47
+ i.read_image(orig)
48
+ i2 = ImageSvd::ImageMatrix.new(2)
49
+ i2.read_image(File.new("#{conv}_2.jpg"))
50
+ diff_matrix = i.reconstruct_matrix - i2.reconstruct_matrix
51
+ diff_matrix.to_a.flatten.each do |diff_component|
52
+ diff_component.abs.should be < 5
53
+ end
54
+ # cleanup
55
+ %x(rm #{conv}_2.jpg #{conv}.svdim)
56
+ end
57
+ end
58
+ end
Binary file
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe ImageSvd::ImageMatrix do
6
+ before :each do
7
+ @m = Matrix[[0, 1, 1], [1, 1, 0]]
8
+ end
9
+
10
+ it 'recovers a 2x3 matrix' do
11
+ i = ImageSvd::ImageMatrix.new(2)
12
+ i.decompose(@m)
13
+ rounded_matrix = Matrix[*i.matrix_to_valid_pixels(i.reconstruct_matrix)]
14
+ # due to numerical instability, even a 2x3 matrix needs to be rounded
15
+ @m.should eq(rounded_matrix)
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
4
+ require 'image_svd'
5
+ require 'pry'
6
+
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: image_svd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ilya Kavalerov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pnm
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: trollop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: |2
98
+ Break down grayscale image matricies into their
99
+ singular value decomposition. Space saving, but CPU intensive.
100
+ email:
101
+ - ilya@artsymail.com
102
+ executables:
103
+ - image_svd
104
+ extensions: []
105
+ extra_rdoc_files: []
106
+ files:
107
+ - ".gitignore"
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/image_svd
113
+ - image_svd.gemspec
114
+ - lib/image_svd.rb
115
+ - lib/image_svd/cli.rb
116
+ - lib/image_svd/image_matrix.rb
117
+ - lib/image_svd/version.rb
118
+ - spec/cli_spec.rb
119
+ - spec/fixtures/2x2.jpg
120
+ - spec/image_matrix_spec.rb
121
+ - spec/spec_helper.rb
122
+ homepage: https://github.com/ilyakava/image_svd
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.2.1
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Compress images with Linear Algebra.
146
+ test_files:
147
+ - spec/cli_spec.rb
148
+ - spec/fixtures/2x2.jpg
149
+ - spec/image_matrix_spec.rb
150
+ - spec/spec_helper.rb