how to get sub image from cv::mat

i have tried following code, but it doesnt work:

  
  
cv::mat grayImg;  
// code change ofimage to cv::mat.  
//....  
ofImage foo;  
foo.setFromPixels(grayImg.data, grayImg.rows, grayImg.cols, OF_IMAGE_SCALE);  
// foo works fine for me.  
cv::mat graySub = grayImg(cv::Rect(10, 10, 100, 100));  
ofImage foo2;  
foo2.setFromPixels(graySub.data, graySub.rows, graySub.cols, OF_IMAGE_SCALE);  
  

foo2 doesnt work,
it seems something wrong with cv::mat operator(cv::Rect()) function.
anyone have the same issue? how to fix it>

i’ve tried:
cv::Mat().colRange(x, w) and cv::Mat().rowRange(y, h)
and both of them give me wrong data.

I think you want:

  
cv::mat graySub(grayImg, cv::Rect(10, 10, 100, 100));  

constructing the new image out of the old image with the set ROI.