首页 > 编程知识 正文

opencv二值像取反,opencv灰度像二值化处理

时间:2023-05-04 07:25:29 阅读:189769 作者:632

用这种方法对图形进行二值化,可以很好地消除光对图像的影响

# include iostream # include opencvopencv.hppusingnamespacecv; using namespace std; voidthresholdintegral (matinputmat,Mat outputMat ){ int nRows=inputMat.rows; int nCols=inputMat.cols; //createtheintegralimagematsummat; 集成(input mat,sumMat ); ints=max(nrows,nCols ) /8; 双精度=0.15; //perform thresholding int S2=/2; int x1,y1,x2,y2,count,sum; int *y1、*y2; uchar *输入,*输出; for(int=0; i nRows; (y1=S2; y2=i s2; if(y10 ){ y1=0; (if ) y2=nrows ) ) y2=nrows1; }y1=summat.ptr int (y1 ); y2=summat.ptr int (y2; input mat=input mat.ptr uchar (; output mat=output mat.ptr uchar (; for (int=0; ncols; (//setthesxsregionx1=Come Come Everybody S2; x2=S2; if(x10 ){ x1=0; (if ) x2=ncols ) x2=ncols1; (count=) x299个Come Come Everybody x1 (* ) y299个Come Come Everybody y1 ); sum=y2x2y1x2 ] 99p _ y2 [ x1 ] p _ y1 [ x1 ]; if(int ) )input mat* count ) ) sum * (1.099 Come Come Everybody) )_ output mat [ 99 ]=0; } elseoutput mat=255; } } }}int main () matsrc=imread ) (img.jpg ); 马特金; cvtcolor(src,grad,CV_BGR2GRAY ); //ma tbw2=mat :3360 zeros (grad.size ),CV_8UC1 ); threshold integral (接地,接地); imshow (【二值化图】)、grad ); imwrite(imggrad.jpg )、grad ); 维客(0; 返回0; 结果如下图所示。

一般方法进行二值化处理

# include iostream # include opencvopencv.hppusingnamespacestd; 使用命名空间cv; int main ((iplimage * MB dst=cv loadimage ) ) img.jpg; iplimage * MB src=cvcreateimage (cv getsize ) MBdst )、mbd st99 Come Come Everybody深度、mbd st99 Come Come Everybody nchannels ); cvcopy(MBdst,MBSrc ); cvShowImage ('原图',MBSrc ); cvthreshold(MBSrc,MBsrc,120,255,cvthreshbinary ); cvShowImage ()【二值化图表】)、MBSrc ); cvwaitkey(0; 返回0; 结果如下图所示。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。