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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:51:52+00:00 2026-06-17T09:51:52+00:00

I am having a problem to translate numpy’s ndarray functions to their equivalent OpenCV

  • 0

I am having a problem to translate numpy’s ndarray functions to their equivalent
OpenCV C++ calls to reshape/split a n-dimensional cv::Mat into appropriate slices.
In particular i am trying to convert the OpenCV python2 sample “texture_flow.py”
(>= OpenCV 2.4.3) to C++. I’ve marked the lines in question in the snippet below.

# [......]
img = cv2.imread(fn)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# simple width and height tuple
h, w = img.shape[:2]

eigen = cv2.cornerEigenValsAndVecs(gray, 15, 3)
print eigen.shape # prints: (height, widht, 6), i.e. 6 channels

# Problem 1:
# OpenCV's reshape function is not sufficient to do this.
# probably must be split into several steps...
eigen = eigen.reshape(h, w, 3, 2)  # [[e1, e2], v1, v2]
print eigen.shape # prints: (height, width, 3, 2)

# Problem 2:
# I assume this is meant to get the the v1 and v2 matrices 
# from the previous reshape
flow = eigen[:,:,2]
print flow.shape # prints: (height, width, 2), i.e. 2 channels

vis = img.copy()
# C++: vis.data[i] = (uchar)((192 + (int)vis.data[i]) / 2);
vis[:] = (192 + np.uint32(vis)) / 2

d = 12

# Problem 3:
# Can probably be split into 2 nested for-loops 
points =  np.dstack( np.mgrid[d/2:w:d, d/2:h:d] ).reshape(-1, 2)

# [......]

Can someone help me to translate the lines in question to C++?

  • 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-06-17T09:51:54+00:00Added an answer on June 17, 2026 at 9:51 am

    After thoroughly thinking about it it was all easier than expected. It’s only the funny numpy array syntax that was confusing me.
    Reshaping of numpy’s arrays is just the python way to access the single channels of the resulting cv::Mat “eigen”.
    The following code is the C++ version of OpenCV’s “texture_flow.py” (taken from OpenCV 2.4.3). The resulting flow image is not 100% identical to the python version, but it is close enough.

    #include <opencv2/opencv.hpp>
    #include <iostream>
    
    int main (int argc, char** argv)
    {
        cv::TickMeter tm;
        tm.start();
        cv::Mat img = cv::imread(argv[1]);
        cv::Mat gray = cv::Mat();
        cv::cvtColor(img, gray, CV_BGR2GRAY);
        // to preserve the original image
        cv::Mat flow = gray.clone();
        int width = img.cols;
        int height = img.rows;
        int graySize = width * height;
        // "brighten" the flow image 
        // C++ version of:
        // vis[:] = (192 + np.uint32(vis)) / 2
        for (unsigned int i=0; i<graySize; ++i)
        {
             flow.data[i] = (uchar)((192 + (int)flow.data[i]) / 2);
        }
        cv::Mat eigen = cv::Mat(height, width, CV_32FC(6));
        cv::cornerEigenValsAndVecs(gray, eigen, 15, 3);
        // this is the equivalent to all the numpy's reshaping etc. to 
        // generate the flow arrays
        // simply use channel 4 and 5 as the actual flow array in C++
        std::vector<cv::Mat> channels;
        cv::split(eigen, channels);
    
        int d = 12;
        cv::Scalar col(0, 0, 0);
        // C++ version of:
        // points =  np.dstack( np.mgrid[d/2:w:d, d/2:h:d] ).reshape(-1, 2)
        // including the actual line drawing part
        for (unsigned int y=(d/2); y<flow.rows; y+=d)
        {
             for (unsigned int x=(d/2); x<flow.cols; x+=d)
             {
                 if (x < flow.cols && y < flow.rows)
                 {
                     cv::Point p(x, y);
                     float dx = channels[4].at<float>(p) * (d/2);
                     float dy = channels[5].at<float>(p) * (d/2);
                     cv::Point p0(p.x - dx, p.y - dy);
                     cv::Point p1(p.x + dx, p.y + dy);
                     cv::line(flow, p0, p1, col, 1);
                  }
             }
        }
        tm.stop();
        std::cout<<"Flow image generated in "<<tm.getTimeMilli()<<" ms."<<std::endl;
        cv::imshow("FLOW", flow);
        cv::waitKey();
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im having a problem with the HeaderText not being translated when i have definied
Having problem with the middle Div not expanding to the width http://acs.graphicsmayhem.com/images/middiv.jpg Ok, how
Im having problem in my UIscrollView ,this is what I have done: Whenever a
I am having problem using mvc:resources in spring 3.1 configuration. Initially i was working
I am having problem with previewing custom performance counters with PerflibV2. Performance Monitor shows
I am having problem to get a numerical value for this expression where I
I'm having problem with datagrid view. I have attached an image with the code
I am having problem with drawing multiple lines during repaint. The code is as
hello I am having problem related to https:// . I have used FB.getLoginStatus(function(response) function
I'am having problem in accessing json array elements. Below is the response when i

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.