【问题标题】:pixels are not scanned (c++ and opencv )不扫描像素(c++ 和 opencv)
【发布时间】:2015-02-24 00:01:19
【问题描述】:

我有两个关于图像处理的问题。我想将两个图像相乘以仅获得前景图像。 第一个问题是,当我运行属于代码但没有好的结果时(图片链接是https://drive.google.com/file/d/0B5b8Wm-4Dlb6dkljSlowV3k3TE0/view?usp=sharing)。如果我写我,所有的图片像素都不会被扫描。如果 ı wrteii

另一个问题是,当这个函数循环运行时(循环是一个每秒运行10次的ROS循环),程序终止。我不明白为什么?

Mat depthSubtraction()
{
typedef ushort imgType;
    Mat sub=imread("/home/aylin/Desktop/sub.png");
    Mat dep=imread("/home/aylin/Desktop/depth.png");
    Mat a,b;
    imgType* m_a = NULL;
    imgType* m_b = NULL;
    imgType* m_c = NULL;

    int picWidth = dep.cols; //picture width
    int picHeight = dep.rows; //picture height

    Mat outputImg(picHeight,picWidth, dep.type()); //initialize the output image
    //memset(outputImg.data, 0, picWidth * picHeight*sizeof(imgType));//set the output image to zeros
    //outputImg=zeros(picHeight, picWidth, dep.type());


    m_a=(imgType*)sub.data;
    m_b=(imgType*)dep.data;
    m_c=(imgType*)outputImg.data;




    for(int i=0;i<picWidth*picHeight ;i++,m_a++,m_b++,m_c++)
    {
        if(*m_a<2)
        {
            *m_a=0;
            *m_c = (*m_a)*(*m_b);
            *m_b=0;

        }
        else
        {
            *m_a=1;
            *m_c = (*m_a)*(*m_b);
            *m_b=0;

        }
        cout<<*m_c<<" "<<*m_a<<endl;
    }

    cvtColor(outputImg, b, CV_BGR2GRAY);
    imshow("aaaa",b);
    imwrite("/home/aylin/Desktop/newww.png",b);
    waitKey();



   }


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

/*
  init(argc, argv, "Aylin_node");

   Subscribe_Depth sd;

   Rate spin_rate(10);

   while( ok() ) {
    spinOnce();

    spin_rate.sleep();
    }

     */

depthSubtraction();



return 0;
   }

【问题讨论】:

  • 首先,不要滚动你自己的每像素循环。如果您想对 2 Mat 进行逐元素乘法,请使用 Mat c=a.mul(b);
  • 那么,您阅读的图像属于 Vec3b 类型,而不是 ushort,因此您的“指针魔法”必须爆炸。
  • 再一次,乘法可能是错误的方法。相反,正确读取您的深度图像(请参阅 imread 文档),对其进行阈值处理,并将其用作掩码。

标签: opencv


【解决方案1】:

你的 imgType 应该是 uchar 而不是 ushort。

此外,由于 imread 默认读取彩色图像,因此您的 for 循环应该运行 picWidth*picHeight*3 次。

除此之外我没有看到代码有任何问题,但是如果我想实现相同的功能,我会写如下:

Mat src = imead("image.png", CV_LOAD_IMAGE_GRAYSCALE);
Mat mask = imread("mask.png", CV_LOAD_IMAGE_GRAYSCALE);

Mat dst = Mat::zeros(src.rows, src.cols, CV_8UC1);
src.copyTo(dst, mask);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-08
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 2014-04-13
    • 2016-12-29
    相关资源
    最近更新 更多