简体   繁体   中英

Live Video feed from IP camera using OpenCv and multithreading

Thank you for taking time and reading my post. I have an application which accepts username, password and IP address of IP camera and displays the video feed(This is done by using timer), also I have button named "Capture" which when clicked saves images to my disk. Also i have tried same for two IP Camera simultaneously, but when the other camera is added the stream of first camera slow downs. So my question is "Is their any scope of solving this slow down problem by using threading with my application". I am new to MFC and C++ and OpenCV. I am using OpenCV to display the video feed from IP cam. Experts please help me to get the right solution. Here's the code which I have implemented to get the video feed.

void CStaticEx::OnPaint()
{
CPaintDC dc(this); // device context for painting

CRect rect;
GetClientRect(&rect);

m_pFrameImg = cvQueryFrame(m_pCamera);
//If camera is disconnected, control goes
//in if condition and displays message in 
//picture control
if(m_pFrameImg == NULL){
    KillTimer( ONE);
    CString sCamName;
    sCamName.Format(_T(CAM_ID_TXT),m_IpAddr);
    CString sDviceLost = _T(CONNECTION_LOST_TXT);
    sCamName.Append(sDviceLost);
    dc.SetBkMode(OPAQUE);
    dc.SetBkColor(BACKBRND_COLOR_TXTOUT);
    dc.SetTextColor(ERR_TXT_COLOR);// BGR(0xbbggrr);
    dc.TextOut(ERR_TXT_XAXIS,ERR_TXT_YAXIS,sCamName,sCamName.GetLength());
    return;//check after removinf return;
}
BITMAPINFO bitmapInfo;
bitmapInfo.bmiHeader.biSize             =   sizeof(BITMAPINFOHEADER);
bitmapInfo.bmiHeader.biPlanes           =   ONE;
bitmapInfo.bmiHeader.biCompression      =   BI_RGB;
bitmapInfo.bmiHeader.biXPelsPerMeter    =   PELSPERMETER;
bitmapInfo.bmiHeader.biYPelsPerMeter    =   PELSPERMETER;
bitmapInfo.bmiHeader.biClrUsed          =   ZERO;
bitmapInfo.bmiHeader.biClrImportant     =   ZERO;
bitmapInfo.bmiHeader.biSizeImage        =   ZERO;
bitmapInfo.bmiHeader.biWidth            =   m_pFrameImg->width;
bitmapInfo.bmiHeader.biHeight           =   -m_pFrameImg->height;
IplImage* tempImage;

if(m_pFrameImg->nChannels == THREE) //Number of color channels (1,2,3,4)
{
    tempImage = (IplImage*)cvClone(m_pFrameImg);
    bitmapInfo.bmiHeader.biBitCount=tempImage->depth * tempImage->nChannels;
}
else if(m_pFrameImg->nChannels ==  ONE) //Number of color channels (1,2,3,4)
{
    tempImage =  cvCreateImage(cvGetSize(m_pFrameImg), IPL_DEPTH_8U, THREE);
    cvCvtColor(m_pFrameImg, tempImage, CV_GRAY2BGR);
    bitmapInfo.bmiHeader.biBitCount=tempImage->depth * tempImage->nChannels;
    }
dc.SetStretchBltMode(COLORONCOLOR);
::StretchDIBits(dc.GetSafeHdc(), rect.left, rect.top, rect.right, rect.bottom,
    ZERO, ZERO, tempImage->width, tempImage->height, tempImage->imageData, &bitmapInfo,
    DIB_RGB_COLORS, SRCCOPY);
cvReleaseImage(&tempImage);
//String for giving Camera name
CString sCamName;
sCamName.Format(_T("Video Stream IP Cam-%s"), m_IpAddr);
dc.SetBkMode(OPAQUE);
dc.SetBkColor(MSG_BGCOLOR);
dc.SetTextColor(WHITE);// BGR(0xbbggrr);
dc.TextOut(MSG_TXT_XAXIS, MSG_TXT_YAXIS, sCamName, sCamName.GetLength());
ReleaseDC(&dc);
SetTimer( ONE,30, NULL);

 }
void CStaticEx::OnTimer(UINT_PTR nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(nIDEvent =  ONE){

    Invalidate();
}

 CStatic::OnTimer(nIDEvent);
}

Thanks in Advance. Have a great day.

Please refer the following to capture the frames from live camera. http://www.codeproject.com/Questions/442770/Connect-to-IP-Camera-in-Cplusplus

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM