📄 alarmsystemview.cpp
字号:
// AlarmSystemView.cpp : implementation of the CAlarmSystemView class
//
#include "stdafx.h"
#include "AlarmSystem.h"
#include "AlarmSystemDoc.h"
#include "AlarmSystemView.h"
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#pragma comment(lib,"vfw32")
#pragma comment(lib,"winmm")
#define IMG_SIZE 307200 // 机器人拍摄的图片为640*480
#define WIDTH 320
#define HEIGHT 240
/////////////////////////////////////////////////////////////////////////////
// CAlarmSystemView
IMPLEMENT_DYNCREATE(CAlarmSystemView, CView)
BEGIN_MESSAGE_MAP(CAlarmSystemView, CView)
//{{AFX_MSG_MAP(CAlarmSystemView)
ON_COMMAND(ID_OPEN, OnOpen)
ON_COMMAND(ID_EXIT, OnExit)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAlarmSystemView construction/destruction
CAlarmSystemView::CAlarmSystemView()
{
// TODO: add construction code here
}
CAlarmSystemView::~CAlarmSystemView()
{
}
BOOL CAlarmSystemView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CAlarmSystemView drawing
void CAlarmSystemView::OnDraw(CDC* pDC)
{
CAlarmSystemDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CAlarmSystemView printing
BOOL CAlarmSystemView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CAlarmSystemView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CAlarmSystemView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CAlarmSystemView diagnostics
#ifdef _DEBUG
void CAlarmSystemView::AssertValid() const
{
CView::AssertValid();
}
void CAlarmSystemView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CAlarmSystemDoc* CAlarmSystemView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAlarmSystemDoc)));
return (CAlarmSystemDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CAlarmSystemView message handlers
void CAlarmSystemView::OnOpen()
{
if(InitAlarm()==TRUE)
{
//进行监测,对第一桢图像进行初始化
int nFrmNum=0;
CString imageName1;
int num=0; //用于一种情况发生时的计数
while(pFrame=cvQueryFrame(pCapture))
{
nFrmNum++;
num++;
int threshold;
if(nFrmNum==1)
{
//初始化,allocate all the buffers
pFrImg=cvCreateImage(cvSize(pFrame->width, pFrame->height),IPL_DEPTH_8U,1);
pBkImg=cvCreateImage(cvSize(pFrame->width, pFrame->height),IPL_DEPTH_8U,1);
HSV=cvCreateImage(cvSize(pFrame->width, pFrame->height),pFrame->depth,pFrame->nChannels);
pFrImg_copy=cvCreateImage(cvSize(pFrame->width, pFrame->height),IPL_DEPTH_8U,1);
pBkMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
pFrMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
pFrameMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
//用于判断面积是否增大
// pFrImg2=cvCreateImage(cvGetSize(pFrame),IPL_DEPTH_8U,1);
// pImgChafen2=cvCreateImage(cvGetSize(pFrame),IPL_DEPTH_8U,1);
//转换成灰度图
cvCvtColor(pFrame,pFrImg,CV_BGR2GRAY);
cvCvtColor(pFrame,pBkImg,CV_BGR2GRAY);
cvConvert(pFrImg, pFrameMat);
cvConvert(pFrImg, pFrMat);
cvConvert(pFrImg, pBkMat);
cvMoveWindow("video", 10, 0);
cvMoveWindow("back", 350, 0);
cvMoveWindow("front", 690, 0);
cvMoveWindow("Chafen",1030,0);
cvFlip(pBkImg,pBkImg,0); //解决图像倒立问题
cvFlip(pFrImg,pFrImg_copy,0);
cvShowImage("video",pFrame);
cvShowImage("back",pBkImg);
cvShowImage("front",pFrImg_copy);
}
else
{
cvCvtColor(pFrame,pFrImg,CV_BGR2GRAY);
cvConvert(pFrImg, pFrameMat);
//差分
cvAbsDiff(pFrameMat, pBkMat, pFrMat);
cvConvert(pFrMat,pFrImg);
// if( (nFrmNum>15) && (num>15) )//等待摄像头曝光时间
if(nFrmNum>15)
{
if(isChange()<400) //环境没变化
{
fprintf(log,"normal!\r\n");
//更新背景
RunningAvg();
}
else //环境变化
{
//otsu求二值化阈值, 然后二值化
threshold=otsu((uchar *)pFrImg->imageData,pFrImg->height,pFrImg->widthStep,0,0,pFrImg->widthStep,pFrImg->height,1);
cvThreshold(pFrImg,pFrImg,threshold,255.0,CV_THRESH_BINARY_INV);
//获取目标区域,以便于计算目标平均亮度,并减少计算量
GetAim(pFrImg);
//计算目标平均亮度和颜色值
color_bright(&avg_bright,&avg_color);
// if( avg_bright>160.0 && (avg_color<110.0 || avg_color>200.0) )
if( avg_bright>183.0 && (avg_color<70.0 || avg_color>200.0) )
{
fprintf(log,"the avg bright is:%f\r\nthe avg color is:%f\r\n",avg_bright,avg_color);
fprintf(log,"the object is bright and it's color is red or yellow!\r\n");
//计算尖角数目(火的轮廓提取好了,计算尖角就可以判断火灾)
SharpAngle(&m_SharpAngleNum,AnglePointPosition);
if( m_SharpAngleNum>0 )
{
fprintf(log,"sharp angle num:%d\r\n",m_SharpAngleNum);
//转换为3通道,以便于显示尖角位置
IplImage* tempImage=cvCreateImage(cvGetSize(pFrImg),pFrImg->depth,3);
cvCvtColor(pFrImg,tempImage,CV_GRAY2BGR);
AnglePointDisplay(tempImage);
cvReleaseImage(&tempImage);
fprintf(log,"fire!\r\n\r\n");
// fprintf(log,"a\r\n");
//当发生一种情况时,停止处理几秒钟。避免重复报警
num=0;
while((pFrame=cvQueryFrame(pCapture))&&(num<5))
{
num++;
nFrmNum++;
cvCvtColor(pFrame,pFrImg,CV_BGR2GRAY);
cvConvert(pFrImg, pFrameMat);
//差分
cvAbsDiff(pFrameMat, pBkMat, pFrMat);
cvConvert(pFrMat,pFrImg);
threshold=otsu((uchar *)pFrImg->imageData,pFrImg->height,pFrImg->widthStep,0,0,pFrImg->widthStep,pFrImg->height,1);
cvThreshold(pFrImg,pFrImg,threshold,255.0,CV_THRESH_BINARY_INV);
RunningAvg();
}
imageName1.Format("new_%d.jpg",nFrmNum);
imageName1="E://image//"+imageName1;
cvSaveImage(imageName1,pFrame);
// cvSaveImage("E://image//new.jpg",pFrame);
// cvSaveImage("E://image//old.jpg",pBkImg);
// MessageBox("着火了!");
////////////////////////////////////////////socket通信,报警,发送彩信
/*
//发出彩信发送命令
m_send="send\r\n"; //\r\n是必须的,因为服务器端读入的是行数据
sendNum=m_send.GetLength();
fprintf(log1,"着火了!请仔细查看家里环境图片!");
int isSend=send(m_hClientSocket,m_send,sendNum,0);
if(isSend==SOCKET_ERROR)
MessageBox("send error");
if(isSend==2)
MessageBox("send ok");
char chInBuffer[128];
int isRecv=recv(m_hClientSocket,(LPSTR)chInBuffer,128,0);
MessageBox(chInBuffer);
if(chInBuffer=="ok")
{
//声音合成输出
}
else
{
//服务器端接收数据出错
}
*/
////////////////////////////接下来启动彩信发送程序
}
else //亮度、颜色满足,但是尖角不满足
{
fprintf(log,"dangerous!\r\n\r\n");
//当发生一种情况时,停止处理几秒钟。避免重复报警
num=0;
while((pFrame=cvQueryFrame(pCapture))&&(num<5))
{
num++;
nFrmNum++;
cvCvtColor(pFrame,pFrImg,CV_BGR2GRAY);
cvConvert(pFrImg, pFrameMat);
//差分
cvAbsDiff(pFrameMat, pBkMat, pFrMat);
cvConvert(pFrMat,pFrImg);
threshold=otsu((uchar *)pFrImg->imageData,pFrImg->height,pFrImg->widthStep,0,0,pFrImg->widthStep,pFrImg->height,1);
cvThreshold(pFrImg,pFrImg,threshold,255.0,CV_THRESH_BINARY_INV);
RunningAvg();
}
imageName1.Format("new_%d.jpg",nFrmNum);
imageName1="E://image//"+imageName1;
cvSaveImage(imageName1,pFrame);
// cvSaveImage("E://image//new.jpg",pFrame);
// cvSaveImage("E://image//old.jpg",pBkImg);
/* //发出彩信发送命令
m_send="send\r\n";
sendNum=m_send.GetLength();
fprintf(log1,"危险!请仔细查看家里环境图片!");
int isSend=send(m_hClientSocket,m_send,sendNum,0);
if(isSend==SOCKET_ERROR)
MessageBox("send error");
if(isSend==2)
MessageBox("send ok");
char chInBuffer[128];
int isRecv=recv(m_hClientSocket,(LPSTR)chInBuffer,128,0);
MessageBox(chInBuffer);
*/
}
}
else //亮度、颜色不满足,不是火
{
//进行人的识别
fprintf(log,"avg bright is:%f\r\navg color is:%f\r\n",avg_bright,avg_color);
fprintf(log,"human intruded!\r\n\r\n");
//保存二值图,以便观察和处理
imageName1.Format("erzhi_%d.jpg",nFrmNum);
imageName1="E://image//"+imageName1;
cvSaveImage(imageName1,pFrImg_copy);
// fprintf(log,"a%d\r\n",nFrmNum);
//当发生一种情况时,停止处理几秒钟。避免重复报警
num=0;
while((pFrame=cvQueryFrame(pCapture))&&(num<5))
{
num++;
nFrmNum++;
cvCvtColor(pFrame,pFrImg,CV_BGR2GRAY);
cvConvert(pFrImg, pFrameMat);
//差分
cvAbsDiff(pFrameMat, pBkMat, pFrMat);
cvConvert(pFrMat,pFrImg);
threshold=otsu((uchar *)pFrImg->imageData,pFrImg->height,pFrImg->widthStep,0,0,pFrImg->widthStep,pFrImg->height,1);
cvThreshold(pFrImg,pFrImg,threshold,255.0,CV_THRESH_BINARY_INV);
RunningAvg();
}
imageName1.Format("new_%d.jpg",nFrmNum);
imageName1="E://image//"+imageName1;
cvSaveImage(imageName1,pFrame);
// cvSaveImage("E://image//new.jpg",pFrame);
// cvSaveImage("E://image//old.jpg",pBkImg);
// MessageBox("可能有人闯入!");
//socket通信,报警,发送彩信,可以传送参数以确定 彩信题目:着火、危险
//发出彩信发送命令
/*
m_send="send\r\n";
sendNum=m_send.GetLength();
fprintf(log1,"可能有人闯入!请仔细查看家里环境图片!");
int isSend=send(m_hClientSocket,m_send,sendNum,0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -