#include <cstdio>
#include <cstring>
#include <iostream>
#include "Windows.h"
#include "HCNetSDK.h"
#include "plaympeg4.h"
#include <opencv2\opencv.hpp>
#include "cv.h"
#include "highgui.h"
#include <time.h>
#include "LPKernelEx.h"
#include "stdio.h"
#define USECOLOR 1
using namespace std;
using namespace cv;
#define fasle 0;
// 影像切割
Mat image_crop;
//--------------------------------------------
int iPicNum = 0;//Set channel NO.
LONG nPort = -1;
HWND hWnd = NULL;
bool verify(RotatedRect rect) {
float error = 0.4;
const float aspect = 4.7272;
int min = 15 * aspect * 15; // 面積下限
int max = 125 * aspect * 125; // 面積上限
float rmin = aspect - aspect * error; // 寬高比下限
float rmax = aspect + aspect * error; // 寬高比上限
int area = rect.size.width * rect.size.height; // 計算面積
float r = rect.size.width / rect.size.height; // 計算寬高比
r = r < 1 ? 1 / r : r;
return area >= min && area <= max && r >= rmin && r <= rmax;
}
void yv12toYUV(char *outYuv, char *inYv12, int width, int height, int widthStep)
{
int col, row;
unsigned int Y, U, V;
int tmp;
int idx;
//printf("widthStep=%d.\n",widthStep);
for (row = 0; row<height; row++)
{
idx = row * widthStep;
int rowptr = row * width;
for (col = 0; col<width; col++)
{
//int colhalf=col>>1;
tmp = (row / 2)*(width / 2) + (col / 2);
// if((row==1)&&( col>=1400 &&col<=1600))
// {
// printf("col=%d,row=%d,width=%d,tmp=%d.\n",col,row,width,tmp);
// printf("row*width+col=%d,width*height+width*height/4+tmp=%d,width*height+tmp=%d.\n",row*width+col,width*height+width*height/4+tmp,width*height+tmp);
// }
Y = (unsigned int)inYv12[row*width + col];
U = (unsigned int)inYv12[width*height + width * height / 4 + tmp];
V = (unsigned int)inYv12[width*height + tmp];
// if ((col==200))
// {
// printf("col=%d,row=%d,width=%d,tmp=%d.\n",col,row,width,tmp);
// printf("width*height+width*height/4+tmp=%d.\n",width*height+width*height/4+tmp);
// return ;
// }
if ((idx + col * 3 + 2)> (1200 * widthStep))
{
//printf("row * widthStep=%d,idx+col*3+2=%d.\n",1200 * widthStep,idx+col*3+2);
}
outYuv[idx + col * 3] = Y;
outYuv[idx + col * 3 + 1] = U;
outYuv[idx + col * 3 + 2] = V;
}
}
//printf("col=%d,row=%d.\n",col,row);
}
//解碼回呼 視頻為YUV資料(YV12),音頻為PCM資料
void CALLBACK DecCBFun(long nPort, char * pBuf, long nSize, FRAME_INFO * pFrameInfo, long nReserved1, long nReserved2)
{
int b;
int nchannel = 1;
long lFrameType = pFrameInfo->nType;
if (lFrameType == T_YV12)
{
#if USECOLOR
//int start = clock();
IplImage* pImgYCrCb = cvCreateImage(cvSize(pFrameInfo->nWidth, pFrameInfo->nHeight), 8, 3);//得到影像的Y分量
yv12toYUV(pImgYCrCb->imageData, pBuf, pFrameInfo->nWidth, pFrameInfo->nHeight, pImgYCrCb->widthStep);//得到全部RGB影像
IplImage* pImg = cvCreateImage(cvSize(pFrameInfo->nWidth, pFrameInfo->nHeight), 8, 3);
//cvCvtColor(pImgYCrCb,pImg,CV_YUV2RGB);
cvCvtColor(pImgYCrCb, pImg, CV_YCrCb2RGB);
//cv::Mat dst(pFrameInfo->nHeight,pFrameInfo->nWidth,CV_8UC3);//這里nHeight為720,nWidth為1280,8UC3表示8bit uchar 無符號型別,3通道值
// cv::Mat src(pFrameInfo->nHeight + pFrameInfo->nHeight/2,pFrameInfo->nWidth,CV_8UC1,(uchar*)pBuf);
// cvtColor(src,dst,CV_YUV2BGR_YV12);
//int end = clock();
#else
IplImage* pImg = cvCreateImage(cvSize(pFrameInfo->nWidth, pFrameInfo->nHeight), 8, 1);
memcpy(pImg->imageData, pBuf, pFrameInfo->nWidth*pFrameInfo->nHeight);
#endif
//printf("%d\n",end-start);
//cvNamedWindow("IPCamera1", 0);
//cvShowImage("IPCamera1", pImg);
IplImage* frame_gray = cvCreateImage(cvGetSize(pImg), pImg->depth, 1);
cvCvtColor(pImg, frame_gray, CV_RGB2GRAY);
Mat image = cvarrToMat(frame_gray);
Mat image2 = cvarrToMat(pImg);
imshow("【原始圖】", image);
blur(image, image, Size(5, 5));
imshow("【去噪后】", image);
Sobel(image, image, CV_8U, 1, 0, 3, 1, 0);
imshow("【sobel濾波】", image);
threshold(image, image, 0, 255, CV_THRESH_OTSU);
imshow("【otsu閾值化】", image);
Mat element = getStructuringElement(MORPH_RECT, Size(17, 3));
morphologyEx(image, image, CV_MOP_CLOSE, element);
imshow("【閉運算】", image);
vector<vector<Point>> contours;
findContours(image, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
map<int, RotatedRect> _map;
for (int i = 0; i < contours.size(); i++)
{
drawContours(image, contours, i, Scalar(255), 1); // 繪制輪廓
// 繪制矩形
RotatedRect rect = minAreaRect(contours[i]);
Point2f vertices[4];
rect.points(vertices);
for (int i = 0; i < 4; i++) {
line(image, vertices[i], vertices[(i + 1) % 4], Scalar(255), 2);
}
// 驗證
if (verify(rect)) {
_map[i] = rect;
}
}
imshow("【輪廓提取】", image);
//繪制通過驗證的矩形
int min_diff = 100000;
int index = 0;
const float square = 27.75;
map<int, RotatedRect>::iterator iter;
iter = _map.begin();
while (iter != _map.end()) {
RotatedRect rect = iter->second;
Point2f vertices[4];
rect.points(vertices);
for (int j = 0; j < 4; j++) {
line(image, vertices[j], vertices[(j + 1) % 4], Scalar(255), 10);
}
// 選擇最接近的矩形
int perimeter = arcLength(contours[iter->first], true);
int area = contourArea(contours[iter->first]);
if (area != 0) {
int squareness = perimeter * perimeter / area;
float diff = abs(squareness - square);
if (diff < min_diff) {
min_diff = diff;
index = iter->first;
}
}
iter++;
}
imshow("【通過驗證】", image);
// 繪制最接近的矩形
RotatedRect rect = _map[index];
Point2f vertices[4];
rect.points(vertices);
for (int i = 0; i < 4; i++) {
cout << " asdf" << endl;
line(image2, vertices[i], vertices[(i + 1) % 4], Scalar(0, 255, 0), 10);
}
imshow("【最接近的矩形】", image2);
Mat image3 = cvarrToMat(frame_gray);
// 始終保持寬 > 高
Size rect_size = rect.size;
if (rect_size.width < rect_size.height) {
swap(rect_size.width, rect_size.height);
//}
getRectSubPix(image3, rect.size, rect.center, image_crop);
if (image_crop.rows != 0) imshow("【切割后的車牌】", image_crop);
}
waitKey(10);
cvReleaseImage(&frame_gray);
//cvReleaseImage(&frame_gray);
//imshow("bgr",dst);
//cvWaitKey(1);
//#if USECOLOR
// cvReleaseImage(&pImgYCrCb);
// cvReleaseImage(&pImg);
//cvReleaseImage(&src);
//cvReleaseImage(&dst);
//#else
// cvReleaseImage(&pImg);
//#endif
//此時是YV12格式的視頻資料,保存在pBuf中,可以fwrite(pBuf,nSize,1,Videofile);
//fwrite(pBuf,nSize,1,fp);
}
/***************
else if (lFrameType ==T_AUDIO16)
{
//此時是音頻資料,資料保存在pBuf中,可以fwrite(pBuf,nSize,1,Audiofile);
}
else
{
}
*******************/
}
///實時流回呼
void CALLBACK fRealDataCallBack(LONG lRealHandle, DWORD dwDataType, BYTE *pBuffer, DWORD dwBufSize, void *pUser)
{
DWORD dRet;
switch (dwDataType)
{
case NET_DVR_SYSHEAD: //系統頭
if (!PlayM4_GetPort(&nPort))
{
break;
}
if (dwBufSize > 0)
{
if (!PlayM4_OpenStream(nPort, pBuffer, dwBufSize, 1024 * 1024))
{
dRet = PlayM4_GetLastError(nPort);
break;
}
if (!PlayM4_SetDecCallBackExMend(nPort, DecCBFun, NULL, 0, NULL))
{
dRet = PlayM4_GetLastError(nPort);
break;
}
//打開視頻解碼
if (!PlayM4_Play(nPort, hWnd))
{
dRet = PlayM4_GetLastError(nPort);
break;
}
//打開音頻解碼, 需要碼流是復合流
if (!PlayM4_PlaySound(nPort))
{
dRet = PlayM4_GetLastError(nPort);
break;
}
}
break;
case NET_DVR_STREAMDATA: //碼流資料
if (dwBufSize > 0 && nPort != -1)
{
BOOL inData = PlayM4_InputData(nPort, pBuffer, dwBufSize);
while (!inData)
{
Sleep(10);
inData = PlayM4_InputData(nPort, pBuffer, dwBufSize);
OutputDebugString(L"PlayM4_InputData failed \n");
}
}
break;
}
}
void CALLBACK g_ExceptionCallBack(DWORD dwType, LONG lUserID, LONG lHandle, void *pUser)
{
char tempbuf[256] = { 0 };
switch (dwType)
{
case EXCEPTION_RECONNECT: //預覽時重連
printf("----------reconnect--------%d\n", time(NULL));
break;
default:
break;
}
}
void main() {
//---------------------------------------
// 初始化
NET_DVR_Init();
//設定連接時間與重連時間
NET_DVR_SetConnectTime(2000, 1);
NET_DVR_SetReconnect(10000, true);
//---------------------------------------
// 獲取控制臺視窗句柄
//HMODULE hKernel32 = GetModuleHandle((LPCWSTR)"kernel32");
//GetConsoleWindow = (PROCGETCONSOLEWINDOW)GetProcAddress(hKernel32,"GetConsoleWindow");
//---------------------------------------
// 注冊設備
LONG lUserID;
NET_DVR_DEVICEINFO_V30 struDeviceInfo;
lUserID = NET_DVR_Login_V30("192.168.1.64", 8000, "admin", "honso123", &struDeviceInfo);
if (lUserID < 0)
{
printf("Login error, %d\n", NET_DVR_GetLastError());
NET_DVR_Cleanup();
return;
}
//---------------------------------------
//設定例外訊息回呼函式
NET_DVR_SetExceptionCallBack_V30(0, NULL, g_ExceptionCallBack, NULL);
//cvNamedWindow("IPCamera");
//---------------------------------------
//啟動預覽并設定回呼資料流
//cvNamedWindow("play",1);
NET_DVR_CLIENTINFO ClientInfo;
ClientInfo.lChannel = 1; //Channel number 設備通道號
ClientInfo.hPlayWnd = NULL; //視窗為空,設備SDK不解碼只取流
ClientInfo.lLinkMode = 1; //Main Stream
ClientInfo.sMultiCastIP = NULL;
LONG lRealPlayHandle;
lRealPlayHandle = NET_DVR_RealPlay_V30(lUserID, &ClientInfo, fRealDataCallBack, NULL, TRUE);
if (lRealPlayHandle<0)
{
printf("NET_DVR_RealPlay_V30 failed! Error number: %d\n", NET_DVR_GetLastError());
return;
}
//cvWaitKey(0);
Sleep(-1);
//fclose(fp);
//---------------------------------------
//關閉預覽
if (!NET_DVR_StopRealPlay(lRealPlayHandle))
{
printf("NET_DVR_StopRealPlay error! Error number: %d\n", NET_DVR_GetLastError());
return;
}
//注銷用戶
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
return;
}
運行時總是提示CRT detected that the application wrote to memory after end of heap buffer還有expression:is_block_type_valid(header->_bloke_use。請假各位大神,怎么解決
uj5u.com熱心網友回復:
如果是商用,直接用他們帶識別的攝像機,有相關SDK的,研究就另說了,看提示應該是寫記憶體問題,除錯下唄uj5u.com熱心網友回復:
你必須設斷點進去跟一下,才知道在什么地方出的問題,看你的描述大概都還沒到視頻影像資料處理那些函式就出錯了,應該是一開始的設備讀取或記憶體分配出的問題,先跟進去找到具體的問題點再說解決吧uj5u.com熱心網友回復:
發下看看到底是什么問題。。。轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/79662.html
標籤:圖形處理/算法
