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 6572653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:02:01+00:00 2026-05-25T15:02:01+00:00

The OpenCV 2 contour finder returns a vector<Point2i> , but sometimes you want to

  • 0

The OpenCV 2 contour finder returns a vector<Point2i>, but sometimes you want to use these with a function that requires a vector<Point2f>. What is the fastest, most elegant way to convert?

Here are some ideas. A very general conversion function for anything that can be converted to a Mat:

template <class SrcType, class DstType>
void convert1(std::vector<SrcType>& src, std::vector<DstType>& dst) {
  cv::Mat srcMat = cv::Mat(src);
  cv::Mat dstMat = cv::Mat(dst);
  cv::Mat tmpMat;
  srcMat.convertTo(tmpMat, dstMat.type());
  dst = (vector<DstType>) tmpMat;
}

But this uses an extra buffer, so it’s not ideal. Here’s an approach that pre-allocates the vector then calls copy():

template <class SrcType, class DstType>
void convert2(std::vector<SrcType>& src, std::vector<DstType>& dst) {
  dst.resize(src.size());
  std::copy(src.begin(), src.end(), dst.begin());
}

Finally, using a back_inserter:

template <class SrcType, class DstType>
void convert3(std::vector<SrcType>& src, std::vector<DstType>& dst) {
  std::copy(src.begin(), src.end(), std::back_inserter(dst));
}
  • 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-25T15:02:02+00:00Added an answer on May 25, 2026 at 3:02 pm

    Assuming src and dst are vectors, in OpenCV 2.x you can say:

    cv::Mat(src).copyTo(dst);
    

    And in OpenCV 2.3.x you can say:

    cv::Mat(src).convertTo(dst, cv::Mat(dst).type());  
    

    Note: type() is a function of the Mat and not of the std::vector class. Therefore, you cannot call dst.type(). If you create a Mat instance using dst as input then you can call the function type() for the newly created object.

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

Sidebar

Related Questions

I've been trying to use the OpenCV function: double pointPolygonTest(InputArray contour, Point2f pt, bool
I am trying to use the function ROI in OpenCV to identify a contour,
I use Emgu CV wrapper for OpenCV . I want to create function which
I am trying to use the Python opencv function Moments() on a grayscale image,
I am using OpenCV library and I want to clone picture in separate function,
I am using the OpenCV function: cvExtractSURF but I am finding a major memory
I am using OpenCV library. It doesn't have a modulo operator that can be
I am trying to find the centroid of a contour but am having trouble
I successfully implemented the OpenCV square-detection example in my test application, but now need
I'm using cvFindContours function in c++ and openCv 2.1, since I have to make

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.