Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6587155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:52:54+00:00 2026-05-25T16:52:54+00:00

I am new to OpenCV. Currently, trying to load and save a defined ROI

  • 0

I am new to OpenCV. Currently, trying to load and save a defined ROI of an image.
For OpenCV 1.x, I got it working with the following function…

#include <cv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>  

void SaveROI(const CStringA& inputFile, const CStringA& outputFile)
{
    if (ATLPath::FileExists(inputFile))
    {
        CvRect rect;
        rect.x      = 8;
        rect.y      = 90;
        rect.width  = 26;
        rect.height = 46;

        IplImage* imgInput = cvLoadImage(inputFile.GetString(), 1); 
        IplImage* imgRoi   = cvCloneImage(imgInput);
        cvSetImageROI(imgRoi, rect);
        cvSaveImage(outputFile.GetString(), imgRoi);

        cvReleaseImage(&imgInput);
        cvReleaseImage(&imgRoi);
    }
}

How can this be done with the OpenCV 2 or C++. I tried the following without a success, the whole image is saved.

void SaveROICPP(const CStringA& inputFile, const CStringA& outputFile)
{      
    if (ATLPath::FileExists(inputFile))
    {
        cv::Mat imgInput = cv::imread(inputFile.GetString());

        if (imgInput.data != NULL)
        {
            cv::Mat imgRoi = imgInput(cv::Rect(8, 90, 26, 46));

            imgInput.copyTo(imgRoi);                 

            cv::imwrite(outputFile.GetString(), imgRoi);
        }
    }
}

Any help or suggestion?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:52:55+00:00Added an answer on May 25, 2026 at 4:52 pm

    You just don’t need to call copyTo:

    void SaveROICPP(const CStringA& inputFile, const CStringA& outputFile)
    {      
        if (ATLPath::FileExists(inputFile))
        {
            cv::Mat imgInput = cv::imread(inputFile.GetString());
    
            if (imgInput.data != NULL)
            {
                cv::Mat imgRoi = imgInput(cv::Rect(8, 90, 26, 46));
                cv::imwrite(outputFile.GetString(), imgRoi);
            }
        }
    }
    

    In your version copyTo sees that imgInput is bigger then imgRoi and reallocates a new full-size matrix to make the copy. imgRoi is already a sub-image and you can simply pass it to any OpenCV function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use the new OpenCV 2.0 API in MS Visual C++ 2008
I'm new to OpenCV and would like to use its Python binding. When trying
i'm new to image processing and opencv, but so far the easy to understand
I am trying to use the Qt new functions described here: http://opencv.willowgarage.com/documentation/cpp/qt_new_functions.html#cv-createbutton And I
I'm trying to use the new 2.0 c++ version of OpenCV , but everything
I am new to learning openCV and was trying to reproduce an online example
i am completly new in openCV(EmguCV) and face comparision. I am trying to implement
i am new in OpenCV and i am trying to setup OpenCV in eclipse
I'm trying to use new OpenCV feature on Android: native camera access. According to
I'm new to OpenCV and image processing in general. I have photos taken with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.