hoang Cap
hoang Cap

Reputation: 804

OpenCV 3 - How can I properly extract the ROI of a cv::Mat?

I've just started working with openCV in iOS, and I want to extract the ROI (Region Of Interest) from a particular cv::Mat.

This is my current code:

- (UIImage*)test {
    UIImage* img = [UIImage imageNamed:@"unnamed"];

    cv::Mat mat1;

    UIImageToMat(img, mat1);

    cv::Rect rect = cv::Rect(175, 219, 130, 50);
    cv::Mat mat2 = mat1(rect);

    return MatToUIImage(mat2);
}

However the result is not as I expected:

Here's the original image, in which I want to extract the bottom texts: https://lh6.ggpht.com/JHYielbi8lDXKgE5h5PSifTqbMKCo8Pc4w3VixR3VB5eedUTIxJE5fco7oHZp294zn4=h900

And here's the result:

enter image description here

I'm absolutely new to OpenCV, so I don't have any idea what causes this. Can someone help please?

Upvotes: 0

Views: 497

Answers (1)

sturkmen
sturkmen

Reputation: 3550

i extracted the ROI with the code below. but i am not sure if MatToUIImage(mat2); will work maybe you need to MatToUIImage(mat2.clone());

cv::Mat mat1 = imread("e:/test/rSfOy.jpg");
cv::Rect rect = cv::Rect(310, 420, 330, 110);
cv::Mat mat2 = mat1(rect);
imshow("rect", mat2);

Upvotes: 2

Related Questions