您的位置:首页 > 编程语言 > C语言/C++

open cv+C++错误及经验总结(二)

2014-02-18 17:40 471 查看


颜色缩减方法:

如果矩阵元素存储的是单通道像素,使用C或C++的无符号字符类型,那么像素可有256个不同值。但若是三通道图像,这种存储格式的颜色数就太多了(确切地说,有一千六百多万种)。用如此之多的颜色可能会对我们的算法性能造成严重影响。其实有时候,仅用这些颜色的一小部分,就足以达到同样效果。

这种情况下,常用的一种方法是颜色空间缩减。其做法是:将现有颜色空间值除以某个输入值,以获得较少的颜色数。例如,颜色值0到9可取为新值0,10到19可取为10,以此类推。

uchar(无符号字符,即0到255之间取值的数)类型的值除以int值,结果仍是char。因为结果是char类型的,所以求出来小数也要向下取整。利用这一点,刚才提到在uchar定义域中进行的颜色缩减运算就可以表达为下列形式:



这样的话,简单的颜色空间缩减算法就可由下面两步组成:一、遍历图像矩阵的每一个像素;二、对像素应用上述公式。值得注意的是,我们这里用到了除法和乘法运算,而这两种运算又特别费时,所以,我们应尽可能用代价较低的加、减、赋值等运算替换它们。此外,还应注意到,上述运算的输入仅能在某个有限范围内取值,如uchar类型可取256个值。

对于较大的图像,有效的方法是预先计算所有可能的值,然后需要这些值的时候,利用查找表直接赋值即可。(查找表可以是一维或多维数组,存储不同输入值所对应的输出值,优势在于只需读取,无需计算。)

makesenseof有道理

incaseof万一,防备

iterator迭代器index索引,指示,标志indices(index复数)

successive连续的sake缘故,理由

random-access随机存取的serial连续的,串行

Someoperations,liketheoneabove,donotactuallydependonthearrayshape.Theyjustprocesselementsofanarrayonebyone(orelementsfrommultiplearrays
thathavethesamecoordinates,forexample,arrayaddition).Suchoperationsarecalledelement-wise.
Itmakessensetocheckwhetheralltheinput/outputarraysarecontinuous,namely,havenogapsattheendofeachrow.Ifyes,processthemasalongsinglerow:

//computethesumofpositivematrixelements,optimizedvariant
doublesum=0;
intcols=M.cols,rows=M.rows;
if(M.isContinuous())
{
cols*=rows;
rows=1;
}
for(inti=0;i<rows;i++)
{
constdouble*Mi=M.ptr<double>(i);
for(intj=0;j<cols;j++)
sum+=std::max(Mi[j],0.);
}


Incaseofthecontinuousmatrix,theouterloopbodyisexecutedjustonce.So,theoverheadissmaller,whichisespeciallynoticeable
incaseofsmallmatrices.

Finally,thereareSTL-styleiteratorsthataresmartenoughtoskipgapsbetweensuccessiverows:

//computesumofpositivematrixelements,iterator-basedvariant
doublesum=0;
MatConstIterator_<double>it=M.begin<double>(),it_end=M.end<double>();
for(;it!=it_end;++it)
sum+=std::max(*it,0.);


2.使用addweighted()函数将两图像叠加scalar梯状的,分等级的

C++:voidaddWeight(InputArraysrc1,doublealpha,InputArraysrc2,doublebeta,doublegamma,OutputArraydst,intdtype=-1)

Parameters

src1-Fisrtsourcearray.

alpha-Weightforthefirstarrayelements.

src2-Secondsourcearrayofthesamesizeandchannelnumberassrc1.

beta-Weightforthesecondarrayelements.

dst-Destinationarraythathasthesamesizeandnumberofchannelsastheinputarrays.

gamma-Scalaraddedtoeachsum.

dtype-optionaldepthofthedestinationarray.whenbothinputarrayshavethesamedepth,dtypecanbesetto-1,whichwillbeequivalenttosrc1.depth().

ThefunctionaddWeightedcalculatestheweightedsumoftwoarraysasfollows:

dst(1)=saturate(src1(1)alpha+src2(1)beta+gamma)

whereIisamulti-dimensionalindexofarrayelements.Incaseofmulti-channelarrays,eachchannelisprocessedindependently.

Thefunctioncanbereplacedwithamatrixexpression:

dst=src1*alpha+src2*beta+gamma;

C++:voidaddWeighted(InputArraysrc1,doublealpha,InputArraysrc2,doublebeta,doublegamma,OutputArray

dst,intdtype=-1)voidaddWeighted(InputArraysrc1,doublealpha,InputArraysrc2,doublebeta,doublegamma,OutputArray

dst,intdtype=-1)voidaddWeighted(InputArraysrc1,doublealpha,InputArraysrc2,doublebeta,doublegamma,OutputArray

dst,intdtype=-1)

#include"stdafx.h"
#include"cv.h"
#include"highgui.h"
usingnamespacecv;
usingnamespacestd;

voidblending_test()
{
Matsrc1,src2,dst;
doublealpha=0.4;
doublebeta=1-alpha;

src1=imread("F:/HU/test_image/Airplane.jpg",CV_LOAD_IMAGE_COLOR);
src2=imread("F:/HU/test_image/Airplane.jpg",CV_LOAD_IMAGE_COLOR);

if(!src1.data)cout<<"errorloadingsrc1"<<endl;
if(!src2.data)cout<<"errorloadingsrc2"<<endl;

addWeighted(src1,alpha,src2,beta,0.0,dst);

/*cvNamedWindow("add",CV_WINDOW_AUTOSIZE);
cvShowImage("add",dst);*/
namedWindow("add",1);
imshow("add",dst);
cvWaitKey(0);
}
int_tmain(intargc,_TCHAR*argv[])
{

blending_test();
return0;
}


3.在opencv学习中经常看见saturate_cast的使用,加入了溢出保护

02.for(inti=0;i<src1.rows;i++)
03.{
04.constuchar*src1_ptr=src1.ptr<uchar>(i);
05.constuchar*src2_ptr=src2.ptr<uchar>(i);
06.uchar*dst_ptr=dst.ptr<uchar>(i);
07.for(intj=0;j<src1.cols*nChannels;j++)
08.{
09.dst_ptr[j]=saturate_cast<uchar>(src1_ptr[j]*alpha+src2_ptr[j]*beta+gama);//gama=-100,alpha=beta=0.5
10.//dst_ptr[j]=(src1_ptr[j]*alpha+src2_ptr[j]*beta+gama);
11.}
12.}
13.imshow("output2",dst);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: