View on GitHub
Basics
Feature Detection


Creating and accessing cv::Mat

 1    require 'ropencv'
 2    include OpenCV
 3    
 4    # basic initialize
 5    mat1 = cv::Mat.new
 6    mat2 = cv::Mat.new(3,4,cv::CV_64FC1)
 7    mat3 = cv::Mat.new [1,2,3,4,5]
 8    mat4 = cv::Mat.new [1,2,3,4,5],[6,7,8,9,10]   #CV_32SC1
 9    mat5 = cv::Mat.new [1.0,2,3,4,5],[6,7,8,9,10] #CV_64FC1
10    mat6 = cv::Mat.new(mat5)
11    
12    # initialize with value
13    mat7 = cv::Mat::ones(3,3,cv::CV_64FC1)
14    mat8 = cv::Mat::zeros(3,3,cv::CV_64FC1)
15    mat9 = cv::Mat::eye(3,3,cv::CV_64FC1)
16    
17    # initialize from vector
18    vec = Std::Vector.new(cv::Point2f)
19    vec << cv::Point2f.new(2,3)
20    mat10 = cv::Mat.new(vec)
21    
22    # basic access
23    puts mat7.to_a.inspect
24    puts mat7[1,1]
25    mat7[1,1] = 102
26    
27    # block access
28    sub = mat7.block(cv::Rect.new(0,0,2,2))
go up

Creating and accessing std::Vector

 1    require 'ropencv'
 2    include OpenCV
 3    
 4    # initialize vector of cv::Point
 5    # see documentation which vector types are defined
 6    vec1 = Std::Vector::Cv_Point.new
 7    vec2 = Std::Vector.new(cv::Point)
 8    vec3 = Std::Vector.new(cv::Point.new,cv::Point.new)
 9    
10    # initialize vector of cv::Point2f
11    vec4 = Std::Vector::Cv_Point2f.new
12    vec5 = Std::Vector.new(cv::Point2f)
13    vec6 = Std::Vector.new(cv::Point2f.new,cv::Point2f.new,cv::Point2f.new)
14    
15    # access
16    vec3.size
17    vec3 << cv::Point.new
18    vec3[2].x
19    vec3[2].y
go up

Loading and displaying images

1    require 'ropencv'
2    include OpenCV
3    
4    image = cv::Mat.new
5    image = cv::imread "logo.png"
6    cv::imshow "logo", image
7    cv::wait_key 1000
go up

Finding and displaying key points

 1    require 'ropencv'
 2    include OpenCV
 3    
 4    mat = cv::imread("logo.png")
 5    detector = cv::FeatureDetector::create("SURF")
 6    keypoints = Vector.new(cv::KeyPoint)
 7    detector.detect(mat,keypoints)
 8    
 9    puts "found #{keypoints.size} keypoints"
10    puts "first keypoint is at #{keypoints[0].pt.x}/#{keypoints[0].pt.y}"
11    
12    cv::draw_keypoints(mat,keypoints,mat)
13    cv::imshow("key_points",mat)
14    cv::wait_key(100)
go up

Matching Features

 1    require 'ropencv'
 2    include OpenCV
 3    
 4    mat1 = cv::imread("image1.png")
 5    mat2 = cv::imread("image2.png")
 6    
 7    detector = cv::FeatureDetector::create("SURF")
 8    extractor = cv::DescriptorExtractor::create("SURF")
 9    matcher = cv::DescriptorMatcher::create("BruteForce")
10    
11    features1 = Std::Vector.new(cv::KeyPoint)
12    features2 = Std::Vector.new(cv::KeyPoint)
13    detector.detect mat1,features1
14    detector.detect mat2,features2
15    
16    descriptor1 = cv::Mat.new
17    descriptor2 = cv::Mat.new
18    extractor.compute(mat1,features1,descriptor1)
19    extractor.compute(mat2,features2,descriptor2)
20    
21    matches = Std::Vector.new(cv::DMatch)
22    matcher.match(descriptor1,descriptor2,matches)
23    
24    result = cv::Mat.new
25    cv::draw_matches(mat1,features1,mat2,features2,matches,result)
26    cv::imshow("result",result)
27    cv::wait_key(1000)
go up