2012-12-08 26 views
2

s.o可以幫助將此代碼轉換爲ruby-openvc嗎?如何:getThresholdedImage with ruby​​-opencv

原始 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 

回答

1

Actally,我不知道紅寶石所有,但似乎我找到問題的解決方案。 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 
*/ 

所以,你應該通過這種方式找到所有需要的紅寶石功能。祝你好運!

+0

謝謝。但我找不到'cvScalar(20,100,100)'的正確譯文//代碼updadet原始帖子 – fabian

+0

CvScalar.new(...) – ArtemStorozhuk

+0

明白了,是的! – fabian