qq_29629287 2015-07-15 07:06 采纳率: 0%
浏览 1585
已结题

opencv中的一段代码,希望有人可以解释下

还非得输入长度30-10000的问题描述。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
opencv中的一段代码,希望有人可以解释下。
就是从垂直投影开始的代码处 没有看懂,好几个循环, 希望大牛可以帮忙解释下,刚接触OPENCV不久。
难道代码没有 传上来 吗

  • 写回答

2条回答 默认 最新

  • qq_29629287 2015-07-15 07:05
    关注
     ```#include <cv.h>
    #include <cxcore.h>
    #include <highgui.h>
    #include <math.h>   
    #define  START  57
    #define  END  181
    #define  MOD_MAX 15
    #define  MOD_MIN  3
    #define  WIDTH  34 //模板宽度宽度
    #define   S(image,x,y) ((uchar*)(image->imageData + image->widthStep*(y)))[(x)]
     int main()
    {
    
    
        IplImage *src=0;
        IplImage *pImg8uSmooth=NULL;       //高斯滤波后的图
        IplImage *pImgHist=NULL; 
        IplImage *pImgThreshold=NULL;
        IplImage *pImgResize=NULL;        //归一化为高90,宽409的图像
        src=cvLoadImage("E:/image/car1/77.jpg",0);
            if(!src)
                printf("can not load file: %s","E:\\tupain\\1.jpg");
        else
        {  
            int nWidth=409,nHeight=90;
            pImg8uSmooth=cvCreateImage(cvSize(nWidth,nHeight),IPL_DEPTH_8U,1);
            pImgThreshold=cvCreateImage(cvSize(nWidth,nHeight),IPL_DEPTH_8U,1);
            pImgResize=cvCreateImage(cvSize(nWidth,nHeight),IPL_DEPTH_8U,1);//归一化
    
            cvResize(src,pImgResize,CV_INTER_LINEAR); //线性插值
            cvSmooth(pImgResize,pImg8uSmooth,CV_GAUSSIAN,3,0,0);//高斯滤波
            cvThreshold(pImg8uSmooth,pImgThreshold,100,255,CV_THRESH_BINARY ); 
    
            int l_sum[409]={0},modle[WIDTH]={0},subre[WIDTH]={0};
            int sum=0,i=0,j=0,k=0,g=0;
            int rig=0,end=0;//分隔符开始与结束位置
            int flag=0;
    
    
            modle[0]=MOD_MIN*255;
            modle[1]=MOD_MIN*255;
            modle[WIDTH-1]=MOD_MIN*255;
            modle[WIDTH-2]=MOD_MIN*255;
            for(i=2;i<WIDTH-2;i++)modle[i]=MOD_MAX*255;
            printf("The pImgThreshold->height is:%d and pImgThreshold->width is: %d\n",pImgThreshold->height,pImgThreshold->width);
    
    
            printf("垂直投影\n");//垂直投影
            for(i=0;i<pImgThreshold->width;i++)
            {  sum=0;
            for(j=0;j<pImgThreshold->height;j++)
            {
                sum=S(pImgThreshold,i,j)+sum;//灰度之和
            }
            l_sum[i]=sum;
            //printf("The l_sum[%d]=%d\n",i,l_sum[i]);
            }
    
            flag=0;
            int m=WIDTH;
            while(m>7)
            { 
                for(i=START;i+m<END;i++)
                {  
    
                    for(j=0,g=i;j<m;j++,g++)
                    {
                        subre[j]=modle[j]-l_sum[g];
                        // printf("The subre[%d]=%d\n",j,subre[j]);
                        //  printf("The l_sum[%d]=%d\n",g,l_sum[g]);
                    }
    
                    j=0;
                    while(subre[j]>0&&j<m)j++;
                    if(j==m)
                    {
                        flag=1;
                        rig=i; 
                        end=rig+m;
                        break;
                    }
                }
    
                if(!flag)
                {
                    modle[m-3]=modle[m-1];
                    modle[m-4]=modle[m-2];
                    m--; 
                }
                else break;
    
            }
            if(m<=7) printf("have not found the rig and end",rig,end);
            printf("orig=%d and  end=%d\n",rig,end);
    
    
            int min_value=0,min_col=0;//最小值及其所在列
            int oneright=0,oneleft=0,twoleft=0,threeright=0,fourleft=0,fourright=0,fiveleft=0,fiveright=0,sixleft=0,sixright=0,sevenleft=0,sevenright=0;
            i=rig-(int)((45/90.0)*pImgThreshold->height)-1;
            min_value=l_sum[i];
            min_col=i;
            for(;i>=rig-(int)((57/90.0)*pImgThreshold->height)-1;i--)
            {
                // printf ("l_sum[%d]=%d\n",i,l_sum[i]);
                if(min_value>l_sum[i])
                { 
                    min_value=l_sum[i];
                    min_col=i;  
                }
    
            }
            for(i=min_col;i<min_col+(int)((12/90.0)*pImgThreshold->height);i++)
            {
                // printf ("l_sum[%d]=%d\n",i,l_sum[i]);
                if(l_sum[i]>3*255)
                {
                    twoleft=i;break;
                }      
            }
            if(i>=min_col+(int)((12/90.0)*pImgThreshold->height))
            {
                printf("hvan not found the twoleft\n");
                twoleft=rig-(int)((51/90.0)*pImgThreshold->height);
            }
            for(i=min_col;i>min_col-(int)((12/90.0)*pImgThreshold->height);i--)
            {
                //printf ("l_sum[%d]=%d\n",i,l_sum[i]);
                if(l_sum[i]>3*255)
                {
                    oneright=i;break;
                }                     
            }
            if(i<=min_col-(int)((12/90.0)*pImgThreshold->height))
            {
                printf("hvan not found the oneright\n");
                oneright=rig-(int)((51/90.0)*pImgThreshold->height);
            }
    
            printf("the oneright=%d and twoleft=%d\n",oneright,twoleft);
    
    
    
            i=oneright-(int)((40/90.0)*pImgThreshold->height)-1;
            min_value=l_sum[i];
            min_col=i;
            for(;i>=oneright-(int)((52/90.0)*pImgThreshold->height)-1;i--)
            {
                // printf ("l_sum[%d]=%d\n",i,l_sum[i]);
                if(min_value>l_sum[i])
                { 
                    min_value=l_sum[i];
                    min_col=i;  
                }
            }
            for(i=min_col;i<min_col+(int)((12/90.0)*pImgThreshold->height);i++)
            {
                // printf ("l_sum[%d]=%d\n",i,l_sum[i]);
                if(l_sum[i]>3*255)
                {
                    oneleft=i;break;
                }      
            }
            if(i>=min_col+(int)((12/90.0)*pImgThreshold->height))
            {
                printf("hvan not found the oneleft\n");
                oneleft=oneright-(int)((51/90.0)*pImgThreshold->height);
            }    
            if(oneleft<0){ oneleft=0;printf("have not found  the oneleft\n");}
            printf("the oneleft=%d\n",oneleft);
    
            i=end+(int)((45/90.0)*pImgThreshold->height)+1;
            min_value=l_sum[i];
            min_col=i;
            for(;i<=end+(int)((57/90.0)*pImgThreshold->height);i++)
            {
                //printf ("l_sum[%d]=%d\n",i,l_sum[i]);
                if(min_value>l_sum[i])
                { 
                    min_value=l_sum[i];
                    min_col=i;  
                }
    
            }
            for(i=min_col;i>min_col-(int)((12/90.0)*pImgThreshold->height);i--)
            {
                //printf ("l_sum[%d]=%d\n",i,l_sum[i]);
                if(l_sum[i]>3*255)
                {
                    threeright=i;break;
                }      
            }
            if(i<=min_col-(int)((12/90.0)*pImgThreshold->height))
            {
                printf("hvan not found the threerigth\n");
                threeright=end+(51/90)*pImgThreshold->height;
            }
    
            for(i=min_col;i<min_col+(int)((12/90.0)*pImgThreshold->height);i++)
            {
                //printf ("l_sum[%d]=%d\n",i,l_sum[i]);
                if(l_sum[i]>3*255)
                {
                    fourleft=i;break;
                }      
            }
            if(i>=min_col+(int)((12/90.0)*pImgThreshold->height))
            {
                //printf("hvan not found the fourleft\n");
                fourleft=end+(51/90)*pImgThreshold->height;
            }
            printf("the threeright=%d and the fourletf=%d\n",threeright,fourleft);
    
            i=fourleft+(int)((45/90.0)*pImgThreshold->height)+1;
            min_value=l_sum[i];
            min_col=i;
            for(;i<=fourleft+(int)((57/90.0)*pImgThreshold->height);i++)
            {
                //printf ("l_sum[%d]=%d\n",i,l_sum[i]);
                if(min_value>l_sum[i])
                { 
                    min_value=l_sum[i];
                    min_col=i;  
                }
            }
            for(i=min_col;i>min_col-(int)((12/90.0)*pImgThreshold->height);i--)
            {
                // printf ("l_sum[%d]=%d\n",i,l_sum[i]);
                if(l_sum[i]>3*255)
                {
                    fourright=i;break;
                }      
            }
            if(i<=min_col-(int)((12/90.0)*pImgThreshold->height))
            {
                printf("hvan not found the fouright\n");
                fourright=fourleft+(int)((51/90.0)*pImgThreshold->height);
            }
    
            for(i=min_col;i<min_col+(int)((12/90.0)*pImgThreshold->height);i++)
            {
                // printf ("l_sum[%d]=%d\n",i,l_sum[i]);
                if(l_sum[i]>3*255)
                {
                    fiveleft=i;break;
                }                     
            }
            if(i>=min_col+(int)((12/90.0)*pImgThreshold->height))
            {
                printf("hvan not found the fiveright\n");
                fiveleft=fourleft+(int)((51/90.0)*pImgThreshold->height);
            }
            printf("the fourright=%d and the fiveleft=%d\n",fourright,fiveleft);
            i=fiveleft+(int)((45/90.0)*pImgThreshold->height)+1;
            min_value=l_sum[i];
            min_col=i;
            for(;i<=fiveleft+(int)((57/90.0)*pImgThreshold->height);i++)
            {
                // printf ("l_sum[%d]=%d\n",i,l_sum[i]);
                if(min_value>l_sum[i])
                { 
                    min_value=l_sum[i];
                    min_col=i;  
                }
    
            }
            for(i=min_col;i>min_col-(int)((12/90.0)*pImgThreshold->height);i--)
            {
                // printf ("l_sum[%d]=%d\n",i,l_sum[i]);
                if(l_sum[i]>3*255)
                {
                    fiveright=i;break;
                }      
            }
            if(i<=min_col-(int)((12/90.0)*pImgThreshold->height))
            {
                printf("hvan not found the fiveright\n");
                fiveright=fiveleft+(int)((51/90.0)*pImgThreshold->height);
            }
    
    
            for(i=min_col;i<min_col+(int)((12/90.0)*pImgThreshold->height);i++)
            {
                //printf ("l_sum[%d]=%d\n",i,l_sum[i]);
                if(l_sum[i]>3*255)
                {
                    sixleft=i;break;
                }                     
            }
            if(i>=min_col+(int)((12/90.0)*pImgThreshold->height))
            {
                printf("hvan not found the sixright\n");
                sixleft= fiveleft+(int)((51/90.0)*pImgThreshold->height);
            }
            printf("the fiveright=%d and the sixleft=%d\n",fiveright,sixleft);
    
            i=sixleft+(int)((45/90.0)*pImgThreshold->height)+1;
            min_value=l_sum[i];
            min_col=i;
            for(;i<=sixleft+(int)((57/90.0)*pImgThreshold->height);i++)
            {
                // printf ("l_sum[%d]=%d\n",i,l_sum[i]);
                if(min_value>l_sum[i])
                { 
                    min_value=l_sum[i];
                    min_col=i;  
                }
    
            }
            for(i=min_col;i>min_col-(int)((12/90.0)*pImgThreshold->height);i--)
            {
                //printf ("l_sum[%d]=%d\n",i,l_sum[i]);
                if(l_sum[i]>3*255)
                {
                    sixright=i;break;
                }      
            }
            if(i<=min_col-(int)((12/90.0)*pImgThreshold->height))
            {
                //printf("hvan not found the fiveright\n");
                sixright=sixleft+(int)((51/90.0)*pImgThreshold->height);
            }
    
    
            for(i=min_col;i<min_col+(int)((12/90.0)*pImgThreshold->height);i++)
            {
                //printf ("l_sum[%d]=%d\n",i,l_sum[i]);
                if(l_sum[i]>3*255)
                {
                    sevenleft=i;break;
                }                     
            }
            if(i>=min_col+(int)((12/90.0)*pImgThreshold->height))
            {
                printf("hvan not found the sixright\n");
                sevenleft=sixleft+(int)((51/90.0)*pImgThreshold->height);
            }
            printf("the sixright=%d and the sevenleft=%d\n",sixright,sevenleft);
            i=sevenleft+(int)((45/90.0)*pImgThreshold->height)+1;
            min_value=l_sum[i];
            min_col=i;
            for(;(i<=sevenleft+(int)((57/90.0)*pImgThreshold->height))&&i<pImgThreshold->width;i++)
            {
                // printf ("l_sum[%d]=%d\n",i,l_sum[i]);
                if(min_value>l_sum[i])
                { 
                    min_value=l_sum[i];
                    min_col=i;  
                }
    
            }
            for(i=min_col;i>min_col-(int)((12/90.0)*pImgThreshold->height);i--)
            {
                //printf ("l_sum[%d]=%d\n",i,l_sum[i]);
                if(l_sum[i]>3*255)
                {
                    sevenright=i;break;
                }      
            }
            if(i<=min_col-(int)((12/90.0)*pImgThreshold->height))
            {
                printf("hvan not found the fiveright\n");
                sevenright=sevenleft+(int)((51/90.0)*pImgThreshold->height);
            }
            if(sevenright>pImgThreshold->width-1){ sevenright=pImgThreshold->width-1;printf("have not found  the sevenright\n");}
            printf("the sevenrigh=%d\n", sevenright);
    
            cvLine(pImgThreshold,cvPoint(oneleft,0),cvPoint(oneleft,pImgThreshold->height),cvScalar(188,0,0),1,8,0);
            cvLine(pImgThreshold,cvPoint(oneright,0),cvPoint(oneright,pImgThreshold->height),cvScalar(188,0,0),1,8,0);
            cvLine(pImgThreshold,cvPoint(twoleft,0),cvPoint(twoleft,pImgThreshold->height),cvScalar(255,0,0),1,8,0);
            cvLine(pImgThreshold,cvPoint(rig,0),cvPoint(rig,pImgThreshold->height),cvScalar(188,0,0),1,8,0);
            cvLine(pImgThreshold,cvPoint(end,0),cvPoint(end,pImgThreshold->height),cvScalar(255,0,0),1,8,0);
            cvLine(pImgThreshold,cvPoint(threeright,0),cvPoint(threeright,pImgThreshold->height),cvScalar(188,0,0),1,8,0);
            cvLine(pImgThreshold,cvPoint(fourleft,0),cvPoint(fourleft,pImgThreshold->height),cvScalar(255,0,0),1,8,0);
            cvLine(pImgThreshold,cvPoint(fourright,0),cvPoint(fourright,pImgThreshold->height),cvScalar(188,0,0),1,8,0);
            cvLine(pImgThreshold,cvPoint(fiveleft,0),cvPoint(fiveleft,pImgThreshold->height),cvScalar(255,0,0),1,8,0);
            cvLine(pImgThreshold,cvPoint(fiveright,0),cvPoint(fiveright,pImgThreshold->height),cvScalar(188,0,0),1,8,0);
            cvLine(pImgThreshold,cvPoint(sixleft,0),cvPoint(sixleft,pImgThreshold->height),cvScalar(255,0,0),1,8,0);
            cvLine(pImgThreshold,cvPoint(sixright,0),cvPoint(sixright,pImgThreshold->height),cvScalar(188,0,0),1,8,0);
            cvLine(pImgThreshold,cvPoint(sevenleft,0),cvPoint(sevenleft,pImgThreshold->height),cvScalar(255,0,0),1,8,0);
            cvLine(pImgThreshold,cvPoint(sevenright,0),cvPoint(sevenright,pImgThreshold->height),cvScalar(188,0,0),1,8,0);
    
    
    
            IplImage *pImgCharOne=NULL;//截取字符
            IplImage *pImgCharTwo=NULL;
            IplImage *pImgCharThree=NULL;
            IplImage *pImgCharFour=NULL;
            IplImage *pImgCharFive=NULL;
            IplImage *pImgCharSix=NULL;
            IplImage *pImgCharSeven=NULL;
            pImgCharOne=cvCreateImage(cvSize(oneright-oneleft+1,nHeight),IPL_DEPTH_8U,1);
            pImgCharTwo=cvCreateImage(cvSize(rig-twoleft+2,nHeight),IPL_DEPTH_8U,1);
            pImgCharThree=cvCreateImage(cvSize(threeright-end+2,nHeight),IPL_DEPTH_8U,1);
            pImgCharFour=cvCreateImage(cvSize(fourright-fourleft+2,nHeight),IPL_DEPTH_8U,1);
            pImgCharFive=cvCreateImage(cvSize(fiveright-fiveleft+2,nHeight),IPL_DEPTH_8U,1);
            pImgCharSix=cvCreateImage(cvSize(sixright-sixleft+2,nHeight),IPL_DEPTH_8U,1);
            pImgCharSeven=cvCreateImage(cvSize(sevenright-sevenleft+1,nHeight),IPL_DEPTH_8U,1);
    
            CvRect ROI_rect1;
            ROI_rect1.x=oneleft;
            ROI_rect1.y=0;
            ROI_rect1.width=oneright-oneleft+1;
            ROI_rect1.height=pImgResize->height;
            cvSetImageROI(pImgResize,ROI_rect1);
            cvCopy(pImgResize,pImgCharOne,NULL); //获取第1个字符
            cvResetImageROI(pImgResize);
    
            ROI_rect1.x=twoleft-1;
            ROI_rect1.y=0;
            ROI_rect1.width=rig-twoleft+2;
            ROI_rect1.height=pImgResize->height;
            cvSetImageROI(pImgResize,ROI_rect1);
            cvCopy(pImgResize,pImgCharTwo,NULL); //获取第2个字符
            cvResetImageROI(pImgResize);
    
            ROI_rect1.x=end-1;
            ROI_rect1.y=0;
            ROI_rect1.width=threeright-end+2;
            ROI_rect1.height=pImgResize->height;
            cvSetImageROI(pImgResize,ROI_rect1);
            cvCopy(pImgResize,pImgCharThree,NULL); //获取第3个字符
            cvResetImageROI(pImgResize);
    
    
            ROI_rect1.x=fourleft-1;
            ROI_rect1.y=0;
            ROI_rect1.width=fourright-fourleft+2;
            ROI_rect1.height=pImgResize->height;
            cvSetImageROI(pImgResize,ROI_rect1);
            cvCopy(pImgResize,pImgCharFour,NULL); //获取第4个字符
            cvResetImageROI(pImgResize);
    
            ROI_rect1.x=fiveleft-1;
            ROI_rect1.y=0;
            ROI_rect1.width=fiveright-fiveleft+2;
            ROI_rect1.height=pImgResize->height;
            cvSetImageROI(pImgResize,ROI_rect1);
            cvCopy(pImgResize,pImgCharFive,NULL); //获取第5个字符
            cvResetImageROI(pImgResize);
    
            ROI_rect1.x=sixleft-1;
            ROI_rect1.y=0;
            ROI_rect1.width=sixright-sixleft+2;
            ROI_rect1.height=pImgResize->height;
            cvSetImageROI(pImgResize,ROI_rect1);
            cvCopy(pImgResize,pImgCharSix,NULL); //获取第6个字符
            cvResetImageROI(pImgResize);
    
            ROI_rect1.x=sevenleft-1;
            ROI_rect1.y=0;
            ROI_rect1.width=sevenright-sevenleft+1;
            ROI_rect1.height=pImgResize->height;
            cvSetImageROI(pImgResize,ROI_rect1);
            cvCopy(pImgResize,pImgCharSeven,NULL); //获取第7个字符
            cvResetImageROI(pImgResize);
    
    
            cvNamedWindow("垂直投影",CV_WINDOW_AUTOSIZE);
            cvMoveWindow("垂直投影",0,0);
            cvShowImage("垂直投影",pImgThreshold);
            //cvSaveImage("E:\\new\\23.jpg",pImgThreshold);
    
            cvNamedWindow("one",CV_WINDOW_AUTOSIZE);
            cvMoveWindow("one",0,300);
            cvShowImage("one",pImgCharOne);
    
            cvNamedWindow("two",1);
            cvMoveWindow("two",50,300);
            cvShowImage("two",pImgCharTwo);
            //cvSaveImage("E:\\library\\0.jpg",pImgCharTwo);
    
            cvNamedWindow("three",1);
            cvMoveWindow("three",100,300);
            cvShowImage("three",pImgCharThree);
            //cvSaveImage("E:\\library\\0.jpg",pImgCharThree);
    
            cvNamedWindow("four",1);
            cvMoveWindow("four",150,300);
            cvShowImage("four",pImgCharFour);
            //cvSaveImage("E:\\library\\17.jpg",pImgCharFour);
    
            cvNamedWindow("five",1);
            cvMoveWindow("five",200,300);
            cvShowImage("five",pImgCharFive);
            //cvSaveImage("E:\\library\\3.jpg",pImgCharFive);
    
            cvNamedWindow("six",1);
            cvMoveWindow("six",250,300);
            cvShowImage("six",pImgCharSix);
            //cvSaveImage("E:\\library\\1.jpg",pImgCharSix);
    
            cvNamedWindow("seven",1);
            cvMoveWindow("seven",300,300);
            cvShowImage("seven",pImgCharSeven);
            //cvSaveImage("E:\\library\\14.jpg",pImgCharSeven);
    
            cvWaitKey();
    
            cvReleaseImage(&pImgThreshold);
            cvDestroyWindow("显示高斯滤波后图像");
            cvDestroyWindow("one");
            cvReleaseImage(&pImgCharOne);
            cvDestroyWindow("two");
            cvReleaseImage(&pImgCharTwo);
            cvDestroyWindow("three");
            cvReleaseImage(&pImgCharThree);
            cvDestroyWindow("four");
            cvReleaseImage(&pImgCharFour);
            cvDestroyWindow("five");
            cvReleaseImage(&pImgCharFive);
            cvDestroyWindow("six");
            cvReleaseImage(&pImgCharSix);
            cvDestroyWindow("seven");
            cvReleaseImage(&pImgCharSeven);
            return -1;
        }
        return 0;
    }   
    
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧