I am a beginner at opencv.
I have this task:
-
Make a new image
-
Put a certain image in it at 0,0
-
Convert the certain image to gray scale
-
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!!!
You have the following problem:
At the constructor instead of
you should write
instead and don’t worry about the default construction.
REMARKS:
Mat temp; cvtColor(m_image, temp, CV_...); m_image=temp;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.