#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include "stdio.h"
//Functions Declarations
int isImageBlack(IplImage *srcImg,int width,int height);
void IPLcvtColor(IplImage *,IplImage *,int);
void IPLflip(IplImage *,IplImage *,int);
void IPLLowPassFilter(IplImage *,IplImage *);
void IPLHighPassFilter(IplImage *,IplImage *);
int main()
{
//Capture from Camera
//CvCapture *input_video=cvCaptureFromCAM(0);
CvCapture *input_video = cvCaptureFromAVI("2.avi");
if (input_video == NULL)
{
printf("Error: Can't get input frame.\n");
return -1;
}
IplImage *frame = NULL;
while(true)
{
int statusFrame=1;
do
{
statusFrame=1;
frame = cvQueryFrame( input_video );
if (frame == NULL)
{
printf("The Video Ends\n");
return -1;
}
statusFrame=isImageBlack(frame,frame->width,frame->height);
}while(statusFrame==1);
//Image Operations
/*
//Operation 1................Flip the video
IplImage *flipImage = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,3);
IPLflip(frame,flipImage,0);
*/
cvNamedWindow("Live Frame",1);
cvShowImage("Live Frame",frame);
//Operation 2................Gray Image Conversion
IplImage *grayImage = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
IPLcvtColor(frame,grayImage,0);
cvNamedWindow("Gray Image",1);
cvShowImage("Gray Image",grayImage);
//Operation 4................High Pass Image Conversion
IplImage *highPassImage = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
IPLHighPassFilter(grayImage,highPassImage);
cvNamedWindow("High Pass Image",1);
cvShowImage("High Pass Image",highPassImage);
//Operation 3................Low Pass Filter Image Conversion
IplImage *lowPassImage = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
IPLLowPassFilter(grayImage,lowPassImage);
cvNamedWindow("Low Pass Image",1);
cvShowImage("Low Pass Image",lowPassImage);
cvWaitKey(100);
//Releaseing of Images
if(grayImage!=NULL)
cvReleaseImage(&grayImage);
/*
if(flipImage!=NULL)
cvReleaseImage(&flipImage);
*/
if(lowPassImage!=NULL)
cvReleaseImage(&lowPassImage);
if(highPassImage!=NULL)
cvReleaseImage(&highPassImage);
}
//Release the Memory
if(frame!=NULL)
cvReleaseImage(&frame);
return 0;
}
//Function Definitions
int isImageBlack(IplImage *srcImg,int width,int height)
{
int t,l;
int countBlackPixel=0;
uchar *srcData=(uchar*)srcImg->imageData;
for(t=0;t<height;t++)
{
for(l=0;l<width;l++)
{
if(srcData[t*width*3 + l*3+0]==0 && srcData[t*width*3 + l*3+1]==0 && srcData[t*width*3 + l*3+2]==0)
countBlackPixel++;
}
}
if(countBlackPixel>(width*height/8))
return 1; //Black
else
return 0; //Normal Image
}
void IPLcvtColor(IplImage *inImage,IplImage *grayImage,int type)
{
int imgWidth=inImage->width;
int imgHeight=inImage->height;
int imgStep=grayImage->widthStep;
uchar* srcData = (uchar *)inImage->imageData;
uchar* dstData = (uchar *)grayImage->imageData;
int i,j;
if(type==0)
{
for(i=0;i<imgHeight;i++)
{
for(j=0;j<imgWidth;j++)
{
dstData[i*imgStep+j]=(uchar)(((srcData[i*inImage->widthStep+j*inImage->nChannels + 0])*0.299)
+((srcData[i*inImage->widthStep+j*inImage->nChannels + 1])*0.587)
+((srcData[i*inImage->widthStep+j*inImage->nChannels + 2])*0.114));
}
}
}
}
void IPLflip(IplImage *srcImage,IplImage *dstImage,int flip_mode)
{
int imgWidth=srcImage->width;
int imgHeight=srcImage->height;
uchar* srcData = (uchar *)srcImage->imageData;
uchar* dstData = (uchar *)dstImage->imageData;
int i,j;
if(srcImage->nChannels==1)
{
if(flip_mode==0)
{
for(i=0;i<imgHeight;i++)
{
for(j=0;j<imgWidth;j++)
{
dstData[i*imgWidth+j]=srcData[(imgHeight-i-1)*imgWidth+j];
}
}
}
if(flip_mode==1)
{
for(i=0;i<imgHeight;i++)
{
for(j=0;j<imgWidth;j++)
{
dstData[i*imgWidth+j]=srcData[i*imgWidth+(imgWidth-j-1)];
}
}
}
if(flip_mode==2)
{
for(i=0;i<imgHeight;i++)
{
for(j=0;j<imgWidth;j++)
{
dstData[i*imgWidth+j]=srcData[(imgHeight-i-1)*imgWidth+(imgWidth-j-1)];
}
}
}
}
if(srcImage->nChannels==3)
{
if(flip_mode==0)
{
for(i=0;i<imgHeight;i++)
{
for(j=0;j<imgWidth;j++)
{
dstData[i*srcImage->widthStep+j*srcImage->nChannels + 0]=srcData[(imgHeight-i-1)*dstImage->widthStep+j*dstImage->nChannels + 0];
dstData[i*srcImage->widthStep+j*srcImage->nChannels + 1]=srcData[(imgHeight-i-1)*dstImage->widthStep+j*dstImage->nChannels + 1];
dstData[i*srcImage->widthStep+j*srcImage->nChannels + 2]=srcData[(imgHeight-i-1)*dstImage->widthStep+j*dstImage->nChannels + 2];
}
}
}
if(flip_mode==1)
{
for(i=0;i<imgHeight;i++)
{
for(j=0;j<imgWidth;j++)
{
dstData[i*srcImage->widthStep+j*srcImage->nChannels + 0]=srcData[i*dstImage->widthStep+(imgWidth-j-1)*dstImage->nChannels + 0];
dstData[i*srcImage->widthStep+j*srcImage->nChannels + 1]=srcData[i*dstImage->widthStep+(imgWidth-j-1)*dstImage->nChannels + 1];
dstData[i*srcImage->widthStep+j*srcImage->nChannels + 2]=srcData[i*dstImage->widthStep+(imgWidth-j-1)*dstImage->nChannels + 2];
}
}
}
if(flip_mode==2)
{
for(i=0;i<imgHeight;i++)
{
for(j=0;j<imgWidth;j++)
{
dstData[i*srcImage->widthStep+j*srcImage->nChannels + 0]=srcData[(imgHeight-i-1)*dstImage->widthStep+(imgWidth-j-1)*dstImage->nChannels + 0];
dstData[i*srcImage->widthStep+j*srcImage->nChannels + 1]=srcData[(imgHeight-i-1)*dstImage->widthStep+(imgWidth-j-1)*dstImage->nChannels + 1];
dstData[i*srcImage->widthStep+j*srcImage->nChannels + 2]=srcData[(imgHeight-i-1)*dstImage->widthStep+(imgWidth-j-1)*dstImage->nChannels + 2];
}
}
}
}
}
void IPLLowPassFilter(IplImage *grayImage,IplImage* resultImage)
{
uchar* srcData = (uchar *)grayImage->imageData;
uchar* dstData = (uchar *)resultImage->imageData;
uchar grayValue;
int imgWidth=grayImage->width;
int imgHeight=grayImage->height;
int i,j,k,l;
double filterSum=0;
int filterSize = 3;
// sobel filter in Y direction
double filter[3][3]={
{1, 1, 1},
{1, 1, 1},
{1, 1, 1}};
/*Sobel Filter*/
for(i = 1; i < imgWidth-1; i++)
{
for(j = 1; j < imgHeight-1; j++)
{
filterSum=0;
for(k = 0; k < filterSize; k++)
{
for(l = 0; l < filterSize; l++)
{
int x = j - ((filterSize-1)>>1) + l;
int y = i - ((filterSize-1)>>1) + k;
grayValue = srcData[x*imgWidth + y];
filterSum += (double)(grayValue * (filter[k][l]));
}
}
filterSum=filterSum/9;
if(filterSum<0.0)
filterSum=0;
if(filterSum>255.0)
filterSum=255;
dstData[j*imgWidth+i]=(uchar)filterSum;
}
}
}
void IPLHighPassFilter(IplImage *grayImage,IplImage* resultImage)
{
uchar* srcData = (uchar *)grayImage->imageData;
uchar* dstData = (uchar *)resultImage->imageData;
uchar grayValue;
int imgWidth=grayImage->width;
int imgHeight=grayImage->height;
int i,j,k,l;
double filterSum=0;
int filterSize = 3;
double filter[3][3]={
{-1, -1, -1},
{-1, 9, -1},
{-1, -1, -1}};
for(i = 1; i < imgWidth-1; i++)
{
for(j = 1; j < imgHeight-1; j++)
{
filterSum=0;
for(k = 0; k < filterSize; k++)
{
for(l = 0; l < filterSize; l++)
{
int x = j - ((filterSize-1)>>1) + l;
int y = i - ((filterSize-1)>>1) + k;
grayValue = srcData[x*imgWidth + y];
filterSum += (double)(grayValue * (filter[k][l]));
}
}
filterSum=filterSum;
Basic-Functions.rar_The Conversion
版权申诉
122 浏览量
2022-09-24
17:20:08
上传
评论
收藏 2KB RAR 举报
林当时
- 粉丝: 98
- 资源: 1万+
最新资源
- ARM Limited 发布的《RealView 编译工具 4.0 版编译器参考指南》
- 《2024音视频技术发展报告》,由LiveVideoStack出品,旨在深入了解流媒体和RTC(实时通信技术)的从业情情况
- 2023-04-06-项目笔记 - 第一百二十五阶段 - 4.4.2.123全局变量的作用域-123 -2024.05.06
- MATLAB编程高效实战:涵盖核心数学、科学计算、数据可视化及算法应用,助力工程师与研究人员的必备函数代码集
- halcon 3D图像重建
- 现有student.txt和student-score.txt 将两个文件上传到hdfs上 使用Map/Reduce框架完成下面
- 测试数据如下 1)文件一:data01.txt,内容:Beijing is beautiful I love Beijing
- 1_notepad_share_encrypt.hdoc..bin
- 基于混合遗传算法的三维装箱问题分析-数学建模
- 2023-04-06-项目笔记 - 第一百二十五阶段 - 4.4.2.123全局变量的作用域-123 -2024.05.06
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈