openCV不会在更改颜色后复制到图像(opencv和c ++) [英] openCV Won't copy to image after changed color ( opencv and c++ )

查看:113
本文介绍了openCV不会在更改颜色后复制到图像(opencv和c ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是opencv的初学者。
我有这个任务:


  1. 制作新图片


  2. 将某个图片转换为灰度

  3. b $ b
  4. 将灰度图像放在旁边(在300,0)


是我做的。
我有一个类imagehandler有构造函数和所有的函数。

  cv :: Mat m_image 

是成员字段。



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


}

从文件读取图像的构造函数: / p>

  imagehandler :: imagehandler(const std :: string& fileName)
:m_image(imread(fileName,CV_LOAD_IMAGE_COLOR))
{
if(!m_image.data)
{
cout< 加载失败<< fileName<< endl;
}

}

灰度:

  void imagehandler :: rgb_to_greyscale(){

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

}

这是复制粘贴图像的功能:

  //将图片粘贴到dst图片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()。
cv:Mat imageROI(dst.m_image,roi);

m_image.copyTo(imageROI);
}

现在,主要是我做的:

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

myimg.copy_paste_image(CSImg,0,0);
CSImg.displayImage(); //这一个正确显示了全彩色图像
myimg.rgb_to_greyscale();
myimg.displayImage(); //这显示GRAY缩放的彩色图像,正确工作
myimg.copy_paste_image(CSImg,300,0);
CSImg.displayImage(); //这一个只显示0,0处的全彩色图像,不显示所有的灰度图像!

什么似乎是问题?

解决方案

您有以下问题:



在构造函数而不是

  m_image(width,height,CV_8UC3){} 

您应该写

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

而不必担心默认结构。



备注:




  • 不知道cvtColor是否与输入和输出相同,认为将其更改为 Mat temp; cvtColor(m_image,temp,CV _...);调用 m_image.empty(),可以检查图像是否为空。m_image = temp; !m_image.data 。否则你不能肯定。指针m_image.data也可以因为引用的资源管理而过时。

  • 从前面的问题,我看到一个自定义析构函数:你不需要,不用担心。 / li>

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!!!

解决方案

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.

这篇关于openCV不会在更改颜色后复制到图像(opencv和c ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆