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

  • Home
  • SEARCH
  • 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 8237539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:30:13+00:00 2026-06-07T19:30:13+00:00

i’m learning c++ api of opencv, and for a simple approach i’ve started with

  • 0

i’m learning c++ api of opencv, and for a simple approach i’ve started with try to downsample image (ok i know that there is pyrDown with gaussian resampling but it’s for learning how to access element in Mat class)

this is my code:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

#define original_window "original"
#define manual_window "manual"

using namespace cv;
using namespace std;

Mat img, manual;

void downsample(Mat src, Mat &dst, const Size& s) {
    float factor = src.rows/(float)s.width;
    Mat_<Vec3f> _dst = Mat(s, src.type());
    Mat_<Vec3f> _src = src;
    for(int i=0; i<src.cols; i+=factor) {
        int _i = i/factor;
        for(int j=0; j<src.rows; j+=factor) {
            int _j = j/factor;
            _dst (_j, _i) = _src(j,i);
        }
    }
    cout << "downsample image size: " << _dst.rows << " " << _dst.cols << endl;
    dst = Mat(_dst);
}


int main(int /*argc*/, char** /*argv*/) {

    img = imread("lena.jpg"); 
    cout << "original image size: " << img.rows << " " << img.cols << endl;

    downsample(img, manual, Size(img.cols/2, img.rows/2));

    namedWindow(original_window, CV_WINDOW_AUTOSIZE);
    namedWindow(manual_window, CV_WINDOW_AUTOSIZE);

    while( true )
    {
        char c = (char)waitKey(10);
        
        if( c == 27 )
          { break; }
        
        imshow( original_window, img );
        imshow( manual_window, manual );
    }
        
    return 0;
}

now, i’m doing a downsampling in a fool way: i’m just deleting elements. and i’m try to use c++ api with Mat_.

in manual window i get a white window, and i don’t understand why. event if i try to cout manual i’seeing different values.. what’s wrong with this piece of code?

EDIT 1

i’ve found a solution:

dst.convertTo(dst, src.type()); // in this particular case: src.type() == CV_8UC3 

at the end of downsample()

now my question is: why that? i declare Mat(s, src.type()); why it is modified?

EDIT 2

if i use @go4sri answer with this line

_dst (_j, _i) = src.at<Vec3f>(j, i);

i get this output:
enter image description here

i really does not understand why..

  • 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-07T19:30:15+00:00Added an answer on June 7, 2026 at 7:30 pm

    The way to access an element in OpenCV’s Mat is as follows:

    for a single channel matrix(tmp)

    Matrix_Name.at<dataType>(row, col)
    

    For a three channel matrix( as is the case for a color image), you will need to use the Vec3b/Vec3f type depending upon if yours is a unsigned char/float matrix.

    As yours is a unsigned char 3Dimensional matrix:
    you will have to access it as src.at<Vec3b>(i, j)

    Your downsample should have been:

    void downsample(const Mat& src, Mat &dst, const Size& s) {
        float factor = src.rows/(float)s.height;
        Mat _dst = Mat(s, src.type());
    
        for(int i=0; i < src.cols; i += factor) {
            int _i = i/factor;
            for(int j=0; j<src.rows; j+=factor) {
                int _j = j/factor;
                _dst.at<Vec3b> (_j, _i) = src.at<Vec3b>(j, i);
            }
        }
        cout << "downsample image size: " << _dst.rows << " " << _dst.cols << endl;
        dst = Mat(_dst);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.