2

可以帮助将此代码转换为 ruby​​-openvc 吗?

原文 http://aishack.in/tutorials/tracking-colored-objects-in-opencv/

IplImage* GetThresholdedImage(IplImage* img)
{
  #Convert the image into an HSV image
  IplImage* imgHSV = cvCreateImage(cvGetSize(img), 8, 3);
  cvCvtColor(img, imgHSV, CV_BGR2HSV);

  #create a new image that will hold the threholded image (which will be returned).
  IplImage* imgThreshed = cvCreateImage(cvGetSize(img), 8, 1);

  #Now we do the actual thresholding:
  cvInRangeS(imgHSV, cvScalar(20, 100, 100), cvScalar(30, 255, 255), imgThreshed)

  cvReleaseImage(&imgHSV);
  return imgThreshed;
}

翻译

    def getThresholdedImage2 (img)
  #blur the source image to reduce color noise
  img = img.smooth(CV_GAUSSIAN, 7, 7)

  #convert the image to hsv(Hue, Saturation, Value) so its
  #easier to determine the color to track(hue)  imgHSV = IplImage.new(img.width, img.height, 8, 3);
  imgHSV = img.BGR2HSV

  #create a new image that will hold the threholded image (which will be returned).
  imgThreshed = IplImage.new(img.width, img.height, 8, 1);

  #Now we do the actual thresholding:
  imgThreshed = imgHSV.in_range(CvScalar.new(20, 100, 100), CvScalar.new(30, 255, 255));


  return imgThreshed
end
4

1 回答 1

1

实际上,我根本不了解红宝石,但似乎我找到了解决您问题的方法。Ruby-OpenCV似乎只是一个库包装器。

例如,如果您想找到cvInRangeS函数的类似物,您应该执行以下操作。

通过在源文件中搜索,我发现ext/opencv/cvmat.h包含以下内容:

VALUE rb_range(VALUE self, VALUE start, VALUE end);
VALUE rb_range_bang(VALUE self, VALUE start, VALUE end);

cpp文件中有描述:

/*
 * call-seq:
 *   in_range(<i>min, max</i>) -> cvmat
 *
 * Check that element lie between two object.
 * <i>min</i> and <i>max</i> should be CvMat that have same size and type, or CvScalar.
 * Return new matrix performed per-element,
 *  dst(I) = within the range ? 0xFF : 0
 */

所以你应该通过这种方式找到所有需要的 ruby​​ 函数。祝你好运!

于 2012-12-08T22:46:17.307 回答