Module: ImageMatch
- Defined in:
- lib/image_match.rb,
lib/image_match/version.rb
Constant Summary collapse
- VERSION =
"0.0.2"
Instance Method Summary collapse
-
#compare_surf_descriptors(d1, d2, best, length) ⇒ Object
Private functions ====================================================================.
- #find_pairs(template_keypoints, template_descriptors, scene_keypoints, scene_descriptors) ⇒ Object
-
#fuzzy_match_template(scene_filename, template_filename, is_output = false) ⇒ Boolean
Try to find 2nd input image in 1st input image.
- #locate_planar_template(template_keypoints, template_descriptors, scene_keypoints, scene_descriptors, src_corners) ⇒ Object
- #naive_nearest_neighbor(vec, laplacian, model_keypoints, model_descriptors) ⇒ Object
-
#perfect_match(image1_filename, image2_filename, limit_similarity = 0.9) ⇒ Boolean
Calculate matching score of 1st input image and 2nd input image required the sizes are same.
-
#perfect_match_template(scene_filename, template_filename, limit_similarity = 0.9, is_output = false) ⇒ Boolean
Calculate matching score of 1st input image and 2nd input image.
Instance Method Details
#compare_surf_descriptors(d1, d2, best, length) ⇒ Object
Private functions
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/image_match.rb', line 11 def compare_surf_descriptors(d1, d2, best, length) raise ArgumentError unless (length % 4) == 0 total_cost = 0 0.step(length - 1, 4) { |i| t0 = d1[i] - d2[i] t1 = d1[i + 1] - d2[i + 1] t2 = d1[i + 2] - d2[i + 2] t3 = d1[i + 3] - d2[i + 3] total_cost += t0 * t0 + t1 * t1 + t2 * t2 + t3 * t3 break if total_cost > best } total_cost end |
#find_pairs(template_keypoints, template_descriptors, scene_keypoints, scene_descriptors) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/image_match.rb', line 49 def find_pairs(template_keypoints, template_descriptors, scene_keypoints, scene_descriptors) ptpairs = [] template_descriptors.size.times { |i| kp = template_keypoints[i] descriptor = template_descriptors[i] nearest_neighbor = naive_nearest_neighbor(descriptor, kp.laplacian, scene_keypoints, scene_descriptors) unless nearest_neighbor.nil? ptpairs << i ptpairs << nearest_neighbor end } ptpairs end |
#fuzzy_match_template(scene_filename, template_filename, is_output = false) ⇒ Boolean
Try to find 2nd input image in 1st input image. This function ignores color, size and shape details. (I mean this func checks whether almost same or clearly different)
Note that this function is useful I think, but sometimes it doesn’t output correct result which you want. It depends on input images. TODO : improve accuracy
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/image_match.rb', line 187 def fuzzy_match_template(scene_filename, template_filename, is_output=false) raise ArgumentError, 'File does not exists.' unless File.exist?(scene_filename) and File.exist?(template_filename) raise ArgumentError, 'is_output must be true or false.' unless is_output == false or is_output == true scene, template = nil, nil begin scene = IplImage.load(scene_filename, CV_LOAD_IMAGE_GRAYSCALE) template = IplImage.load(template_filename, CV_LOAD_IMAGE_GRAYSCALE) rescue raise RuntimeError, 'Couldn\'t read image files correctly' return false end return false unless scene.width >= template.width and scene.height >= template.height param = CvSURFParams.new(1500) template_keypoints, template_descriptors = template.extract_surf(param) scene_keypoints, scene_descriptors = scene.extract_surf(param) src_corners = [CvPoint.new(0, 0), CvPoint.new(template.width, 0), CvPoint.new(template.width, template.height), CvPoint.new(0, template.height)] dst_corners = locate_planar_template(template_keypoints, template_descriptors, scene_keypoints, scene_descriptors, src_corners) if is_output correspond = IplImage.new(scene.width, template.height + scene.height, CV_8U, 1); correspond.set_roi(CvRect.new(0, 0, template.width, template.height)) template.copy(correspond) correspond.set_roi(CvRect.new(0, template.height, scene.width, scene.height)) scene.copy(correspond) correspond.reset_roi correspond = correspond.GRAY2BGR if dst_corners 4.times { |i| r1 = dst_corners[i % 4] r2 = dst_corners[(i + 1) % 4] correspond.line!(CvPoint.new(r1.x, r1.y + template.height), CvPoint.new(r2.x, r2.y + template.height), :color => CvColor::Red, :thickness => 2, :line_type => :aa) } end ptpairs = find_pairs(template_keypoints, template_descriptors, scene_keypoints, scene_descriptors) 0.step(ptpairs.size - 1, 2) { |i| r1 = template_keypoints[ptpairs[i]] r2 = scene_keypoints[ptpairs[i + 1]] correspond.line!(r1.pt, CvPoint.new(r2.pt.x, r2.pt.y + template.height), :color => CvColor::Red, :line_type => :aa) } correspond.save_image(Time.now.to_i.to_s + "_match_result.png") end return (dst_corners ? true : false) end |
#locate_planar_template(template_keypoints, template_descriptors, scene_keypoints, scene_descriptors, src_corners) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/image_match.rb', line 63 def locate_planar_template(template_keypoints, template_descriptors, scene_keypoints, scene_descriptors, src_corners) ptpairs = find_pairs(template_keypoints, template_descriptors, scene_keypoints, scene_descriptors) n = ptpairs.size / 2 return nil if n < 4 pt1 = [] pt2 = [] n.times { |i| pt1 << template_keypoints[ptpairs[i * 2]].pt pt2 << scene_keypoints[ptpairs[i * 2 + 1]].pt } _pt1 = CvMat.new(1, n, CV_32F, 2) _pt2 = CvMat.new(1, n, CV_32F, 2) _pt1.set_data(pt1) _pt2.set_data(pt2) h = CvMat.find_homography(_pt1, _pt2, :ransac, 5) dst_corners = [] 4.times { |i| x = src_corners[i].x y = src_corners[i].y z = 1.0 / (h[6][0] * x + h[7][0] * y + h[8][0]) x = (h[0][0] * x + h[1][0] * y + h[2][0]) * z y = (h[3][0] * x + h[4][0] * y + h[5][0]) * z dst_corners << CvPoint.new(x.to_i, y.to_i) } dst_corners end |
#naive_nearest_neighbor(vec, laplacian, model_keypoints, model_descriptors) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/image_match.rb', line 25 def naive_nearest_neighbor(vec, laplacian, model_keypoints, model_descriptors) length = model_descriptors[0].size neighbor = nil dist1 = 1e6 dist2 = 1e6 model_descriptors.size.times { |i| kp = model_keypoints[i] mvec = model_descriptors[i] next if laplacian != kp.laplacian d = compare_surf_descriptors(vec, mvec, dist2, length) if d < dist1 dist2 = dist1 dist1 = d neighbor = i elsif d < dist2 dist2 = d end } return (dist1 < 0.6 * dist2) ? neighbor : nil end |
#perfect_match(image1_filename, image2_filename, limit_similarity = 0.9) ⇒ Boolean
Calculate matching score of 1st input image and 2nd input image required the sizes are same.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/image_match.rb', line 110 def perfect_match(image1_filename, image2_filename, limit_similarity=0.9) raise ArgumentError, 'File does not exists.' unless File.exist?(image1_filename) and File.exist?(image2_filename) raise ArgumentError, 'limit_similarity must be 0.1 - 1.0.' unless limit_similarity >= 0.1 and limit_similarity <= 1.0 image1, image2 = nil, nil begin image1 = IplImage.load(image1_filename) image2 = IplImage.load(image2_filename) rescue raise RuntimeError, 'Couldn\'t read image files correctly' return false end return false unless image1.width == image2.width and image1.height == image2.height return perfect_match_template(image1_filename, image2_filename, limit_similarity) end |
#perfect_match_template(scene_filename, template_filename, limit_similarity = 0.9, is_output = false) ⇒ Boolean
Calculate matching score of 1st input image and 2nd input image. The 2nd input image size must be smaller than 1st input image. This function is robust for brightness.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/image_match.rb', line 141 def perfect_match_template(scene_filename, template_filename, limit_similarity=0.9, is_output=false) raise ArgumentError, 'File does not exists.' unless File.exist?(scene_filename) and File.exist?(template_filename) raise ArgumentError, 'limit_similarity must be 0.1 - 1.0.' unless limit_similarity >= 0.1 and limit_similarity <= 1.0 raise ArgumentError, 'is_output must be true or false.' unless is_output == false or is_output == true scene, template = nil, nil begin scene = IplImage.load(scene_filename) template = IplImage.load(template_filename) rescue raise RuntimeError, 'Couldn\'t read image files correctly' return false end return false unless scene.width >= template.width and scene.height >= template.height result = scene.match_template(template, :ccoeff_normed) min_score, max_score, min_point, max_point = result.min_max_loc if is_output from = max_point to = CvPoint.new(from.x + template.width, from.y + template.height) scene.rectangle!(from, to, :color => CvColor::Red, :thickness => 3) scene.save_image(Time.now.to_i.to_s + "_match_result.png") end return (max_score >= limit_similarity ? true : false) end |