【发布时间】: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