You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
3.9 KiB
166 lines
3.9 KiB
|
|
#include "dataMediaImageSeq.h"
|
|
|
|
|
|
imageSequence::imageSequence(){
|
|
head = NULL;
|
|
tail = NULL;
|
|
videoWriter = NULL;
|
|
savePath = NULL;
|
|
//TS = new basicTimeStamp();
|
|
TS = getUnixTimeStamp();
|
|
currentCount = 0;
|
|
saveMode = false;
|
|
maxCount = IMAGE_SEQ_DEFAULT_LENGTH;
|
|
};
|
|
|
|
imageSequence::imageSequence( int frameCount ){
|
|
head = NULL;
|
|
tail = NULL;
|
|
videoWriter = NULL;
|
|
savePath = NULL;
|
|
//TS = new basicTimeStamp();
|
|
TS = getUnixTimeStamp();
|
|
currentCount = 0;
|
|
saveMode = false;
|
|
maxCount = frameCount;
|
|
};
|
|
|
|
imageSequence::~imageSequence(){
|
|
if( currentCount ) {
|
|
if( saveMode ) this->flush();
|
|
};
|
|
if( savePath ) {
|
|
delete( savePath );
|
|
};
|
|
};
|
|
|
|
bool imageSequence::alarm(){
|
|
//TS->setCurrent();
|
|
if( saveCount == 0 ) {
|
|
// Set TS just for New Clean Events.
|
|
TS = getUnixTimeStamp();
|
|
};
|
|
saveCount = maxCount >> 1;
|
|
if( saveCount ) { // If MaxCount too Low ( <3 ) do nothing
|
|
saveMode = true;
|
|
return true;
|
|
};
|
|
return false;
|
|
};
|
|
|
|
bool imageSequence::pushImage( basicMediaImage *Img ){
|
|
bool retVal;
|
|
retVal = false;
|
|
if( Img != NULL ) {
|
|
if( tail == NULL ){
|
|
head = ( imageSequenceNode *) malloc(sizeof( imageSequenceNode ));
|
|
if( head!= NULL ) {
|
|
tail = head;
|
|
tail->frame = Img;
|
|
tail->next = NULL;
|
|
retVal = true;
|
|
currentCount++;
|
|
};
|
|
} else {
|
|
tail->next = ( imageSequenceNode *) malloc(sizeof( imageSequenceNode ));
|
|
if( tail->next != NULL ) {
|
|
tail = tail->next;
|
|
tail->frame = Img;
|
|
tail->next = NULL;
|
|
retVal = true;
|
|
currentCount++;
|
|
};
|
|
};
|
|
};
|
|
if( saveMode ){
|
|
if( head != NULL ){
|
|
imageSequenceNode *tNode;
|
|
tNode = head;
|
|
saveImage( head->frame );
|
|
head = head->next;
|
|
delete( tNode->frame );
|
|
delete( tNode );
|
|
currentCount--;
|
|
};
|
|
};
|
|
return retVal;
|
|
};
|
|
|
|
/*
|
|
void createAlphaMat( cv::Mat &mat ){
|
|
CV_Assert( mat.channels() == 4);
|
|
for (int i = 0; i < mat.rows; ++i) {
|
|
for (int j = 0; j < mat.cols; ++j) {
|
|
Vec4b& bgra = mat.at<Vec4b>(i, j);
|
|
bgra[0] = UCHAR_MAX; // Blue
|
|
bgra[1] = saturate_cast<uchar>((float (mat.cols - j)) / ((float)mat.cols) * UCHAR_MAX); // Green
|
|
bgra[2] = saturate_cast<uchar>((float (mat.rows - i)) / ((float)mat.rows) * UCHAR_MAX); // Red
|
|
bgra[3] = saturate_cast<uchar>(0.5 * (bgra[1] + bgra[2])); // Alpha
|
|
}
|
|
}
|
|
};
|
|
*/
|
|
|
|
void imageSequence::saveImage( basicMediaImage *Img ){
|
|
switch( saveMethod ) {
|
|
case 1:{
|
|
std::vector<int> compression_params;
|
|
compression_params.push_back( CV_IMWRITE_PNG_COMPRESSION );
|
|
compression_params.push_back( 9 );
|
|
try {
|
|
imwrite( "alpha.png", *Img->getMatHandle(), compression_params );
|
|
//} catch ( std::runtime_error &ex ){
|
|
} catch ( ... ){
|
|
//fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
|
|
return;
|
|
};
|
|
break;
|
|
};
|
|
case 2:{ // Save Image just after trigger.
|
|
break;
|
|
};
|
|
case 3:{ // Always save video stream
|
|
if( videoWriter ) {
|
|
if( videoWriter->isOpened() ) {
|
|
videoWriter->write( *Img->getMatHandle() );
|
|
} else {
|
|
if ( videoWriter->open( savePath, CV_FOURCC('M','J','P','G'), framePerSecond, frameSize, true ) ){
|
|
videoWriter->write( *Img->getMatHandle() );
|
|
};
|
|
};
|
|
};
|
|
break;
|
|
};
|
|
};
|
|
};
|
|
|
|
bool imageSequence::flush( void ){
|
|
return true;
|
|
};
|
|
|
|
void imageSequence::setSaveMethod( U8 Method, CSTR Destination ){
|
|
switch( Method ) {
|
|
case 1:
|
|
if( videoWriter ) {
|
|
delete( videoWriter );
|
|
videoWriter = NULL;
|
|
};
|
|
break;
|
|
case 2:
|
|
videoWriter = new cv::VideoWriter();
|
|
break;
|
|
case 3:
|
|
char fileName[1024];
|
|
|
|
framePerSecond = 10.0;
|
|
frameSize = cv::Size(640,480);
|
|
savePath = createTimeHashPath( Destination, TS, fileName );
|
|
videoWriter = new cv::VideoWriter( savePath, CV_FOURCC('M','J','P','G'), framePerSecond, frameSize, true );
|
|
break;
|
|
};
|
|
};
|
|
|
|
|
|
//CV_FOURCC('P','I','M','1')
|
|
//CV_FOURCC('M','J','P','G')
|