基于OpenCV C++写了一个截屏程序,但是当视频总帧数为截屏间隔整数倍时候 死循环
intDeVideo(char*videoNam,intfpsNum){intflag;//该变量为了判断是否进行了完整的截图循环intcount=0;//文件编号开始计...
int DeVideo(char*videoNam, int fpsNum)
{
int flag;//该变量为了判断是否进行了完整的截图循环
int count = 0; //文件编号开始计数值
char filename[13];
CvCapture * capture = cvCaptureFromFile(videoNam);//打开视频文件
if (capture == NULL)printf("Could not load video file\n");//如果没有成功载入
IplImage * frame;
while (1)
{
for (int i = 0; i < fpsNum; i++)
{
frame = cvQueryFrame(capture);
if (!frame)break;
flag = i;
}
if (flag<fpsNum - 1)break;
if (count <= 9)
{
sprintf(filename, "0000000%d.bmp", count++);
printf("ImgNum %d\n Name 0000000%d.bmp\n", count, count - 1);
}
else if (count <= 99 && count >= 10)
{
sprintf(filename, "000000%d.bmp", count++);
printf("ImgNum %d\n Name 000000%d.bmp\n", count, count - 1);
}
else if (count <= 999 && count >= 100)
{
sprintf(filename, "00000%d.bmp", count++);
printf("ImgNum %d\n Name 00000%d.bmp\n", count, count - 1);
}
else if (count <= 9999 && count >= 1000)
{
sprintf(filename, "0000%d.bmp", count++);
printf("ImgNum %d\n Name 0000%d.bmp\n", count, count - 1);
}
else if (count <= 99999 && count >= 10000)
{
sprintf(filename, "000%d.bmp", count++);
printf("ImgNum %d\n Name 000%d.bmp\n", count, count - 1);
}
else if (count <= 999999 && count >= 100000)
{
sprintf(filename, "00%d.bmp", count++);
printf("ImgNum %d\n Name 00%d.bmp\n", count, count - 1);
}
else if (count <= 9999999 && count >= 1000000)
{
sprintf(filename, "0%d.bmp", count++);
printf("ImgNum %d\n Name 0%d.bmp\n", count, count - 1);
}
else
{
sprintf(filename, "%d.bmp", count++);
printf("ImgNum %d\n Name %d.bmp\n", count, count - 1);
}
cvSaveImage(filename, frame);
}
cvReleaseCapture(&capture);
return count;//返回图片数目
}
这么多if else是为了对所截图片按照预先设定好的命名 展开
{
int flag;//该变量为了判断是否进行了完整的截图循环
int count = 0; //文件编号开始计数值
char filename[13];
CvCapture * capture = cvCaptureFromFile(videoNam);//打开视频文件
if (capture == NULL)printf("Could not load video file\n");//如果没有成功载入
IplImage * frame;
while (1)
{
for (int i = 0; i < fpsNum; i++)
{
frame = cvQueryFrame(capture);
if (!frame)break;
flag = i;
}
if (flag<fpsNum - 1)break;
if (count <= 9)
{
sprintf(filename, "0000000%d.bmp", count++);
printf("ImgNum %d\n Name 0000000%d.bmp\n", count, count - 1);
}
else if (count <= 99 && count >= 10)
{
sprintf(filename, "000000%d.bmp", count++);
printf("ImgNum %d\n Name 000000%d.bmp\n", count, count - 1);
}
else if (count <= 999 && count >= 100)
{
sprintf(filename, "00000%d.bmp", count++);
printf("ImgNum %d\n Name 00000%d.bmp\n", count, count - 1);
}
else if (count <= 9999 && count >= 1000)
{
sprintf(filename, "0000%d.bmp", count++);
printf("ImgNum %d\n Name 0000%d.bmp\n", count, count - 1);
}
else if (count <= 99999 && count >= 10000)
{
sprintf(filename, "000%d.bmp", count++);
printf("ImgNum %d\n Name 000%d.bmp\n", count, count - 1);
}
else if (count <= 999999 && count >= 100000)
{
sprintf(filename, "00%d.bmp", count++);
printf("ImgNum %d\n Name 00%d.bmp\n", count, count - 1);
}
else if (count <= 9999999 && count >= 1000000)
{
sprintf(filename, "0%d.bmp", count++);
printf("ImgNum %d\n Name 0%d.bmp\n", count, count - 1);
}
else
{
sprintf(filename, "%d.bmp", count++);
printf("ImgNum %d\n Name %d.bmp\n", count, count - 1);
}
cvSaveImage(filename, frame);
}
cvReleaseCapture(&capture);
return count;//返回图片数目
}
这么多if else是为了对所截图片按照预先设定好的命名 展开
2个回答
展开全部
for (int i = 0; i < fpsNum; i++)
{
frame = cvQueryFrame(capture);
if (!frame)break;
flag = i;
}
问题出在这里
if (!frame)break;
这个break只是中断了for循环
当i为0,即刚进入for循环,视频就达到了最后一帧,则flag还没有在此次for循环赋值就跳出了,而此时flag的值为上一次for循环的最后一次赋值,即 fpsNum-1,导致这一句的break无法执行:if (flag<fpsNum - 1)break
可以这样改:
while (1)
{
bool flag = false;
for (int i = 0; i < fpsNum; i++)
{
frame = cvQueryFrame(capture);
if (!frame)
{
flag = true;
break;
}
}
if(flag)break;
(此处省略)
cvSaveImage(filename, frame);
}
还有个更简单的修改方法可以试一下
在for循环之前即这句话:for (int i = 0; i < fpsNum; i++)
之前对flag进行一次赋值,flag = 0;
{
frame = cvQueryFrame(capture);
if (!frame)break;
flag = i;
}
问题出在这里
if (!frame)break;
这个break只是中断了for循环
当i为0,即刚进入for循环,视频就达到了最后一帧,则flag还没有在此次for循环赋值就跳出了,而此时flag的值为上一次for循环的最后一次赋值,即 fpsNum-1,导致这一句的break无法执行:if (flag<fpsNum - 1)break
可以这样改:
while (1)
{
bool flag = false;
for (int i = 0; i < fpsNum; i++)
{
frame = cvQueryFrame(capture);
if (!frame)
{
flag = true;
break;
}
}
if(flag)break;
(此处省略)
cvSaveImage(filename, frame);
}
还有个更简单的修改方法可以试一下
在for循环之前即这句话:for (int i = 0; i < fpsNum; i++)
之前对flag进行一次赋值,flag = 0;
展开全部
/ /截图
的UIImage *快照;
CGImageRef cgScreen = UIGetScreenImage();
如果(cgScreen){
快照= [UIImage的imageWithCGImage:cgScreen];
CGImageRelease( cgScreen);
}
的CGRect RECT = CGRectMake(0,125,640,750);/ /创建要砍你可以自己
的UIImage * RES =矩形[UIImage的imageWithCGImage:CGImageCreateWithImageInRect([快照的CGImage],矩形)]
/ / RES是后br的UIImage br出手试试吧
的UIImage *快照;
CGImageRef cgScreen = UIGetScreenImage();
如果(cgScreen){
快照= [UIImage的imageWithCGImage:cgScreen];
CGImageRelease( cgScreen);
}
的CGRect RECT = CGRectMake(0,125,640,750);/ /创建要砍你可以自己
的UIImage * RES =矩形[UIImage的imageWithCGImage:CGImageCreateWithImageInRect([快照的CGImage],矩形)]
/ / RES是后br的UIImage br出手试试吧
追问
这是啥
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询