Cropping image in Android using opencv

后端 未结 3 853
走了就别回头了
走了就别回头了 2020-12-30 05:16

I am using OpenCV 2.3.1 in Android. I need to crop the image into half. What I am doing is:

    Mat mIntermediateMat2 = new Mat(frame_height,frame_width,rgba         


        
相关标签:
3条回答
  • 2020-12-30 05:56

    I have always done cropping this way:

    Mat image = ...; // fill it however you want to
    Mat crop(image, Rect(0, 0, image.cols, image.rows / 2)); // NOTE: this will only give you a reference to the ROI of the original data
    
    // if you want a copy of the crop do this:
    Mat output = crop.clone();
    

    Hope that helps!

    0 讨论(0)
  • 2020-12-30 06:12

    There are a few constructors for the Mat class, one of which takes a Mat and an ROI (region of interest).

    Here's how to do it in Android/Java:

    Mat uncropped = getUncroppedImage();
    Rect roi = new Rect(x, y, width, height);
    Mat cropped = new Mat(uncropped, roi);
    
    0 讨论(0)
  • 2020-12-30 06:14

    Seems cv::getRectSubPix does what you want. Plus you don't have to allocate more space than you need. Also it does necessary interpolations if the cropped area is not aligned over an exact pixel.

    This should do what you want. Input type will be the output type.

    Mat dst;
    getRectSubPix(src, Size(src.rows()/2,src.cols()), Point2f(src.rows()/4, src.cols()/2), dst);
    
    0 讨论(0)
提交回复
热议问题