OpenCV保存图像出现卡片现象

各位用过OpenCV或者mfc的大神们,我最近在用OpenCV做图像处理,是基于图像检测隧道内部有没有车,其中保存图像时出现图像卡片的现象,有同学说是OpenCV保存图像时内存是自动分配的,可能是内存串动导致的,让我把图像变量定义为全局变量,可是按照这样改完程序之后还是出现卡图现象~下面附上两张隧道路面图,明天把程序附上~希望大家帮忙解决下!谢谢图片图片

5个回答

你在显示图像时是否正常,最好贴上删减以后的代码,突出问题

ruozhimilan
若芷迷蓝 我没有进行图像显示,保存完之后就直接进行处理了
大约 4 年之前 回复

我主要有两个线程,一个是利用事先保存好的3张隧道内部没有车的照片作为背景,然后相机实时拍图像,实拍图像与背景图像相减,判断有无车辆,如果没车,则实时更新背景图片。

另一个线程是隧道内灯具进行调光时,实时抓拍70张图像,对70张图像求平均,平均图像作为新的背景图像去覆盖之前的3张图像。
发现出问题的图片的时间都是第二个线程保存图片的时间,我对同一个照片保存了3个名字,只有一张图片出现卡片现象。

我用手机上的论坛,图片怎么传不上来了~

代码主要有两个线程的代码:
thread1主要是隧道内灯具调光之后会进行抓拍70张图像,并求这70张的平均图像,保存成3个名字:
void CRealPlayDlg::m_CaputurePicture2(void) //66相机
{

while (flag_com66)//////ex1
{
int nameex1=1;
i66=0;
Sleep(13000);
if (flag_car_stop66==0) //此时没有车辆停在隧道里面
{
m_lex1_2=atoi(m_Lex1);

        char picname66[100][100] ={0};
        char picbj66[3][100] ={0};

        NET_DVR_JPEGPARA JpgPara = {0};
        JpgPara.wPicSize = 0;   //0代表图像是CIF格式,常用的标准化图像格式
        JpgPara.wPicQuality = 0; /* 图片质量系数 0-最好 1-较好 2-一般 */

        BJ_image66_sum11 = cvCreateImage(cvSize(1920,1080),IPL_DEPTH_32F,1);  

        cvZero(BJ_image66_sum11);


        for (int i=0;i<71;i++)
        {
            sprintf(picname66[i],"D:\\image\\ch66_image_%u.jpg",i+1);

        }

        for (i66=0;i66<70;i66++)
        {

            if (lUserID66>=0)/////如果程序已开始运行时,66相机存在
            {
                sprintf(picname66[i66],"D:\\image\\ch66_image_%u.jpg",nameex1);
                NET_DVR_CaptureJPEGPicture(lUserID66, 1, &JpgPara, picname66[i66]);//caputure这句就可以直接保存图像
                imageRT66_1 = cvLoadImage(picname66[i66],0);     

                if (imageRT66_1)//如果图像存在,则进行操作,防止摄相机中途坏掉
                {
                    cvAcc(imageRT66_1,BJ_image66_sum11);  
                    cvReleaseImage(&imageRT66_1);

                    if (i66==69)
                    {

                        BJ_image6611 = cvCreateImage(cvSize(1920,1080),IPL_DEPTH_8U,1);  
                        flag_com66=0;
                        cvConvertScale(BJ_image66_sum11,BJ_image6611,1.0/70.0); //32位图像转换为8位

                        cvSetImageROI(BJ_image6611,cvRect(0,273,1100,BJ_image6611->height-273));//对实时采集图像进行图像截取
                        RT_image_ROI6666 = cvCreateImage(cvSize(1100,BJ_image6611->height-273),BJ_image6611->depth,BJ_image6611->nChannels);//创建目标图像

                        cvCopy(BJ_image6611,RT_image_ROI6666); //将截取后的图像复制到RT_image_ROI中
                        cvResetImageROI(BJ_image6611);//源图像用完后,清空ROI

                        sprintf(picbj66[0],"D:\\image\\beijing\\192.0.0.66_%u_1.jpg",m_lex1_2);
                        sprintf(picbj66[1],"D:\\image\\beijing\\192.0.0.66_%u_2.jpg",m_lex1_2);
                        sprintf(picbj66[2],"D:\\image\\beijing\\192.0.0.66_%u_3.jpg",m_lex1_2);


                        cvSaveImage(picbj66[0],RT_image_ROI6666);
                        cvSaveImage(picbj66[1],RT_image_ROI6666);
                        cvSaveImage(picbj66[2],RT_image_ROI6666);


                        cvReleaseImage(&RT_image_ROI6666);
                        cvReleaseImage(&BJ_image66_sum11);
                        cvReleaseImage(&BJ_image6611);

                    }
                }
                else flag_com66=0;//如果66相机中间坏掉,则不进行图像抓取
            }
            else flag_com66=0;//如果程序一开始运行时,相机就坏掉,也不进行抓图
            nameex1++;
        }
        cvReleaseImage(&BJ_image66_sum11);
    }
    else flag_com66=0;
}

}

另一个线程是一直运行的:事先取出3张无车背景图片,求平均,然后与实时拍摄图像相减,判断有无车辆:
switch (m_lex1) //66相机根据调光等级选择背景照片
{
case 5:
BJ_image66_1= cvLoadImage("D:\image\beijing\192.0.0.66_5_1.jpg",0);
BJ_image66_2= cvLoadImage("D:\image\beijing\192.0.0.66_5_2.jpg",0);
BJ_image66_3= cvLoadImage("D:\image\beijing\192.0.0.66_5_3.jpg",0);
break;
case 10:

    BJ_image66_1=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_10_1.jpg",0); 
    BJ_image66_2=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_10_2.jpg",0); 
    BJ_image66_3=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_10_3.jpg",0);
    break;
case 15:
    BJ_image66_1=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_15_1.jpg",0); 
    BJ_image66_2=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_15_2.jpg",0); 
    BJ_image66_3=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_15_3.jpg",0);
    break;
case 20:

    BJ_image66_1=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_20_1.jpg",0); 
    BJ_image66_2=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_20_2.jpg",0); 
    BJ_image66_3=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_20_3.jpg",0);
    break;
case 25:

    BJ_image66_1=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_25_1.jpg",0); 
    BJ_image66_2=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_25_2.jpg",0); 
    BJ_image66_3=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_25_3.jpg",0); 
    break;
case 30:

    BJ_image66_1=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_30_1.jpg",0); 
    BJ_image66_2=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_30_2.jpg",0); 
    BJ_image66_3=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_30_3.jpg",0); 

    break;
case 35:

    BJ_image66_1=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_35_1.jpg",0); 
    BJ_image66_2=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_35_2.jpg",0); 
    BJ_image66_3=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_35_3.jpg",0);

    break;
case 40:
    BJ_image66_1=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_40_1.jpg",0); 
    BJ_image66_2=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_40_2.jpg",0); 
    BJ_image66_3=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_40_3.jpg",0); 

    break;
case 45:

    BJ_image66_1=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_45_1.jpg",0); 
    BJ_image66_2=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_45_2.jpg",0); 
    BJ_image66_3=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_45_3.jpg",0);
    break;
case 50:

    BJ_image66_1=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_50_1.jpg",0); 
    BJ_image66_2=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_50_2.jpg",0); 
    BJ_image66_3=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_50_3.jpg",0);
    break;
case 80:

    BJ_image66_1=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_80_1.jpg",0); 
    BJ_image66_2=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_80_2.jpg",0); 
    BJ_image66_3=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_80_3.jpg",0);
    break;
case 85:

    BJ_image66_1=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_85_1.jpg",0); 
    BJ_image66_2=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_85_2.jpg",0); 
    BJ_image66_3=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_85_3.jpg",0); 

    break;
case 90:
    BJ_image66_1=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_90_1.jpg",0); 
    BJ_image66_2=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_90_2.jpg",0); 
    BJ_image66_3=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_90_3.jpg",0);
    break;
case 95:

    BJ_image66_1=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_95_1.jpg",0); 
    BJ_image66_2=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_95_2.jpg",0); 
    BJ_image66_3=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_95_3.jpg",0); 
    break;
case 100:
    BJ_image66_1=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_100_1.jpg",0); 
    BJ_image66_2=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_100_2.jpg",0); 
    BJ_image66_3=  cvLoadImage("D:\\image\\beijing\\192.0.0.66_100_3.jpg",0); 

    break;
default:
    break;
}

if (lUserID66>=0)
{
    sprintf(PicName66,"D:\\image\\08工控机\\ch66_image%u_1.jpg",m_nImageNameAdd);
    NET_DVR_CaptureJPEGPicture(lUserID66, 1, &JpgPara, PicName66);//caputure这句就可以直接保存图像
}

/***********************************************
如果摄像机192.0.0.66存在,则对其拍摄的图片进行处理
************************************************/

if (lUserID66>=0) 
{
    if ((BJ_image66_1!=NULL) & (BJ_image66_2!=NULL) & (BJ_image66_3!=NULL))
    {
        BJ_image66_sum = cvCreateImage(cvGetSize(BJ_image66_1),IPL_DEPTH_32F,1);  
        cvZero(BJ_image66_sum);  
        cvAcc(BJ_image66_1,BJ_image66_sum);  
        cvAcc(BJ_image66_2,BJ_image66_sum);  
        cvAcc(BJ_image66_3,BJ_image66_sum);  

        BJ_image66 = cvCreateImage(cvGetSize(BJ_image66_1),IPL_DEPTH_8U,1);  
        cvConvertScale(BJ_image66_sum,BJ_image66,1.0/3.0); //32位图像转换为8位


        cvReleaseImage(&BJ_image66_sum);

        imageRT66     =  cvLoadImage(PicName66,0);               //打开192.0.0.66实时采集的图片

        if (imageRT66)//如果实时采集的图像存在,则进行车辆检测和路面提取,计算均匀度和照度平均值
        {
            flag_camera66=1;
            int a66[15]= {0};
            int sum66=0;
            BaiDian_Tongji66(imageRT66,BJ_image66,&sum66,a66);

            if (sum66>2000)//如果总的白点数大于400,则认为有车
            {
                flag66=1;

            }
            else if (sum66>=180&sum66<=2000) //如果总的白点数在180-400之间,且a[4]大于150,则认为有车
            {
                    if (a66[4]>=150)
                    {
                        flag66=1;
                    }
                    else 
                    {
                        flag66=0;
                        //保存实时图像,替换已有背景图像
                        sprintf(PicName_ForBJ66,"D:\\image\\beijing\\192.0.0.66_%u_%u.jpg",m_lex1 ,BeiJingAdd66);
                        cvSaveImage(PicName_ForBJ66,RT_image_ROI66);

                        if (BeiJingAdd66 == 3)
                        {
                            BeiJingAdd66 =0;
                        }

                        BeiJingAdd66++;

                    }
            }
               else
               {
                   flag66=0;
                   //保存实时图像,替换已有背景图像
                   sprintf(PicName_ForBJ66,"D:\\image\\beijing\\192.0.0.66_%u_%u.jpg",m_lex1 ,BeiJingAdd66);
                   cvSaveImage(PicName_ForBJ66,RT_image_ROI66);

                   if (BeiJingAdd66 == 3)
                   {
                       BeiJingAdd66 =0;
                   }

                   BeiJingAdd66++;
                }
            cvReleaseImage(&RT_image_ROI66);

        }
        else flag_camera66=0;//说明此时相机坏掉了

        cvReleaseImage(&BJ_image66);//释放图像内存
        cvReleaseImage(&imageRT66);
    }
}
    else flag_camera66=0;

cvReleaseImage(&BJ_image66_1);//释放图像内存
cvReleaseImage(&BJ_image66_2);
cvReleaseImage(&BJ_image66_3);


第二个线程是一直运行的,地一个线程只有在灯具调光时才会触发,但是保存之后的图片出现卡图的时间都是在第一个线程拍完70张图,保存平均图像的时候出现的。 求大家帮忙解决下。

cvSaveImage(picbj66[0],RT_image_ROI6666);
cvSaveImage(picbj66[1],RT_image_ROI6666);
cvSaveImage(picbj66[2],RT_image_ROI6666);
你是指最后一局话保存的图像时有误的吗?
试试看保存四次5次,如果是内存上的冲突,那3 4 5图像应该都是错的

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!