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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:32:40+00:00 2026-06-14T18:32:40+00:00

I am a beginner at opencv. I have this task: Make a new image

  • 0

I am a beginner at opencv.
I have this task:

  1. Make a new image

  2. Put a certain image in it at 0,0

  3. Convert the certain image to gray scale

  4. put the grayscaled image next to it ( at 300, 0 )

This is what I did.
I have a class imagehandler that has constructor and all the functions.

cv::Mat m_image

is the member field.

Constructor to make new image:

imagehandler::imagehandler(int width, int height)
: m_image(width, height, CV_8UC3){


}

Constructor to read image from file:

imagehandler::imagehandler(const std::string& fileName)
: m_image(imread(fileName, CV_LOAD_IMAGE_COLOR))
{
if(!m_image.data)
{
    cout << "Failed loading " << fileName << endl;
}

}

This is the function to convert to grayscale:

void imagehandler::rgb_to_greyscale(){

cv::cvtColor(m_image, m_image, CV_RGB2GRAY);

}

This is the function to copy paste image:

//paste image to dst image at xloc,yloc
void imagehandler::copy_paste_image(imagehandler& dst, int xLoc, int yLoc){

cv::Rect roi(xLoc, yLoc, m_image.size().width, m_image.size().height);
cv::Mat imageROI (dst.m_image, roi);

m_image.copyTo(imageROI);
 }

Now, in the main, this is what I did :

imagehandler CSImg(600, 320); //declare the new image
imagehandler myimg(filepath);

myimg.copy_paste_image(CSImg, 0, 0);
CSImg.displayImage(); //this one showed the full colour image correctly
myimg.rgb_to_greyscale();
myimg.displayImage(); //this shows the colour image in GRAY scale, works correctly
myimg.copy_paste_image(CSImg, 300, 0);
CSImg.displayImage(); // this one shows only the full colour image at 0,0 and does NOT show the greyscaled one at ALL!

What seems to be the problem? I’ve been scratching my head for hours on this one!!!

  • 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-14T18:32:41+00:00Added an answer on June 14, 2026 at 6:32 pm

    You have the following problem:

    At the constructor instead of

    m_image(width, height, CV_8UC3){}
    

    you should write

    {  
      m_image.create(width, height, CV_8UC3);  
    } 
    

    instead and don’t worry about the default construction.

    REMARKS:

    • Im not sure cvtColor works properly with the same Mat as input and output, I think it is safer to change it to Mat temp; cvtColor(m_image, temp, CV_...); m_image=temp;
    • You can check if the image is empty by calling m_image.empty() not by checking !m_image.data. Otherwise you can’t be sure. The pointer m_image.data also can be stale due to refcounted resource management.
    • from your earlier question, where I saw a custom destructor: you don’t need that, no worries there.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Beginner Android and Java developer here. I have this snippet of code inside a
I'm a total beginner with OpenCL and I'm trying to make the following kernel
Beginner iOS dev here. I have a problem in my array. Im making an
Beginner in Android development. My code crashes. I have made a simple Java method
Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
Beginner question: I have a dictionary where the values are lists of (a variable
Python beginner here, I have a list of lists and want to refer to
I am a beginner with Windows Azure and I want to make an app
Beginner question here. I'm going to make a Jquery function that is used to
(Beginner to HTML) I have made a Photoshop mock-up of the website I want

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.