如何用opencv如何实现数学中sign函数的功能呢?
sign(x)=1,x>0;
sign(x)=0,x=0;
sign(x)=-1,x<0;
如何实现一个cvSign(CvArr* src, CvArr* dst)函数,根据src(可为Mat或是IplImage)中元素的值,决定dst中元素的值?
萌化三仟院
12 years, 10 months ago
Answers
// dst = src .* sign(sign)
void cvSignMul(IplImage* src,IplImage* dst, IplImage* sign)
{
for (int i=0;i<sign->height;i++)
{
for (int j=0;j<sign->width;j++)
{
CvScalar sign_elem=cvGet2D(sign,i,j);
CvScalar src_elem=cvGet2D(src,i,j);
CvScalar dst_elem=cvGet2D(dst,i,j);
for (int n=0;n<4;n++)
{
if (sign_elem.val[n]>0)
{
dst_elem.val[n]=src_elem.val[n];
}
else if (sign_elem.val[n]<0)
{
dst_elem.val[n]=-1*src_elem.val[n];
}
else
{
dst_elem.val[n]=0;
}
}
cvSet2D(dst,i,j,dst_elem);
}
}
}
888888
answered 12 years, 10 months ago