mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2024-12-23 08:46:40 +00:00
motioncells: new element to detect areas of motion
This commit is contained in:
parent
976f4b0bbf
commit
4723a5d90c
8 changed files with 2403 additions and 2 deletions
|
@ -16,7 +16,12 @@ libgstopencv_la_SOURCES = gstopencv.c \
|
|||
gstfacedetect.c \
|
||||
gstpyramidsegment.c \
|
||||
gsttemplatematch.c \
|
||||
gsttextoverlay.c
|
||||
gsttextoverlay.c \
|
||||
gstmotioncells.c \
|
||||
motioncells_wrapper.cpp \
|
||||
MotionCells.cpp
|
||||
|
||||
libgstopencv_la_CXXFLAGS = $(GST_CXXFLAGS) $(OPENCV_CFLAGS)
|
||||
|
||||
# flags used to compile this facedetect
|
||||
# add other _CFLAGS and _LIBS as needed
|
||||
|
@ -46,4 +51,7 @@ noinst_HEADERS = gstopencvvideofilter.h gstopencvutils.h \
|
|||
gstfacedetect.h \
|
||||
gstpyramidsegment.h \
|
||||
gsttemplatematch.h \
|
||||
gsttextoverlay.h
|
||||
gsttextoverlay.h \
|
||||
gstmotioncells.h \
|
||||
motioncells_wrapper.h \
|
||||
MotionCells.h
|
||||
|
|
593
ext/opencv/MotionCells.cpp
Normal file
593
ext/opencv/MotionCells.cpp
Normal file
|
@ -0,0 +1,593 @@
|
|||
/*
|
||||
* GStreamer
|
||||
* Copyright (C) 2011 Robert Jobbagy <jobbagy.robert@gmail.com>
|
||||
* Copyright (C) 2011 Nicola Murino <nicola.murino@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the
|
||||
* GNU Lesser General Public License Version 2.1 (the "LGPL"), in
|
||||
* which case the following provisions apply instead of the ones
|
||||
* mentioned above:
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#include <cstdlib>
|
||||
#include <errno.h>
|
||||
#include <math.h>
|
||||
#include <gst/gst.h>
|
||||
#include <arpa/inet.h>
|
||||
#include "MotionCells.h"
|
||||
|
||||
uint64_t ntohl64 (uint64_t val);
|
||||
uint64_t htonl64 (uint64_t val);
|
||||
|
||||
uint64_t
|
||||
ntohl64 (uint64_t val)
|
||||
{
|
||||
uint64_t res64;
|
||||
uint32_t low = (uint32_t) (val & 0x00000000FFFFFFFFLL);
|
||||
uint32_t high = (uint32_t) ((val & 0xFFFFFFFF00000000LL) >> 32);
|
||||
low = ntohl (low);
|
||||
high = ntohl (high);
|
||||
res64 = (uint64_t) high + (((uint64_t) low) << 32);
|
||||
return res64;
|
||||
}
|
||||
|
||||
|
||||
uint64_t
|
||||
htonl64 (uint64_t val)
|
||||
{
|
||||
uint64_t res64;
|
||||
uint32_t low = (uint32_t) (val & 0x00000000FFFFFFFFLL);
|
||||
uint32_t high = (uint32_t) ((val & 0xFFFFFFFF00000000LL) >> 32);
|
||||
low = htonl (low);
|
||||
high = htonl (high);
|
||||
res64 = (uint64_t) high + (((uint64_t) low) << 32);
|
||||
return res64;
|
||||
}
|
||||
|
||||
MotionCells::MotionCells ()
|
||||
{
|
||||
m_framecnt = 0;
|
||||
m_motioncells_idx_count = 0;
|
||||
m_motioncellsidxcstr = NULL;
|
||||
m_saveInDatafile = false;
|
||||
mc_savefile = NULL;
|
||||
m_pcurFrame = NULL;
|
||||
m_pprevFrame = NULL;
|
||||
transparencyimg = NULL;
|
||||
m_pdifferenceImage = NULL;
|
||||
m_pbwImage = NULL;
|
||||
m_initdatafilefailed = new char[BUSMSGLEN];
|
||||
m_savedatafilefailed = new char[BUSMSGLEN];
|
||||
m_initerrorcode = 0;
|
||||
m_saveerrorcode = 0;
|
||||
m_alpha = 0.5;
|
||||
m_beta = 0.5;
|
||||
|
||||
}
|
||||
|
||||
MotionCells::~MotionCells ()
|
||||
{
|
||||
if (mc_savefile) {
|
||||
fclose (mc_savefile);
|
||||
mc_savefile = NULL;
|
||||
}
|
||||
delete[]m_initdatafilefailed;
|
||||
delete[]m_savedatafilefailed;
|
||||
if (m_motioncellsidxcstr)
|
||||
delete[]m_motioncellsidxcstr;
|
||||
if (m_pcurFrame)
|
||||
cvReleaseImage (&m_pcurFrame);
|
||||
if (m_pprevFrame)
|
||||
cvReleaseImage (&m_pprevFrame);
|
||||
if (transparencyimg)
|
||||
cvReleaseImage (&transparencyimg);
|
||||
if (m_pdifferenceImage)
|
||||
cvReleaseImage (&m_pdifferenceImage);
|
||||
if (m_pbwImage)
|
||||
cvReleaseImage (&m_pbwImage);
|
||||
}
|
||||
|
||||
int
|
||||
MotionCells::performDetectionMotionCells (IplImage * p_frame,
|
||||
double p_sensitivity, double p_framerate, int p_gridx, int p_gridy,
|
||||
gint64 timestamp_millisec, bool p_isVisible, bool p_useAlpha,
|
||||
int motionmaskcoord_count, motionmaskcoordrect * motionmaskcoords,
|
||||
int motionmaskcells_count, motioncellidx * motionmaskcellsidx,
|
||||
cellscolor motioncellscolor, int motioncells_count,
|
||||
motioncellidx * motioncellsidx, gint64 starttime, char *p_datafile,
|
||||
bool p_changed_datafile, int p_thickness)
|
||||
{
|
||||
|
||||
int sumframecnt = 0;
|
||||
int ret = 0;
|
||||
p_framerate >= 1 ? p_framerate <= 5 ? sumframecnt = 1
|
||||
: p_framerate <= 10 ? sumframecnt = 2
|
||||
: p_framerate <= 15 ? sumframecnt = 3
|
||||
: p_framerate <= 20 ? sumframecnt = 4
|
||||
: p_framerate <= 25 ? sumframecnt = 5 : sumframecnt = 0 : sumframecnt = 0;
|
||||
|
||||
m_framecnt++;
|
||||
m_changed_datafile = p_changed_datafile;
|
||||
if (m_framecnt >= sumframecnt) {
|
||||
m_useAlpha = p_useAlpha;
|
||||
m_gridx = p_gridx;
|
||||
m_gridy = p_gridy;
|
||||
if (m_changed_datafile) {
|
||||
ret = initDataFile (p_datafile, starttime);
|
||||
if (ret != 0)
|
||||
return ret;
|
||||
}
|
||||
|
||||
m_frameSize = cvGetSize (p_frame);
|
||||
m_frameSize.width /= 2;
|
||||
m_frameSize.height /= 2;
|
||||
setMotionCells (m_frameSize.width, m_frameSize.height);
|
||||
m_sensitivity = 1 - p_sensitivity;
|
||||
m_isVisible = p_isVisible;
|
||||
m_pcurFrame = cvCloneImage (p_frame);
|
||||
IplImage *m_pcurgreyImage = cvCreateImage (m_frameSize, IPL_DEPTH_8U, 1);
|
||||
IplImage *m_pprevgreyImage = cvCreateImage (m_frameSize, IPL_DEPTH_8U, 1);
|
||||
IplImage *m_pgreyImage = cvCreateImage (m_frameSize, IPL_DEPTH_8U, 1);
|
||||
IplImage *m_pcurDown =
|
||||
cvCreateImage (m_frameSize, m_pcurFrame->depth, m_pcurFrame->nChannels);
|
||||
IplImage *m_pprevDown = cvCreateImage (m_frameSize, m_pprevFrame->depth,
|
||||
m_pprevFrame->nChannels);
|
||||
m_pbwImage = cvCreateImage (m_frameSize, IPL_DEPTH_8U, 1);
|
||||
cvPyrDown (m_pprevFrame, m_pprevDown);
|
||||
cvCvtColor (m_pprevDown, m_pprevgreyImage, CV_RGB2GRAY);
|
||||
if (m_pprevFrame)
|
||||
cvReleaseImage (&m_pprevFrame);
|
||||
cvPyrDown (m_pcurFrame, m_pcurDown);
|
||||
cvCvtColor (m_pcurDown, m_pcurgreyImage, CV_RGB2GRAY);
|
||||
m_pdifferenceImage = cvCloneImage (m_pcurgreyImage);
|
||||
//cvSmooth(m_pcurgreyImage, m_pcurgreyImage, CV_GAUSSIAN, 3, 0);//TODO camera noise reduce,something smoothing, and rethink runningavg weights
|
||||
|
||||
//Minus the current gray frame from the 8U moving average.
|
||||
cvAbsDiff (m_pprevgreyImage, m_pcurgreyImage, m_pdifferenceImage);
|
||||
|
||||
//Convert the image to black and white.
|
||||
cvAdaptiveThreshold (m_pdifferenceImage, m_pbwImage, 255,
|
||||
CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY_INV, 7);
|
||||
|
||||
// Dilate and erode to get object blobs
|
||||
cvDilate (m_pbwImage, m_pbwImage, NULL, 2);
|
||||
cvErode (m_pbwImage, m_pbwImage, NULL, 2);
|
||||
|
||||
//mask-out the overlay on difference image
|
||||
if (motionmaskcoord_count > 0)
|
||||
performMotionMaskCoords (motionmaskcoords, motionmaskcoord_count);
|
||||
if (motionmaskcells_count > 0)
|
||||
performMotionMask (motionmaskcellsidx, motionmaskcells_count);
|
||||
if (getIsNonZero (m_pbwImage)) { //detect Motion
|
||||
GST_DEBUG ("DETECT MOTION \n");
|
||||
if (m_MotionCells.size () > 0) //it contains previous motioncells what we used when frames dropped
|
||||
m_MotionCells.clear ();
|
||||
if (transparencyimg)
|
||||
cvReleaseImage (&transparencyimg);
|
||||
(motioncells_count > 0) ?
|
||||
calculateMotionPercentInMotionCells (motioncellsidx,
|
||||
motioncells_count)
|
||||
: calculateMotionPercentInMotionCells (motionmaskcellsidx, 0);
|
||||
|
||||
transparencyimg = cvCreateImage (cvGetSize (p_frame), p_frame->depth, 3);
|
||||
cvSetZero (transparencyimg);
|
||||
if (m_motioncellsidxcstr)
|
||||
delete[]m_motioncellsidxcstr;
|
||||
m_motioncells_idx_count = m_MotionCells.size () * MSGLEN; //one motion cell idx: (lin idx : col idx,) it's 4 character except last motion cell idx
|
||||
m_motioncellsidxcstr = new char[m_motioncells_idx_count];
|
||||
char *tmpstr = new char[MSGLEN];
|
||||
for (int i = 0; i < MSGLEN; i++)
|
||||
tmpstr[i] = ' ';
|
||||
for (unsigned int i = 0; i < m_MotionCells.size (); i++) {
|
||||
CvPoint pt1, pt2;
|
||||
pt1.x = m_MotionCells.at (i).cell_pt1.x * 2;
|
||||
pt1.y = m_MotionCells.at (i).cell_pt1.y * 2;
|
||||
pt2.x = m_MotionCells.at (i).cell_pt2.x * 2;
|
||||
pt2.y = m_MotionCells.at (i).cell_pt2.y * 2;
|
||||
if (m_useAlpha && m_isVisible) {
|
||||
cvRectangle (transparencyimg,
|
||||
pt1,
|
||||
pt2,
|
||||
CV_RGB (motioncellscolor.B_channel_value,
|
||||
motioncellscolor.G_channel_value,
|
||||
motioncellscolor.R_channel_value), CV_FILLED);
|
||||
} else if (m_isVisible) {
|
||||
cvRectangle (p_frame,
|
||||
pt1,
|
||||
pt2,
|
||||
CV_RGB (motioncellscolor.B_channel_value,
|
||||
motioncellscolor.G_channel_value,
|
||||
motioncellscolor.R_channel_value), p_thickness);
|
||||
}
|
||||
|
||||
if (i < m_MotionCells.size () - 1) {
|
||||
snprintf (tmpstr, MSGLEN, "%d:%d,", m_MotionCells.at (i).lineidx,
|
||||
m_MotionCells.at (i).colidx);
|
||||
} else {
|
||||
snprintf (tmpstr, MSGLEN, "%d:%d", m_MotionCells.at (i).lineidx,
|
||||
m_MotionCells.at (i).colidx);
|
||||
}
|
||||
if (i == 0)
|
||||
strncpy (m_motioncellsidxcstr, tmpstr, m_motioncells_idx_count);
|
||||
else
|
||||
strcat (m_motioncellsidxcstr, tmpstr);
|
||||
}
|
||||
if (m_MotionCells.size () == 0)
|
||||
strncpy (m_motioncellsidxcstr, " ", m_motioncells_idx_count);
|
||||
|
||||
if (m_useAlpha && m_isVisible) {
|
||||
if (m_MotionCells.size () > 0)
|
||||
blendImages (p_frame, transparencyimg, m_alpha, m_beta);
|
||||
}
|
||||
|
||||
delete[]tmpstr;
|
||||
|
||||
if (mc_savefile && m_saveInDatafile) {
|
||||
ret = saveMotionCells (timestamp_millisec);
|
||||
if (ret != 0)
|
||||
return ret;
|
||||
}
|
||||
} else {
|
||||
m_motioncells_idx_count = 0;
|
||||
if (m_MotionCells.size () > 0)
|
||||
m_MotionCells.clear ();
|
||||
if (transparencyimg)
|
||||
cvReleaseImage (&transparencyimg);
|
||||
}
|
||||
|
||||
m_pprevFrame = cvCloneImage (m_pcurFrame);
|
||||
m_framecnt = 0;
|
||||
if (m_pcurFrame)
|
||||
cvReleaseImage (&m_pcurFrame);
|
||||
if (m_pdifferenceImage)
|
||||
cvReleaseImage (&m_pdifferenceImage);
|
||||
if (m_pcurgreyImage)
|
||||
cvReleaseImage (&m_pcurgreyImage);
|
||||
if (m_pprevgreyImage)
|
||||
cvReleaseImage (&m_pprevgreyImage);
|
||||
if (m_pgreyImage)
|
||||
cvReleaseImage (&m_pgreyImage);
|
||||
if (m_pbwImage)
|
||||
cvReleaseImage (&m_pbwImage);
|
||||
if (m_pprevDown)
|
||||
cvReleaseImage (&m_pprevDown);
|
||||
if (m_pcurDown)
|
||||
cvReleaseImage (&m_pcurDown);
|
||||
if (m_pCells) {
|
||||
for (int i = 0; i < m_gridy; ++i) {
|
||||
delete[]m_pCells[i];
|
||||
}
|
||||
delete[]m_pCells;
|
||||
}
|
||||
|
||||
if (p_framerate <= 5) {
|
||||
if (m_MotionCells.size () > 0)
|
||||
m_MotionCells.clear ();
|
||||
if (transparencyimg)
|
||||
cvReleaseImage (&transparencyimg);
|
||||
}
|
||||
} else { //we do frame drop
|
||||
m_motioncells_idx_count = 0;
|
||||
ret = -2;
|
||||
for (unsigned int i = 0; i < m_MotionCells.size (); i++) {
|
||||
CvPoint pt1, pt2;
|
||||
pt1.x = m_MotionCells.at (i).cell_pt1.x * 2;
|
||||
pt1.y = m_MotionCells.at (i).cell_pt1.y * 2;
|
||||
pt2.x = m_MotionCells.at (i).cell_pt2.x * 2;
|
||||
pt2.y = m_MotionCells.at (i).cell_pt2.y * 2;
|
||||
if (m_useAlpha && m_isVisible) {
|
||||
cvRectangle (transparencyimg,
|
||||
pt1,
|
||||
pt2,
|
||||
CV_RGB (motioncellscolor.B_channel_value,
|
||||
motioncellscolor.G_channel_value,
|
||||
motioncellscolor.R_channel_value), CV_FILLED);
|
||||
} else if (m_isVisible) {
|
||||
cvRectangle (p_frame,
|
||||
pt1,
|
||||
pt2,
|
||||
CV_RGB (motioncellscolor.B_channel_value,
|
||||
motioncellscolor.G_channel_value,
|
||||
motioncellscolor.R_channel_value), p_thickness);
|
||||
}
|
||||
|
||||
}
|
||||
if (m_useAlpha && m_isVisible) {
|
||||
if (m_MotionCells.size () > 0)
|
||||
blendImages (p_frame, transparencyimg, m_alpha, m_beta);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
MotionCells::initDataFile (char *p_datafile, gint64 starttime) //p_date is increased with difference between current and previous buffer ts
|
||||
{
|
||||
MotionCellData mcd;
|
||||
if (strncmp (p_datafile, " ", 1)) {
|
||||
mc_savefile = fopen (p_datafile, "w");
|
||||
if (mc_savefile == NULL) {
|
||||
//fprintf(stderr, "%s %d:initDataFile:fopen:%d (%s)\n", __FILE__, __LINE__, errno,
|
||||
//strerror(errno));
|
||||
strncpy (m_initdatafilefailed, strerror (errno), BUSMSGLEN - 1);
|
||||
m_initerrorcode = errno;
|
||||
return 1;
|
||||
} else {
|
||||
m_saveInDatafile = true;
|
||||
}
|
||||
} else
|
||||
mc_savefile = NULL;
|
||||
bzero (&m_header, sizeof (MotionCellHeader));
|
||||
m_header.headersize = htonl (MC_HEADER);
|
||||
m_header.type = htonl (MC_TYPE);
|
||||
m_header.version = htonl (MC_VERSION);
|
||||
//it needs these bytes
|
||||
m_header.itemsize =
|
||||
htonl ((int) ceil (ceil (m_gridx * m_gridy / 8.0) / 4.0) * 4 +
|
||||
sizeof (mcd.timestamp));
|
||||
m_header.gridx = htonl (m_gridx);
|
||||
m_header.gridy = htonl (m_gridy);
|
||||
m_header.starttime = htonl64 (starttime);
|
||||
|
||||
snprintf (m_header.name, sizeof (m_header.name), "%s %dx%d", MC_VERSIONTEXT,
|
||||
ntohl (m_header.gridx), ntohl (m_header.gridy));
|
||||
m_changed_datafile = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
MotionCells::saveMotionCells (gint64 timestamp_millisec)
|
||||
{
|
||||
|
||||
MotionCellData mc_data;
|
||||
mc_data.timestamp = htonl (timestamp_millisec);
|
||||
mc_data.data = NULL;
|
||||
//There is no datafile
|
||||
if (mc_savefile == NULL)
|
||||
return 0;
|
||||
|
||||
if (ftello (mc_savefile) == 0) {
|
||||
//cerr << "Writing out file header"<< m_header.headersize <<":" << sizeof(MotionCellHeader) << " itemsize:"
|
||||
//<< m_header.itemsize << endl;
|
||||
if (fwrite (&m_header, sizeof (MotionCellHeader), 1, mc_savefile) != 1) {
|
||||
//fprintf(stderr, "%s %d:saveMotionCells:fwrite:%d (%s)\n", __FILE__, __LINE__, errno,
|
||||
//strerror(errno));
|
||||
strncpy (m_savedatafilefailed, strerror (errno), BUSMSGLEN - 1);
|
||||
m_saveerrorcode = errno;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
mc_data.data =
|
||||
(char *) calloc (1,
|
||||
ntohl (m_header.itemsize) - sizeof (mc_data.timestamp));
|
||||
if (mc_data.data == NULL) {
|
||||
//fprintf(stderr, "%s %d:saveMotionCells:calloc:%d (%s)\n", __FILE__, __LINE__, errno,
|
||||
//strerror(errno));
|
||||
strncpy (m_savedatafilefailed, strerror (errno), BUSMSGLEN - 1);
|
||||
m_saveerrorcode = errno;
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < m_MotionCells.size (); i++) {
|
||||
int bitnum =
|
||||
m_MotionCells.at (i).lineidx * ntohl (m_header.gridx) +
|
||||
m_MotionCells.at (i).colidx;
|
||||
int bytenum = (int) floor (bitnum / 8.0);
|
||||
int shift = bitnum - bytenum * 8;
|
||||
mc_data.data[bytenum] = mc_data.data[bytenum] | (1 << shift);
|
||||
//cerr << "Motion Detected " << "line:" << m_MotionCells.at(i).lineidx << " col:" << m_MotionCells.at(i).colidx;
|
||||
//cerr << " bitnum " << bitnum << " bytenum " << bytenum << " shift " << shift << " value " << (int)mc_data.data[bytenum] << endl;
|
||||
}
|
||||
|
||||
if (fwrite (&mc_data.timestamp, sizeof (mc_data.timestamp), 1,
|
||||
mc_savefile) != 1) {
|
||||
//fprintf(stderr, "%s %d:saveMotionCells:fwrite:%d (%s)\n", __FILE__, __LINE__, errno,
|
||||
//strerror(errno));
|
||||
strncpy (m_savedatafilefailed, strerror (errno), BUSMSGLEN - 1);
|
||||
m_saveerrorcode = errno;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (fwrite (mc_data.data,
|
||||
ntohl (m_header.itemsize) - sizeof (mc_data.timestamp), 1,
|
||||
mc_savefile) != 1) {
|
||||
//fprintf(stderr, "%s %d:saveMotionCells:fwrite:%d (%s)\n", __FILE__, __LINE__, errno,
|
||||
//strerror(errno));
|
||||
strncpy (m_savedatafilefailed, strerror (errno), BUSMSGLEN - 1);
|
||||
m_saveerrorcode = errno;
|
||||
return -1;
|
||||
}
|
||||
|
||||
free (mc_data.data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
double
|
||||
MotionCells::calculateMotionPercentInCell (int p_row, int p_col,
|
||||
double *p_cellarea, double *p_motionarea)
|
||||
{
|
||||
double cntpixelsnum = 0;
|
||||
double cntmotionpixelnum = 0;
|
||||
|
||||
int ybegin = floor ((double) p_row * m_cellheight);
|
||||
int yend = floor ((double) (p_row + 1) * m_cellheight);
|
||||
int xbegin = floor ((double) (p_col) * m_cellwidth);
|
||||
int xend = floor ((double) (p_col + 1) * m_cellwidth);
|
||||
int cellw = xend - xbegin;
|
||||
int cellh = yend - ybegin;
|
||||
int cellarea = cellw * cellh;
|
||||
*p_cellarea = cellarea;
|
||||
int thresholdmotionpixelnum = floor ((double) cellarea * m_sensitivity);
|
||||
|
||||
for (int i = ybegin; i < yend; i++) {
|
||||
for (int j = xbegin; j < xend; j++) {
|
||||
cntpixelsnum++;
|
||||
if ((((uchar *) (m_pbwImage->imageData + m_pbwImage->widthStep * i))[j]) >
|
||||
0) {
|
||||
cntmotionpixelnum++;
|
||||
if (cntmotionpixelnum >= thresholdmotionpixelnum) { //we dont needs calculate anymore
|
||||
*p_motionarea = cntmotionpixelnum;
|
||||
return (cntmotionpixelnum / cntpixelsnum);
|
||||
}
|
||||
}
|
||||
int remainingpixelsnum = cellarea - cntpixelsnum;
|
||||
if ((cntmotionpixelnum + remainingpixelsnum) < thresholdmotionpixelnum) { //moving pixels number will be less than threshold
|
||||
*p_motionarea = 0;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (cntmotionpixelnum / cntpixelsnum);
|
||||
}
|
||||
|
||||
void
|
||||
MotionCells::calculateMotionPercentInMotionCells (motioncellidx *
|
||||
p_motioncellsidx, int p_motioncells_count)
|
||||
{
|
||||
if (p_motioncells_count == 0) {
|
||||
for (int i = 0; i < m_gridy; i++) {
|
||||
for (int j = 0; j < m_gridx; j++) {
|
||||
m_pCells[i][j].MotionPercent = calculateMotionPercentInCell (i, j,
|
||||
&m_pCells[i][j].CellArea, &m_pCells[i][j].MotionArea);
|
||||
m_pCells[i][j].hasMotion =
|
||||
m_sensitivity < m_pCells[i][j].MotionPercent ? true : false;
|
||||
if (m_pCells[i][j].hasMotion) {
|
||||
MotionCellsIdx mci;
|
||||
mci.lineidx = i;
|
||||
mci.colidx = j;
|
||||
mci.cell_pt1.x = floor ((double) j * m_cellwidth);
|
||||
mci.cell_pt1.y = floor ((double) i * m_cellheight);
|
||||
mci.cell_pt2.x = floor ((double) (j + 1) * m_cellwidth);
|
||||
mci.cell_pt2.y = floor ((double) (i + 1) * m_cellheight);
|
||||
int w = mci.cell_pt2.x - mci.cell_pt1.x;
|
||||
int h = mci.cell_pt2.y - mci.cell_pt1.y;
|
||||
mci.motioncell = cvRect (mci.cell_pt1.x, mci.cell_pt1.y, w, h);
|
||||
m_MotionCells.push_back (mci);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (int k = 0; k < p_motioncells_count; ++k) {
|
||||
|
||||
int i = p_motioncellsidx[k].lineidx;
|
||||
int j = p_motioncellsidx[k].columnidx;
|
||||
m_pCells[i][j].MotionPercent =
|
||||
calculateMotionPercentInCell (i, j,
|
||||
&m_pCells[i][j].CellArea, &m_pCells[i][j].MotionArea);
|
||||
m_pCells[i][j].hasMotion =
|
||||
m_pCells[i][j].MotionPercent > m_sensitivity ? true : false;
|
||||
if (m_pCells[i][j].hasMotion) {
|
||||
MotionCellsIdx mci;
|
||||
mci.lineidx = p_motioncellsidx[k].lineidx;
|
||||
mci.colidx = p_motioncellsidx[k].columnidx;
|
||||
mci.cell_pt1.x = floor ((double) j * m_cellwidth);
|
||||
mci.cell_pt1.y = floor ((double) i * m_cellheight);
|
||||
mci.cell_pt2.x = floor ((double) (j + 1) * m_cellwidth);
|
||||
mci.cell_pt2.y = floor ((double) (i + 1) * m_cellheight);
|
||||
int w = mci.cell_pt2.x - mci.cell_pt1.x;
|
||||
int h = mci.cell_pt2.y - mci.cell_pt1.y;
|
||||
mci.motioncell = cvRect (mci.cell_pt1.x, mci.cell_pt1.y, w, h);
|
||||
m_MotionCells.push_back (mci);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
MotionCells::performMotionMaskCoords (motionmaskcoordrect * p_motionmaskcoords,
|
||||
int p_motionmaskcoords_count)
|
||||
{
|
||||
CvPoint upperleft;
|
||||
upperleft.x = 0;
|
||||
upperleft.y = 0;
|
||||
CvPoint lowerright;
|
||||
lowerright.x = 0;
|
||||
lowerright.y = 0;
|
||||
for (int i = 0; i < p_motionmaskcoords_count; i++) {
|
||||
upperleft.x = p_motionmaskcoords[i].upper_left_x;
|
||||
upperleft.y = p_motionmaskcoords[i].upper_left_y;
|
||||
lowerright.x = p_motionmaskcoords[i].lower_right_x;
|
||||
lowerright.y = p_motionmaskcoords[i].lower_right_y;
|
||||
cvRectangle (m_pbwImage, upperleft, lowerright, CV_RGB (0, 0, 0),
|
||||
CV_FILLED);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
MotionCells::performMotionMask (motioncellidx * p_motionmaskcellsidx,
|
||||
int p_motionmaskcells_count)
|
||||
{
|
||||
for (int k = 0; k < p_motionmaskcells_count; k++) {
|
||||
int beginy = p_motionmaskcellsidx[k].lineidx * m_cellheight;
|
||||
int beginx = p_motionmaskcellsidx[k].columnidx * m_cellwidth;
|
||||
int endx =
|
||||
(double) p_motionmaskcellsidx[k].columnidx * m_cellwidth + m_cellwidth;
|
||||
int endy =
|
||||
(double) p_motionmaskcellsidx[k].lineidx * m_cellheight + m_cellheight;
|
||||
for (int i = beginy; i < endy; i++)
|
||||
for (int j = beginx; j < endx; j++) {
|
||||
((uchar *) (m_pbwImage->imageData + m_pbwImage->widthStep * i))[j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///BGR if we use only OpenCV
|
||||
//RGB if we use gst+OpenCV
|
||||
void
|
||||
MotionCells::blendImages (IplImage * p_actFrame, IplImage * p_cellsFrame,
|
||||
float p_alpha, float p_beta)
|
||||
{
|
||||
|
||||
int height = p_actFrame->height;
|
||||
int width = p_actFrame->width;
|
||||
int step = p_actFrame->widthStep / sizeof (uchar);
|
||||
int channels = p_actFrame->nChannels;
|
||||
int cellstep = p_cellsFrame->widthStep / sizeof (uchar);
|
||||
uchar *curImageData = (uchar *) p_actFrame->imageData;
|
||||
uchar *cellImageData = (uchar *) p_cellsFrame->imageData;
|
||||
|
||||
for (int i = 0; i < height; i++)
|
||||
for (int j = 0; j < width; j++)
|
||||
for (int k = 0; k < channels; k++)
|
||||
if (cellImageData[i * cellstep + j * channels + k] > 0) {
|
||||
curImageData[i * step + j * channels + k] =
|
||||
round ((double) curImageData[i * step + j * channels +
|
||||
k] * p_alpha + ((double) cellImageData[i * cellstep +
|
||||
j * channels + k] * p_beta));
|
||||
}
|
||||
}
|
259
ext/opencv/MotionCells.h
Normal file
259
ext/opencv/MotionCells.h
Normal file
|
@ -0,0 +1,259 @@
|
|||
/*
|
||||
* GStreamer
|
||||
* Copyright (C) 2011 Robert Jobbagy <jobbagy.robert@gmail.com>
|
||||
* Copyright (C) 2011 Nicola Murino <nicola.murino@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the
|
||||
* GNU Lesser General Public License Version 2.1 (the "LGPL"), in
|
||||
* which case the following provisions apply instead of the ones
|
||||
* mentioned above:
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#ifndef MOTIONCELLS_H_
|
||||
#define MOTIONCELLS_H_
|
||||
|
||||
#include <cv.h> // includes OpenCV definitions
|
||||
#include <highgui.h> // includes highGUI definitions
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
#include <cstdio>
|
||||
#include <cmath>
|
||||
#include <glib.h>
|
||||
|
||||
//MotionCells defines
|
||||
#define MC_HEADER 64
|
||||
#define MC_TYPE 1
|
||||
#define MC_VERSION 1
|
||||
#define MC_VERSIONTEXT "MotionCells-1"
|
||||
#define MSGLEN 6
|
||||
#define BUSMSGLEN 20
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct MotionCellHeader{
|
||||
gint32 headersize;
|
||||
gint32 type;
|
||||
gint32 version;
|
||||
gint32 itemsize;
|
||||
gint32 gridx;
|
||||
gint32 gridy;
|
||||
gint64 starttime;
|
||||
char name[MC_HEADER - 32];
|
||||
};
|
||||
|
||||
struct MotionCellData{
|
||||
gint32 timestamp;
|
||||
char *data;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
int upper_left_x;
|
||||
int upper_left_y;
|
||||
int lower_right_x;
|
||||
int lower_right_y;
|
||||
} motionmaskcoordrect;
|
||||
|
||||
typedef struct {
|
||||
int R_channel_value;
|
||||
int G_channel_value;
|
||||
int B_channel_value;
|
||||
} cellscolor;
|
||||
|
||||
typedef struct {
|
||||
int lineidx;
|
||||
int columnidx;
|
||||
} motioncellidx;
|
||||
|
||||
struct Cell
|
||||
{
|
||||
double MotionArea;
|
||||
double CellArea;
|
||||
double MotionPercent;
|
||||
bool hasMotion;
|
||||
};
|
||||
|
||||
struct MotionCellsIdx
|
||||
{
|
||||
CvRect motioncell;
|
||||
//Points for the edges of the rectangle.
|
||||
CvPoint cell_pt1;
|
||||
CvPoint cell_pt2;
|
||||
int lineidx;
|
||||
int colidx;
|
||||
};
|
||||
|
||||
struct OverlayRegions
|
||||
{
|
||||
CvPoint upperleft;
|
||||
CvPoint lowerright;
|
||||
};
|
||||
|
||||
class MotionCells
|
||||
{
|
||||
public:
|
||||
|
||||
MotionCells ();
|
||||
virtual ~ MotionCells ();
|
||||
|
||||
int performDetectionMotionCells (IplImage * p_frame, double p_sensitivity,
|
||||
double p_framerate, int p_gridx, int p_gridy, gint64 timestamp_millisec,
|
||||
bool p_isVisble, bool p_useAlpha, int motionmaskcoord_count,
|
||||
motionmaskcoordrect * motionmaskcoords, int motionmaskcells_count,
|
||||
motioncellidx * motionmaskcellsidx, cellscolor motioncellscolor,
|
||||
int motioncells_count, motioncellidx * motioncellsidx, gint64 starttime,
|
||||
char *datafile, bool p_changed_datafile, int p_thickness);
|
||||
|
||||
void setPrevFrame (IplImage * p_prevframe)
|
||||
{
|
||||
m_pprevFrame = cvCloneImage (p_prevframe);
|
||||
}
|
||||
char *getMotionCellsIdx ()
|
||||
{
|
||||
return m_motioncellsidxcstr;
|
||||
}
|
||||
|
||||
int getMotionCellsIdxCount ()
|
||||
{
|
||||
return m_motioncells_idx_count;
|
||||
}
|
||||
|
||||
bool getChangedDataFile ()
|
||||
{
|
||||
return m_changed_datafile;
|
||||
}
|
||||
|
||||
char *getDatafileInitFailed ()
|
||||
{
|
||||
return m_initdatafilefailed;
|
||||
}
|
||||
|
||||
char *getDatafileSaveFailed ()
|
||||
{
|
||||
return m_savedatafilefailed;
|
||||
}
|
||||
|
||||
int getInitErrorCode ()
|
||||
{
|
||||
return m_initerrorcode;
|
||||
}
|
||||
|
||||
int getSaveErrorCode ()
|
||||
{
|
||||
return m_saveerrorcode;
|
||||
}
|
||||
|
||||
void freeDataFile ()
|
||||
{
|
||||
if (mc_savefile) {
|
||||
fclose (mc_savefile);
|
||||
mc_savefile = NULL;
|
||||
m_saveInDatafile = false;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
double calculateMotionPercentInCell (int p_row, int p_col, double *p_cellarea,
|
||||
double *p_motionarea);
|
||||
void performMotionMaskCoords (motionmaskcoordrect * p_motionmaskcoords,
|
||||
int p_motionmaskcoords_count);
|
||||
void performMotionMask (motioncellidx * p_motionmaskcellsidx,
|
||||
int p_motionmaskcells_count);
|
||||
void calculateMotionPercentInMotionCells (motioncellidx *
|
||||
p_motionmaskcellsidx, int p_motionmaskcells_count = 0);
|
||||
int saveMotionCells (gint64 timestamp_millisec);
|
||||
int initDataFile (char *p_datafile, gint64 starttime);
|
||||
void blendImages (IplImage * p_actFrame, IplImage * p_cellsFrame,
|
||||
float p_alpha, float p_beta);
|
||||
|
||||
void setData (IplImage * img, int lin, int col, uchar valor)
|
||||
{
|
||||
((uchar *) (img->imageData + img->widthStep * lin))[col] = valor;
|
||||
}
|
||||
|
||||
uchar getData (IplImage * img, int lin, int col)
|
||||
{
|
||||
return ((uchar *) (img->imageData + img->widthStep * lin))[col];
|
||||
}
|
||||
|
||||
bool getIsNonZero (IplImage * img)
|
||||
{
|
||||
for (int lin = 0; lin < img->height; lin++)
|
||||
for (int col = 0; col < img->width; col++) {
|
||||
if ((((uchar *) (img->imageData + img->widthStep * lin))[col]) > 0)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void setMotionCells (int p_frameWidth, int p_frameHeight)
|
||||
{
|
||||
m_cellwidth = (double) p_frameWidth / (double) m_gridx;
|
||||
m_cellheight = (double) p_frameHeight / (double) m_gridy;
|
||||
m_pCells = new Cell *[m_gridy];
|
||||
for (int i = 0; i < m_gridy; i++)
|
||||
m_pCells[i] = new Cell[m_gridx];
|
||||
|
||||
//init cells
|
||||
for (int i = 0; i < m_gridy; i++)
|
||||
for (int j = 0; j < m_gridx; j++) {
|
||||
m_pCells[i][j].MotionArea = 0;
|
||||
m_pCells[i][j].CellArea = 0;
|
||||
m_pCells[i][j].MotionPercent = 0;
|
||||
m_pCells[i][j].hasMotion = false;
|
||||
}
|
||||
}
|
||||
|
||||
IplImage *m_pcurFrame, *m_pprevFrame, *m_pdifferenceImage,
|
||||
*m_pbwImage,*transparencyimg;
|
||||
CvSize m_frameSize;
|
||||
bool m_isVisible, m_changed_datafile, m_useAlpha, m_saveInDatafile;
|
||||
Cell **m_pCells;
|
||||
vector < MotionCellsIdx > m_MotionCells;
|
||||
vector < OverlayRegions > m_OverlayRegions;
|
||||
int m_gridx, m_gridy;
|
||||
double m_cellwidth, m_cellheight;
|
||||
double m_alpha, m_beta;
|
||||
double m_thresholdBoundingboxArea, m_cellArea, m_sensitivity;
|
||||
int m_framecnt, m_motioncells_idx_count, m_initerrorcode, m_saveerrorcode;
|
||||
char *m_motioncellsidxcstr, *m_initdatafilefailed, *m_savedatafilefailed;
|
||||
FILE *mc_savefile;
|
||||
MotionCellHeader m_header;
|
||||
|
||||
};
|
||||
|
||||
#endif /* MOTIONCELLS_H_ */
|
1111
ext/opencv/gstmotioncells.c
Normal file
1111
ext/opencv/gstmotioncells.c
Normal file
File diff suppressed because it is too large
Load diff
124
ext/opencv/gstmotioncells.h
Normal file
124
ext/opencv/gstmotioncells.h
Normal file
|
@ -0,0 +1,124 @@
|
|||
/*
|
||||
* GStreamer
|
||||
* Copyright (C) 2011 Robert Jobbagy <jobbagy.robert@gmail.com>
|
||||
* Copyright (C) 2011 Nicola Murino <nicola.murino@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the
|
||||
* GNU Lesser General Public License Version 2.1 (the "LGPL"), in
|
||||
* which case the following provisions apply instead of the ones
|
||||
* mentioned above:
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#ifndef __GST_MOTIONCELLS_H__
|
||||
#define __GST_MOTIONCELLS_H__
|
||||
|
||||
#include <gst/gst.h>
|
||||
#include <cv.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
/* #defines don't like whitespacey bits */
|
||||
#define GST_TYPE_MOTIONCELLS \
|
||||
(gst_motion_cells_get_type())
|
||||
#define gst_motion_cells(obj) \
|
||||
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_MOTIONCELLS,GstMotioncells))
|
||||
#define gst_motion_cells_CLASS(klass) \
|
||||
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_MOTIONCELLS,GstMotioncellsClass))
|
||||
#define GST_IS_MOTIONCELLS(obj) \
|
||||
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_MOTIONCELLS))
|
||||
#define GST_IS_MOTIONCELLS_CLASS(klass) \
|
||||
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_MOTIONCELLS))
|
||||
typedef struct _GstMotioncells GstMotioncells;
|
||||
typedef struct _GstMotioncellsClass GstMotioncellsClass;
|
||||
|
||||
typedef struct {
|
||||
int upper_left_x;
|
||||
int upper_left_y;
|
||||
int lower_right_x;
|
||||
int lower_right_y;
|
||||
} motionmaskcoordrect;
|
||||
|
||||
typedef struct {
|
||||
int R_channel_value;
|
||||
int G_channel_value;
|
||||
int B_channel_value;
|
||||
} cellscolor;
|
||||
|
||||
typedef struct {
|
||||
int lineidx;
|
||||
int columnidx;
|
||||
} motioncellidx;
|
||||
|
||||
struct _GstMotioncells
|
||||
{
|
||||
GstElement element;
|
||||
GstPad *sinkpad, *srcpad;
|
||||
GstState state;
|
||||
gboolean display, calculate_motion, firstgridx, firstgridy, changed_gridx,
|
||||
changed_gridy, changed_startime;
|
||||
gboolean previous_motion, changed_datafile, postallmotion, usealpha,
|
||||
firstdatafile, firstframe;
|
||||
gboolean sent_init_error_msg, sent_save_error_msg;
|
||||
gchar *prev_datafile, *cur_datafile, *basename_datafile, *datafile_extension;
|
||||
gint prevgridx, gridx, prevgridy, gridy, id;
|
||||
gdouble sensitivity, threshold;
|
||||
IplImage *cvImage;
|
||||
motionmaskcoordrect *motionmaskcoords;
|
||||
cellscolor *motioncellscolor;
|
||||
motioncellidx *motioncellsidx, *motionmaskcellsidx;
|
||||
int motionmaskcoord_count, motioncells_count, motionmaskcells_count;
|
||||
int gap, thickness, datafileidx, postnomotion, minimum_motion_frames;
|
||||
guint64 motion_begin_timestamp, last_motion_timestamp, motion_timestamp,
|
||||
last_nomotion_notified, prev_buff_timestamp, cur_buff_timestamp;
|
||||
gint64 diff_timestamp, starttime;
|
||||
guint64 consecutive_motion;
|
||||
gint width, height;
|
||||
//time stuff
|
||||
struct timeval tv;
|
||||
GMutex *propset_mutex;
|
||||
double framerate;
|
||||
};
|
||||
|
||||
struct _GstMotioncellsClass
|
||||
{
|
||||
GstElementClass parent_class;
|
||||
};
|
||||
|
||||
GType gst_motion_cells_get_type (void);
|
||||
|
||||
gboolean gst_motioncells_plugin_init (GstPlugin * plugin);
|
||||
|
||||
G_END_DECLS
|
||||
#endif /* __GST_MOTION_CELLS_H__ */
|
|
@ -32,6 +32,7 @@
|
|||
#include "gstedgedetect.h"
|
||||
#include "gstfaceblur.h"
|
||||
#include "gstfacedetect.h"
|
||||
#include "gstmotioncells.h"
|
||||
#include "gstpyramidsegment.h"
|
||||
#include "gsttemplatematch.h"
|
||||
#include "gsttextoverlay.h"
|
||||
|
@ -66,6 +67,9 @@ plugin_init (GstPlugin * plugin)
|
|||
if (!gst_facedetect_plugin_init (plugin))
|
||||
return FALSE;
|
||||
|
||||
if (!gst_motioncells_plugin_init (plugin))
|
||||
return FALSE;
|
||||
|
||||
if (!gst_pyramidsegment_plugin_init (plugin))
|
||||
return FALSE;
|
||||
|
||||
|
|
213
ext/opencv/motioncells_wrapper.cpp
Normal file
213
ext/opencv/motioncells_wrapper.cpp
Normal file
|
@ -0,0 +1,213 @@
|
|||
/*
|
||||
* GStreamer
|
||||
* Copyright (C) 2011 Robert Jobbagy <jobbagy.robert@gmail.com>
|
||||
* Copyright (C) 2011 Nicola Murino <nicola.murino@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the
|
||||
* GNU Lesser General Public License Version 2.1 (the "LGPL"), in
|
||||
* which case the following provisions apply instead of the ones
|
||||
* mentioned above:
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
#include "motioncells_wrapper.h"
|
||||
|
||||
extern int instanceCounter;
|
||||
extern bool element_id_was_max;
|
||||
MotionCells *mc;
|
||||
char p_str[] = "idx failed";
|
||||
|
||||
void
|
||||
motion_cells_init ()
|
||||
{
|
||||
mc = new MotionCells ();
|
||||
instanceOfMC tmpmc;
|
||||
tmpmc.id = instanceCounter;
|
||||
tmpmc.mc = mc;
|
||||
motioncellsvector.push_back (tmpmc);
|
||||
if ((instanceCounter < INT_MAX) && !element_id_was_max) {
|
||||
instanceCounter++;
|
||||
element_id_was_max = false;
|
||||
} else {
|
||||
element_id_was_max = true;
|
||||
instanceCounter = motioncellsfreeids.back ();
|
||||
motioncellsfreeids.pop_back ();
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
perform_detection_motion_cells (IplImage * p_image, double p_sensitivity,
|
||||
double p_framerate, int p_gridx, int p_gridy, long int p_timestamp_millisec,
|
||||
bool p_isVisible, bool p_useAlpha, int motionmaskcoord_count,
|
||||
motionmaskcoordrect * motionmaskcoords, int motionmaskcells_count,
|
||||
motioncellidx * motionmaskcellsidx, cellscolor motioncellscolor,
|
||||
int motioncells_count, motioncellidx * motioncellsidx, gint64 starttime,
|
||||
char *p_datafile, bool p_changed_datafile, int p_thickness, int p_id)
|
||||
{
|
||||
int idx = 0;
|
||||
idx = searchIdx (p_id);
|
||||
return motioncellsvector.at (idx).mc->performDetectionMotionCells (p_image,
|
||||
p_sensitivity, p_framerate, p_gridx, p_gridy, p_timestamp_millisec,
|
||||
p_isVisible, p_useAlpha, motionmaskcoord_count, motionmaskcoords,
|
||||
motionmaskcells_count, motionmaskcellsidx, motioncellscolor,
|
||||
motioncells_count, motioncellsidx, starttime, p_datafile,
|
||||
p_changed_datafile, p_thickness);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
setPrevFrame (IplImage * p_prevFrame, int p_id)
|
||||
{
|
||||
int idx = 0;
|
||||
idx = searchIdx (p_id);
|
||||
motioncellsvector.at (idx).mc->setPrevFrame (p_prevFrame);
|
||||
}
|
||||
|
||||
char *
|
||||
getMotionCellsIdx (int p_id)
|
||||
{
|
||||
int idx = 0;
|
||||
idx = searchIdx (p_id);
|
||||
if (idx > -1)
|
||||
return motioncellsvector.at (idx).mc->getMotionCellsIdx ();
|
||||
else {
|
||||
return p_str;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int
|
||||
getMotionCellsIdxCnt (int p_id)
|
||||
{
|
||||
int idx = 0;
|
||||
idx = searchIdx (p_id);
|
||||
if (idx > -1)
|
||||
return motioncellsvector.at (idx).mc->getMotionCellsIdxCount ();
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool
|
||||
getChangedDataFile (int p_id)
|
||||
{
|
||||
int idx = 0;
|
||||
idx = searchIdx (p_id);
|
||||
if (idx > -1)
|
||||
return motioncellsvector.at (idx).mc->getChangedDataFile ();
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
int
|
||||
searchIdx (int p_id)
|
||||
{
|
||||
for (unsigned int i = 0; i < motioncellsvector.size (); i++) {
|
||||
instanceOfMC tmpmc;
|
||||
tmpmc = motioncellsvector.at (i);
|
||||
if (tmpmc.id == p_id) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *
|
||||
getInitDataFileFailed (int p_id)
|
||||
{
|
||||
int idx = 0;
|
||||
idx = searchIdx (p_id);
|
||||
if (idx > -1)
|
||||
return motioncellsvector.at (idx).mc->getDatafileInitFailed ();
|
||||
else {
|
||||
return p_str;
|
||||
}
|
||||
}
|
||||
|
||||
char *
|
||||
getSaveDataFileFailed (int p_id)
|
||||
{
|
||||
int idx = 0;
|
||||
idx = searchIdx (p_id);
|
||||
if (idx > -1)
|
||||
return motioncellsvector.at (idx).mc->getDatafileSaveFailed ();
|
||||
else {
|
||||
return p_str;
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
getInitErrorCode (int p_id)
|
||||
{
|
||||
int idx = 0;
|
||||
idx = searchIdx (p_id);
|
||||
if (idx > -1)
|
||||
return motioncellsvector.at (idx).mc->getInitErrorCode ();
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
getSaveErrorCode (int p_id)
|
||||
{
|
||||
int idx = 0;
|
||||
idx = searchIdx (p_id);
|
||||
if (idx > -1)
|
||||
return motioncellsvector.at (idx).mc->getSaveErrorCode ();
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
void
|
||||
motion_cells_free (int p_id)
|
||||
{
|
||||
int idx = 0;
|
||||
idx = searchIdx (p_id);
|
||||
if (idx > -1) {
|
||||
delete motioncellsvector.at (idx).mc;
|
||||
motioncellsvector.erase (motioncellsvector.begin () + idx);
|
||||
motioncellsfreeids.push_back (p_id);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
motion_cells_free_resources (int p_id)
|
||||
{
|
||||
int idx = 0;
|
||||
idx = searchIdx (p_id);
|
||||
if (idx > -1)
|
||||
motioncellsvector.at (idx).mc->freeDataFile ();
|
||||
}
|
89
ext/opencv/motioncells_wrapper.h
Normal file
89
ext/opencv/motioncells_wrapper.h
Normal file
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
* GStreamer
|
||||
* Copyright (C) 2011 Robert Jobbagy <jobbagy.robert@gmail.com>
|
||||
* Copyright (C) 2011 Nicola Murino <nicola.murino@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"),
|
||||
* to deal in the Software without restriction, including without limitation
|
||||
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the
|
||||
* Software is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
* DEALINGS IN THE SOFTWARE.
|
||||
*
|
||||
* Alternatively, the contents of this file may be used under the
|
||||
* GNU Lesser General Public License Version 2.1 (the "LGPL"), in
|
||||
* which case the following provisions apply instead of the ones
|
||||
* mentioned above:
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Library General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Library General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Library General Public
|
||||
* License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#ifndef MOTIONCELLS_WRAPPER_H
|
||||
#define MOTIONCELLS_WRAPPER_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
#include "MotionCells.h"
|
||||
struct instanceOfMC
|
||||
{
|
||||
int id;
|
||||
MotionCells *mc;
|
||||
};
|
||||
vector < instanceOfMC > motioncellsvector;
|
||||
vector < int >motioncellsfreeids;
|
||||
|
||||
int searchIdx (int p_id);
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
void motion_cells_init ();
|
||||
int perform_detection_motion_cells (IplImage * p_image, double p_sensitivity,
|
||||
double p_framerate, int p_gridx, int p_gridy,
|
||||
long int p_timestamp_millisec, bool p_isVisible, bool p_useAlpha,
|
||||
int motionmaskcoord_count, motionmaskcoordrect * motionmaskcoords,
|
||||
int motionmaskcells_count, motioncellidx * motionmaskcellsidx,
|
||||
cellscolor motioncellscolor, int motioncells_count,
|
||||
motioncellidx * motioncellsidx, gint64 starttime, char *datafile,
|
||||
bool p_changed_datafile, int p_thickness, int p_id);
|
||||
void setPrevFrame (IplImage * p_prevFrame, int p_id);
|
||||
void motion_cells_free (int p_id);
|
||||
void motion_cells_free_resources (int p_id);
|
||||
char *getMotionCellsIdx (int p_id);
|
||||
int getMotionCellsIdxCnt (int p_id);
|
||||
bool getChangedDataFile (int p_id);
|
||||
char *getInitDataFileFailed (int p_id);
|
||||
char *getSaveDataFileFailed (int p_id);
|
||||
int getInitErrorCode (int p_id);
|
||||
int getSaveErrorCode (int p_id);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* MOTIONCELLS_WRAPPER_H */
|
Loading…
Reference in a new issue