From 3191f8a00b2e4471d6d2d9c1b328f4ca690b2b99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Charette?= Date: Fri, 20 Feb 2026 18:57:25 -0800 Subject: [PATCH] comments and #error in unused Darknet/YOLO files --- Projects/img_mill/IM_DnnWorker.cpp | 9 +- Projects/img_mill/IM_DnnWorker.h | 7 +- Projects/img_slim/IM_DLibWorker.cpp | 23 +- Projects/img_slim/IM_DnnWorker.cpp | 7 +- Projects/img_slim/IM_DnnWorker.h | 5 +- lib/App/common/kernelModuleEmbedVideo.cpp | 14 +- lib/App/mojo/mojoKernel.cpp | 69 +- lib/alg/generic/md5.cpp | 19 +- lib/components/base/basicPosition.cpp | 14 +- lib/data/cortex/base/basicImage.cpp | 3068 +++++++++++---------- lib/data/darknet/darknetWraper.cpp | 4 + lib/data/media/video/videoAVLIBSource.cpp | 26 - 12 files changed, 1611 insertions(+), 1654 deletions(-) diff --git a/Projects/img_mill/IM_DnnWorker.cpp b/Projects/img_mill/IM_DnnWorker.cpp index 99efb59e..6ada16aa 100644 --- a/Projects/img_mill/IM_DnnWorker.cpp +++ b/Projects/img_mill/IM_DnnWorker.cpp @@ -1,3 +1,6 @@ +#error "This source file seems to be unused. 2026-02-20" + + #include "IM_DnnWorker.h" #include "../../lib/components/base/basicFunctionality.h" @@ -196,7 +199,7 @@ bool IM_DnnWorker::create( void ){ LOG_ERROR("Failure during DarkNet Detector Creation!"); }; #endif - + } catch ( exception e ) { LOG_ERROR( e.what() ); }; @@ -272,7 +275,7 @@ IM_RESULT *IM_DnnWorker::m_fImageAnalyzer( Mat &Picture ){ #else std::shared_ptr img = m_pDarknetDetector->mat_to_image_resize( Picture ); std::vector result_vec; - //result_vec = detector.detect_resized( * img, inpSize.width, inpSize.height, thresh, true); + //result_vec = detector.detect_resized( * img, inpSize.width, inpSize.height, thresh, true); result_vec = m_pDarknetDetector->detect_resized( *img, inpSize.width, inpSize.height, m_fDetectionProbability, true); if( result_vec.size() ) { retVal = (IM_RESULT *)malloc(sizeof(IM_RESULT)); @@ -324,7 +327,7 @@ void IM_DnnWorker::m_fThreadProc( void ){ EVT_PTR->m_eType = EVT_READY; if( IM_RESULT *tData = m_fImageAnalyzer( m_oWorkingMat ) ){ //generate detection event - EVT_PTR->m_pEvtData = tData; + EVT_PTR->m_pEvtData = tData; } else { //generate NOT detection event EVT_PTR->m_pEvtData = NULL; diff --git a/Projects/img_mill/IM_DnnWorker.h b/Projects/img_mill/IM_DnnWorker.h index f2677a03..bc6d168d 100644 --- a/Projects/img_mill/IM_DnnWorker.h +++ b/Projects/img_mill/IM_DnnWorker.h @@ -1,3 +1,6 @@ +#error "This source file seems to be unused. 2026-02-20" + + #ifndef __IM_DNNWORKER_H_INCL__ #define __IM_DNNWORKER_H_INCL__ @@ -14,7 +17,7 @@ #ifndef GPU #define DIRECT_DARKNET #endif - + #ifdef DIRECT_DARKNET //#include #include "../../lib/data/darknet/darknetWraper.h" @@ -35,7 +38,7 @@ using namespace cv; /** * TODO: Add class description - * + * * @author Gagua */ class IM_DnnWorker : public basicThread { diff --git a/Projects/img_slim/IM_DLibWorker.cpp b/Projects/img_slim/IM_DLibWorker.cpp index b45eac56..2d3e9ccf 100644 --- a/Projects/img_slim/IM_DLibWorker.cpp +++ b/Projects/img_slim/IM_DLibWorker.cpp @@ -1,3 +1,6 @@ +#error "This source file seems to be unused. 2026-02-20" + + #include "IM_DLibWorker.h" #include #include "../../lib/alg/generic/md5.h" @@ -129,7 +132,7 @@ bool IM_DlibWorker::create( void ){ LOG("No CHOWN GROUP! Using default!"); }; sprintf( m_sChownGroup, "%s", config_setting_get_string(tSetting)); - + /* tSetting = config_setting_get_member( m_pSettings, "USE_CLASS_MAP" ); @@ -209,8 +212,8 @@ bool IM_DlibWorker::create( void ){ if( strstr( config_setting_get_string(tSetting), "VKCOM" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_VKCOM; if( strstr( config_setting_get_string(tSetting), "CUDA" )!=NULL ) m_eBackendEngine = cv::dnn::DNN_BACKEND_CUDA; sprintf( tBuff, "Engine: %s", config_setting_get_string(tSetting) ); - LOG( tBuff ); - */ + LOG( tBuff ); + */ basicSettingsProvider::getInstance()->validate(); }; if( m_bUseShapePredictor ) { @@ -254,7 +257,7 @@ std::vector> jitter_image( const matrix& img ){ // mirrored left to right. thread_local dlib::rand rnd; - std::vector> crops; + std::vector> crops; for (int i = 0; i < 100; ++i) crops.push_back(jitter_image(img,rnd)); @@ -266,7 +269,7 @@ CSTR IM_DlibWorker::m_fSaveFace( matrix &Picture, CSTR md5, uint32_t char tPathDest[1024]; CSTR nMd5; CSTR nPath; - sprintf( tPathSrc, "%s/%s_%d.jpg", m_pFacesBase, md5, index ); + sprintf( tPathSrc, "%s/%s_%d.jpg", m_pFacesBase, md5, index ); LOG("Save Face"); LOG( tPathSrc ); save_jpeg( Picture, tPathSrc, 75 ); @@ -289,7 +292,7 @@ IM_RESULT *IM_DlibWorker::m_fImageAnalyzer( cv::Mat &Pic ){ uint32_t idx = 0; CSTR tVAL = NULL; for( auto face : m_oFaceDetector( Picture ) ){ - //dlib::rectangle face + //dlib::rectangle face if( m_bUseShapePredictor ) { auto shape = m_oShapePredictor( Picture, face ); matrix face_chip; @@ -342,7 +345,7 @@ IM_RESULT *IM_DlibWorker::m_fImageAnalyzer( cv::Mat &Pic ){ } else { LOG("No Faces Found!"); }; - + //std::vector> face_descriptors = m_oNet( faces ); // In particular, one simple thing we can do is face clustering. This next bit of code @@ -368,7 +371,7 @@ IM_RESULT *IM_DlibWorker::m_fImageAnalyzer( cv::Mat &Pic ){ // Now let's display the face clustering results on the screen. You will see that it - // correctly grouped all the faces. + // correctly grouped all the faces. /* std::vector win_clusters(num_clusters); for( size_t cluster_id = 0; cluster_id < num_clusters; ++cluster_id ){ @@ -380,7 +383,7 @@ IM_RESULT *IM_DlibWorker::m_fImageAnalyzer( cv::Mat &Pic ){ win_clusters[cluster_id].set_image(tile_images(temp)); }; - // Finally, let's print one of the face descriptors to the screen. + // Finally, let's print one of the face descriptors to the screen. cout << "face descriptor for one face: " << trans(face_descriptors[0]) << endl; // It should also be noted that face recognition accuracy can be improved if jittering @@ -412,7 +415,7 @@ void IM_DlibWorker::m_fThreadProc( void ){ EVT_PTR->m_eType = EVT_READY; if( IM_RESULT *tData = m_fImageAnalyzer( m_oWorkingMat ) ){ //generate detection event - EVT_PTR->m_pEvtData = tData; + EVT_PTR->m_pEvtData = tData; } else { //generate NOT detection event EVT_PTR->m_pEvtData = NULL; diff --git a/Projects/img_slim/IM_DnnWorker.cpp b/Projects/img_slim/IM_DnnWorker.cpp index 148d9913..7c8a8308 100644 --- a/Projects/img_slim/IM_DnnWorker.cpp +++ b/Projects/img_slim/IM_DnnWorker.cpp @@ -1,3 +1,6 @@ +#error "This source file seems to be unused. 2026-02-20" + + #include "IM_DnnWorker.h" #include "../../lib/components/base/basicFunctionality.h" @@ -234,7 +237,7 @@ IM_RESULT *IM_DnnWorker::m_fImageAnalyzer( Mat &Picture ){ #else std::shared_ptr img = m_pDarknetDetector->mat_to_image_resize( Picture ); std::vector result_vec; - //result_vec = detector.detect_resized( * img, inpSize.width, inpSize.height, thresh, true); + //result_vec = detector.detect_resized( * img, inpSize.width, inpSize.height, thresh, true); result_vec = m_pDarknetDetector->detect_resized( *img, inpSize.width, inpSize.height, m_fDetectionProbability, true); if( result_vec.size() ) { retVal = (IM_RESULT *)malloc(sizeof(IM_RESULT)); @@ -285,7 +288,7 @@ void IM_DnnWorker::m_fThreadProc( void ){ EVT_PTR->m_eType = EVT_READY; if( IM_RESULT *tData = m_fImageAnalyzer( m_oWorkingMat ) ){ //generate detection event - EVT_PTR->m_pEvtData = tData; + EVT_PTR->m_pEvtData = tData; } else { //generate NOT detection event EVT_PTR->m_pEvtData = NULL; diff --git a/Projects/img_slim/IM_DnnWorker.h b/Projects/img_slim/IM_DnnWorker.h index 41fc07f4..1102d90d 100644 --- a/Projects/img_slim/IM_DnnWorker.h +++ b/Projects/img_slim/IM_DnnWorker.h @@ -1,3 +1,6 @@ +#error "This source file seems to be unused. 2026-02-20" + + #ifndef __IM_DNNWORKER_H_INCL__ #define __IM_DNNWORKER_H_INCL__ @@ -25,7 +28,7 @@ using namespace cv; /** * TODO: Add class description - * + * * @author Gagua */ class IM_DnnWorker : public basicThread { diff --git a/lib/App/common/kernelModuleEmbedVideo.cpp b/lib/App/common/kernelModuleEmbedVideo.cpp index c19d1df5..f70108c7 100644 --- a/lib/App/common/kernelModuleEmbedVideo.cpp +++ b/lib/App/common/kernelModuleEmbedVideo.cpp @@ -1999,19 +1999,7 @@ bool streamHandler::pushFrame( AVFrame *Frame, void* src){ bool streamHandler::pushFrame( cv::Mat &Frame, void* src ){ - /** @todo Valgrind is reporting an important memory leak in this method. The @p malloc() call on line 2568 (and others, such as 2576). - * The details are as follows: - * - * ~~~~ - = =00:00:04:04.219 26021== 8,805,208 (4,416 direct, 8,800,792 *indirect) bytes in 184 blocks are definitely lost in loss record 3,972 of 3,972 - ==00:00:04:04.219 26021== at 0x483877F: malloc (vg_replace_malloc.c:307) - ==00:00:04:04.219 26021== by 0x1B0FF9: streamHandler::pushFrame(cv::Mat&, void*) (kernelModuleEmbedVideo.cpp:2568) - ==00:00:04:04.219 26021== by 0x2490CB: videoAVLIBSource::m_fThreadProc() (videoAVLIBSource.cpp:874) - ==00:00:04:04.219 26021== by 0x131B54: basicThread::m_fThreadEntryPoint(void*) (basicThread.cpp:62) - ==00:00:04:04.219 26021== by 0x49B7EA6: start_thread (pthread_create.c:477) - ==00:00:04:04.219 26021== by 0x7874ADE: clone (clone.S:95) - * ~~~~ - */ + /// @todo Valgrind is reporting an important memory leak in this method. bool SkeepW, SkeepA, SkeepB; if( m_pMaster == NULL ) return false; diff --git a/lib/App/mojo/mojoKernel.cpp b/lib/App/mojo/mojoKernel.cpp index 2527aa6c..b8243480 100644 --- a/lib/App/mojo/mojoKernel.cpp +++ b/lib/App/mojo/mojoKernel.cpp @@ -25,7 +25,7 @@ #include "../common/kernelEventFilterAPF.h" #include "../common/kernelEventFilterTrain.h" #include "../common/kernelEventFilterBubble.h" -#include "../common/kernelModuleEmbedVideo.h" +#include "../common/kernelModuleEmbedVideo.h" #ifdef USE_MYSQL @@ -49,7 +49,7 @@ mojoKernel *mojoKernel::_instance = NULL; mojoKernel *mojoKernel::getInstance( config_setting_t *ptr ){ - if ( _instance == NULL ) { + if ( _instance == NULL ) { _instance = new mojoKernel( ptr ); }; return _instance; @@ -230,7 +230,7 @@ bool mojoKernel::create(){ if ( m_bDebugMode ) m_bKeepLocalReport = true; //Forced in case of debug mode m_bWorkWithRemoteServer = m_sSiteData.use_remote_server; - + //Check date/Time if correct! // Just in case that we have local reports if ( m_bKeepLocalReport ) { @@ -293,9 +293,9 @@ bool mojoKernel::create(){ #ifdef DEBUG LOG("Working With Remote Server"); #endif - if ( m_pRPC = new mojoRemoteClient( this ) ) { + if ( m_pRPC = new mojoRemoteClient( this ) ) { if( m_bWorkWithRemoteServer = m_pRPC->create() ){ - + } else LOG_ERROR("Cannot Connect to Remote Server!"); } else m_bWorkWithRemoteServer = false; }; @@ -327,7 +327,7 @@ void mojoKernel::startSession(){ m_mSessionProtector.unlock(); }; -void mojoKernel::stopSession( bool forced ){ +void mojoKernel::stopSession( bool forced ){ m_mSessionProtector.lock(); if( m_bSessionStarted ){ uint64_t TS_64 = getMilliseconds() + 20; // +20 msec after @@ -643,7 +643,7 @@ void mojoKernel::m_fThreadProc( void ){ m_bThreadStatus = TH_ENDED; setExitCause( EXIT_CODE_KERNEL_CLEAR ); }; -#else +#else timespec ts; ts.tv_sec = (time_t)uts; ts.tv_nsec = 0; @@ -697,7 +697,7 @@ void mojoKernel::m_fThreadProc( void ){ }; void mojoKernel::loadFilters(){ - if( m_pDBC == NULL ) return; + if( m_pDBC == NULL ) return; DBC_INT_LIST *tList = m_pDBC->listFilters( m_nServerID ); if( tList != NULL ) { if( tList->size() > 0 ) { @@ -826,14 +826,14 @@ void mojoKernel::validateFilters(){ sprintf( tBuffer, "Unknown Filter: %d", (*idx)->getType() ); LOG( tBuffer ); }; - }; - */ + }; + */ }; }; }; }; -bool mojoKernel::dispatchEvent( EVENT_CONTAINER *Evt ){ +bool mojoKernel::dispatchEvent( EVENT_CONTAINER *Evt ){ if( Evt == NULL ) return false; if( m_bWorkWithRemoteServer && ( m_pRPC!=NULL) ) m_pRPC->addEvent( Evt ); if( !m_bKeepLocalReport ) return true; @@ -855,7 +855,7 @@ bool mojoKernel::dispatchEvent( EVENT_CONTAINER *Evt ){ break; }; }; - break; + break; /* case EVT_CLASS_EXTERNAL_CMD: switch( Evt->m_eType ){ @@ -868,9 +868,9 @@ bool mojoKernel::dispatchEvent( EVENT_CONTAINER *Evt ){ #endif FH_CTRL_DATA * tData = (FH_CTRL_DATA *) Evt->m_pEvtData; if( tData ) { - if( m_pDBC->addEvent( + if( m_pDBC->addEvent( 0, - Evt->m_UUID, + Evt->m_UUID, TS_A, TS_B, EVT_FH_JAMER_ON, @@ -914,9 +914,9 @@ bool mojoKernel::dispatchEvent( EVENT_CONTAINER *Evt ){ uint32_t TS_A, TS_B; TS_A = Evt->m_UMSTS / 1000; TS_B = Evt->m_UMSTS % 1000; - if( m_pDBC->addEvent( + if( m_pDBC->addEvent( 0, - Evt->m_UUID, + Evt->m_UUID, TS_A, TS_B, EVT_FH_LANDED ) ){ @@ -957,9 +957,9 @@ bool mojoKernel::dispatchEvent( EVENT_CONTAINER *Evt ){ uint32_t TS_A, TS_B; TS_A = Evt->m_UMSTS / 1000; TS_B = Evt->m_UMSTS % 1000; - if( Evt->m_db_id = m_pDBC->addEvent( + if( Evt->m_db_id = m_pDBC->addEvent( 0, - Evt->m_UUID, + Evt->m_UUID, TS_A, TS_B, EVT_FH_FLY_TO, @@ -1120,7 +1120,7 @@ bool mojoKernel::dispatchEvent( EVENT_CONTAINER *Evt ){ //startSession(); preserveSession(); - TARGET_DATA *tDATA = (TARGET_DATA *) Evt->m_pEvtData; + TARGET_DATA *tDATA = (TARGET_DATA *) Evt->m_pEvtData; if( tDATA != NULL ) { DBC_EVT_RECORD tRecord; memset( &tRecord, 0, sizeof(DBC_EVT_RECORD)); @@ -1164,7 +1164,7 @@ bool mojoKernel::dispatchEvent( EVENT_CONTAINER *Evt ){ case EVT_OP_OFF_MASK: case EVT_CONNECTED: case EVT_DISCONNECTED:{ - + DBC_EVT_RECORD tRecord; memset( &tRecord, 0, sizeof(DBC_EVT_RECORD)); uint32_t TS_A, TS_B; @@ -1194,9 +1194,9 @@ bool mojoKernel::dispatchEvent( EVENT_CONTAINER *Evt ){ break; }; }; - break; + break; case EVT_CLASS_KRNL_FILTER_LATF:{ - TARGET_DATA *tDATA = (TARGET_DATA *) Evt->m_pEvtData; + TARGET_DATA *tDATA = (TARGET_DATA *) Evt->m_pEvtData; if ( ( Evt->m_eType > EVT_FLT_LATF_DIFF_DET ) || ( tDATA == NULL ) ) { //AUX messages (no additional target data) DBC_EVT_RECORD tRecord; @@ -1211,7 +1211,7 @@ bool mojoKernel::dispatchEvent( EVENT_CONTAINER *Evt ){ strncpy( tRecord.uuid, Evt->m_UUID, 64 ); tRecord.ts_s = TS_A; tRecord.ts_ms = TS_B; - + Evt->m_db_id = m_pDBC->addEvent( &tRecord ); } else { //If First Suspiciouse -> Start session to collect Fliter Messages @@ -1254,7 +1254,7 @@ bool mojoKernel::dispatchEvent( EVENT_CONTAINER *Evt ){ preserveSession(); - TARGET_DATA *tDATA = (TARGET_DATA *) Evt->m_pEvtData; + TARGET_DATA *tDATA = (TARGET_DATA *) Evt->m_pEvtData; DBC_EVT_RECORD tRecord; memset( &tRecord, 0, sizeof(DBC_EVT_RECORD)); uint32_t TS_A, TS_B; @@ -1314,13 +1314,13 @@ typedef struct S_VIDEO_SEGMENT_INFO{ char scratchPath[PATH_MAX]; uint32_t dataSize; uint32_t db_id; -}VIDEO_SEGMENT_INFO; - +}VIDEO_SEGMENT_INFO; + */ -void mojoKernel::handleVideo( VIDEO_SEGMENT_INFO *ptr ){ +void mojoKernel::handleVideo( VIDEO_SEGMENT_INFO *ptr ){ if ( fileExist( ptr->scratchPath ) ) { bool bKeepVideoFile = false; - if( ptr->recordMD5[0] ) { + if( ptr->recordMD5[0] ) { if( m_bKeepLocalReport ) { //if wm is debug -> add all to database, else send to server ptr->db_id = m_pDBC->addVideoRecord( ptr->stream_id, ptr->recordMD5, ptr->recordEXT, ptr->streamTSS, ptr->streamTMS, ptr->streamFPS ); @@ -1337,7 +1337,7 @@ void mojoKernel::handleVideo( VIDEO_SEGMENT_INFO *ptr ){ LOG("OK"); #endif }; - }; + }; CSTR tP = m_pDBC->getCurrentReportDir(); if ( tP ) { char tB[MAX_PATH]; @@ -1370,12 +1370,14 @@ void mojoKernel::handleVideo( VIDEO_SEGMENT_INFO *ptr ){ }; bool mojoKernel::validateReports( void ){ + /// @todo Stephane needs to follow this function... + if ( m_bKeepLocalReport ) { DBC_SITE_DATA *tSiteData = m_pDBC->getSiteData(1); if( tSiteData ) { // Check if settings has been changed in DB if ( tSiteData->report_cleanup != m_sSiteData.report_cleanup || tSiteData->preserve_empty != m_sSiteData.preserve_empty ) { - memcpy( &m_sSiteData, tSiteData, sizeof( DBC_SITE_DATA )); + memcpy( &m_sSiteData, tSiteData, sizeof( DBC_SITE_DATA )); }; free( tSiteData ); } else { @@ -1421,8 +1423,8 @@ void mojoKernel::backupReports( uint32_t days ){ sprintf( tB, "%s/data/reports/%s/%s/%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY ); char tPATH[MAX_PATH]; sprintf( tPATH, "%s/data/archive/%s_%s_%s", getcwd(dB, MAX_PATH), tRDI->YEAR, tRDI->MONTH, tRDI->DAY ); - if ( fileExist( tB ) ) { - if ( !fileExist( tPATH ) ) { + if ( fileExist( tB ) ) { + if ( !fileExist( tPATH ) ) { // Try Create Job if sucess then create dir to skip next validation if( createCronJob( 0, tPATH, cTS ) ){ // Create Backup Dir @@ -1447,7 +1449,7 @@ void mojoKernel::cleanUpReportDir( uint32_t days ){ // Cleanup Current reports no Backup uint32_t oTS = m_pDBC->getFirstEventTS(); uint32_t cTS = (uint32_t)time(NULL); - cTS -= 86400 * days; + cTS -= 86400 * days; #ifdef DEBUG_EXTRA char tB[128]; sprintf( tB, "Oldest %d border %d", oTS, cTS ); @@ -1457,6 +1459,7 @@ void mojoKernel::cleanUpReportDir( uint32_t days ){ #ifdef DEBUG_EXTRA LOG("Deleting Old Reports"); #endif + /// @todo Stephane needs to see when this gets called... m_pDBC->deleteReportDate( oTS ); }; }; diff --git a/lib/alg/generic/md5.cpp b/lib/alg/generic/md5.cpp index ed68f131..289a8e85 100644 --- a/lib/alg/generic/md5.cpp +++ b/lib/alg/generic/md5.cpp @@ -81,24 +81,7 @@ char* PrintMD5(u_char md5Digest[16]) strncat(chBuffer, chEach, sizeof(chEach)); }; #ifdef __linux__ - /** @todo Valgrind tells us this @p strdup() called from @ref videoAVLIBSource is never freed. - * - * ~~~~ - = =00:00:04:04.006 26021== 33 bytes in 1 blocks are definitely* lost in loss record 2,412 of 3,972 - ==00:00:04:04.006 26021== at 0x483877F: malloc (vg_replace_malloc.c:307) - ==00:00:04:04.006 26021== by 0x7802C3A: strdup (strdup.c:42) [now line 102 since I've inserted this comment] - ==00:00:04:04.006 26021== by 0x1D20BD: PrintMD5(unsigned char*) (md5.cpp:84) - ==00:00:04:04.006 26021== by 0x1D212A: MD5String(char*) (md5.cpp:100) - ==00:00:04:04.006 26021== by 0x2476EA: videoAVLIBSource::_open(char*, bool, float, int, int, int, int, int) (videoAVLIBSource.cpp:388) - ==00:00:04:04.006 26021== by 0x1AE949: streamHandler::create(dataBinder*, unsigned int) (kernelModuleEmbedVideo.cpp:1949) - ==00:00:04:04.006 26021== by 0x1ADA24: kernelModuleEmbedVideo::registerStream(unsigned int, unsigned char) (kernelModuleEmbedVideo.cpp:1731) - ==00:00:04:04.006 26021== by 0x1AC2A9: kernelModuleEmbedVideo::validate() (kernelModuleEmbedVideo.cpp:1347) - ==00:00:04:04.006 26021== by 0x1BB1E7: kernelModuleFactory::m_fThreadProc() (kernelModuleFactory.cpp:397) - ==00:00:04:04.006 26021== by 0x131B54: basicThread::m_fThreadEntryPoint(void*) (basicThread.cpp:62) - ==00:00:04:04.006 26021== by 0x49B7EA6: start_thread (pthread_create.c:477) - ==00:00:04:04.006 26021== by 0x7874ADE: clone (clone.S:95) - * ~~~~ - */ + /// @todo Valgrind tells us this @p strdup() called from @ref videoAVLIBSource is never freed. return strdup(chBuffer); #else return _strdup(chBuffer); diff --git a/lib/components/base/basicPosition.cpp b/lib/components/base/basicPosition.cpp index 83f80002..e0b99071 100644 --- a/lib/components/base/basicPosition.cpp +++ b/lib/components/base/basicPosition.cpp @@ -217,19 +217,7 @@ void basicPosition::setPositionDeg(float Lat, float Lon, float Alt, float h_lat, void basicPosition::updateMatrix( void ){ - /** @todo Valgrind is showing several memory leaks from this function. - * - * ~~~~ - ==00:00:04:04.173 26021== 176 bytes in 2 blocks are definitely lost in loss record 3,541 of 3,972 - ==00:00:04:04.173 26021== at 0x4838DEF: operator new(unsigned long) (vg_replace_malloc.c:342) - ==00:00:04:04.173 26021== by 0x130F5C: basicPosition::updateMatrix() (basicPosition.cpp:232) - ==00:00:04:04.173 26021== by 0x167C58: deviceNodeSensor::reloadDeviceNodeFromDB() (deviceNodeSensor.cpp:298) - ==00:00:04:04.173 26021== by 0x169DE2: deviceNodeSensor::m_fThreadProc() (deviceNodeSensor.cpp:1075) - ==00:00:04:04.173 26021== by 0x131B54: basicThread::m_fThreadEntryPoint(void*) (basicThread.cpp:62) - ==00:00:04:04.173 26021== by 0x49B7EA6: start_thread (pthread_create.c:477) - ==00:00:04:04.173 26021== by 0x7874ADE: clone (clone.S:95) - * ~~~~ - */ + /// @todo Valgrind is showing several memory leaks from this function. //Tait-Bryan angles //X1Y2Z3 diff --git a/lib/data/cortex/base/basicImage.cpp b/lib/data/cortex/base/basicImage.cpp index 8b135de0..9b21980d 100644 --- a/lib/data/cortex/base/basicImage.cpp +++ b/lib/data/cortex/base/basicImage.cpp @@ -1,1536 +1,1538 @@ -//---------------------------------------------------------------------------- -// Dr.Frucht Systems Ltd. -// Copyright © 2000-2017. All Rights Reserved. -// Version 2.0 9/14/2017 -// Last modification: 9/14/2017 -// PROJECT: -// SUBSYSTEM: -// FILE: basicImage.cpp -// AUTHOR: gagua -// OVERVIEW -// ~~~~~~~~ -// Header file of basicImage.cpp -//---------------------------------------------------------------------------- -// - - -#include "basicImage.h" -//#include "utils.h" -//#include "blas.h" -//#include "dn_cuda.h" - -#define STB_IMAGE_IMPLEMENTATION -#include "../../media/pictures/stb_image.h" -#define STB_IMAGE_WRITE_IMPLEMENTATION -#include "../../media/pictures/stb_image_write.h" - -const uint16_t cortexCOLORS[6][3] = { - { 1, 0, 1 }, - { 0, 0, 1 }, - { 0, 1, 1 }, - - { 0, 1, 0 }, - { 1, 1, 0 }, - { 1, 0, 0 } -}; - -using namespace cv; - -cortexImage::cortexImage(){ - h = 0; - w = 0; - c = 0; - step = 0; - data = NULL; -}; - - -cortexImage::cortexImage(IplImage* src){ - if( src !=NULL ) { - uchar *ipldata = (uchar *)src->imageData; - h = src->height; - w = src->width; - c = src->nChannels; - step = src->widthStep; - data = (float*)calloc(h*w*c, sizeof(float)); - size = h*w*c; - if( data != NULL ) { - int i, j, k; - for(i = 0; i < h; ++i){ - for(k= 0; k < c; ++k){ - for(j = 0; j < w; ++j){ - data[k*w*h + i*w + j] = ((float)ipldata[i*step + j*c + k]) / 255.f; - }; - }; - }; - } else { - h = 0; - w = 0; - c = 0; - size = 0; - step = 0; - }; - } else { - h = 0; - w = 0; - c = 0; - size = 0; - step = 0; - }; -}; - -cortexImage::cortexImage(cv::Mat* src){ - if( src!=NULL ) { - h= src->rows; - w= src->cols; - c= src->channels(); - step = w*c; - size = h*w*c; - data = (float*)calloc(h*w*c, sizeof(float)); - if( data != NULL ) { - int i, j, k; - for(i = 0; i < h; ++i){ - for(k= 0; k < c; ++k){ - for(j = 0; j < w; ++j){ - data[k*w*h + i*w + j] = ((float)src->data[i*step + j*c + k])/255.f; - }; - }; - }; - } else { - h=0; - w=0; - c=0; - size = 0; - step = 0; - }; - } else { - h=0; - w=0; - c=0; - size = 0; - step = 0; - }; -}; - -bool cortexImage::fillFromMat(cv::Mat* src){ - if (src != NULL && src!= NULL) { - if ( h == src->rows && - w == src->cols && - c == src->channels()){ - int i, j, k; - for (i = 0; i < h; ++i){ - for (k = 0; k < c; ++k){ - for (j = 0; j < w; ++j){ - data[k*w*h + i*w + j] = ((float)src->data[i*step + j*c + k]) / 255.f; - }; - }; - }; - }; - }; - return false; -}; - -cortexImage::cortexImage( const cortexImage &CC ){ - h=CC.h; - w=CC.w; - c=CC.c; - step=CC.step; - size = CC.size; - data = (float*)calloc(h*w*c, sizeof(float)); - if( data != NULL ) { - memcpy( data, CC.data, h*w*c*sizeof(float)); - } else { - h=0; - w=0; - c=0; - size = 0; - step = 0; - }; -}; - -cortexImage::cortexImage( const cortexImage *CC ){ - h = CC->h; - w = CC->w; - c = CC->c; - step = CC->step; - size = CC->size; - data = (float*)calloc(h*w*c, sizeof(float)); - if (data != NULL) { - memcpy(data, CC->data, h*w*c*sizeof(float)); - } else { - h = 0; - w = 0; - c = 0; - step = 0; - size = 0; - }; -}; - -/* -cortexImage::cortexImage( cortexImage &CC ){ - h = CC.h; - w = CC.w; - c = CC.c; - step = CC.step; - data = (float*)calloc(h*w*c, sizeof(float)); - if (data != NULL) { - memcpy(data, CC.data, h*w*c*sizeof(float)); - } else { - h = 0; - w = 0; - c = 0; - step = 0; - }; -}; - -cortexImage::cortexImage( cortexImage *CC ){ - h = CC->h; - w = CC->w; - c = CC->c; - step = CC->step; - data = (float*)calloc(h*w*c, sizeof(float)); - if (data != NULL) { - memcpy(data, CC->data, h*w*c*sizeof(float)); - } else { - h = 0; - w = 0; - c = 0; - step = 0; - }; -}; -*/ - -cortexImage::~cortexImage(){ - freeData(); -}; - -float cortexImage::getColor(uint32_t c, uint32_t x, uint32_t max){ - float ratio = ((float)x/max)*5; - int i = (int)floor(ratio); - int j = (int)ceil(ratio); - ratio -= i; - float r = ((1.f - ratio)* cortexCOLORS[i][c])+ ( ratio * cortexCOLORS[j][c]); - return r; -}; - -void cortexImage::toMat(cv::Mat &dst){ - int mW, mH; - mW = (w >= dst.cols) ? dst.cols : w; - mH = (h >= dst.rows) ? dst.rows : h; - int index = 0; - for (int y = 0; y < mW; y++){ - for (int x = 0; x < mW*dst.channels(); x++){ - index = x + y*w*c; - if(dst.channels()==1){ - uchar& BGR = dst.at(y, x); - BGR = (uchar)(((uint32_t)(255.f*data[index])) & 0xFF); - } else if (dst.channels() == 3){ - Vec3b& BGR = dst.at(y, x); - BGR[0] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 0)])) & 0xFF); - BGR[1] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 1)])) & 0xFF); - BGR[2] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 2)])) & 0xFF); - } else if (dst.channels() == 4){ - Vec4b& BGR = dst.at(y, x); - BGR[0] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 0)])) & 0xFF); - BGR[1] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 1)])) & 0xFF); - BGR[2] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 2)])) & 0xFF); - BGR[3] = 0xFF; - } else return; - }; - }; -}; - -void cortexImage::toMat(cv::Mat *dst){ - if (dst != NULL)toMat(*dst); -}; - -cv::Mat *cortexImage::toMat(void){ - cv::Mat *RetVal; - RetVal = new cv::Mat(h, w, CV_8UC3); - if (RetVal != NULL)toMat(*RetVal); - return RetVal; -}; -/* -cv::Mat *mask_to_rgb( cortexImage *mask ){ - int n = mask->c; - cv::Mat *im; - im = new cv::Mat( mask->h, mask->w, CV_32FC3 ); - int i, j; - for (j = 0; j < n; ++j){ - int offset = j * 123457 % n; - float red = getColor(2, offset, n); - float green = getColor(1, offset, n); - float blue = getColor(0, offset, n); - for (i = 0; i < im.cols*im.rows; ++i){ - im.data[i + 0 * im.cols*im.rows] += mask.data[j*im.cols*im.rows + i] * red; - im.data[i + 1 * im.cols*im.rows] += mask.data[j*im.cols*im.rows + i] * green; - im.data[i + 2 * im.cols*im.rows] += mask.data[j*im.cols*im.rows + i] * blue; - } - } - return im; -}; -*/ - -bool cortexImage::composite(cortexImage *source, uint32_t dx, uint32_t dy){ - uint32_t x,y,k; - for(k = 0; k < source->c; ++k){ - for(y = 0; y < source->h; ++y){ - for(x = 0; x < source->w; ++x){ - float val = source->getPixel( x, y, k ); - float val2 = getPixelEX( dx+x, dy+y, k ); - setPixel( dx+x, dy+y, k, val*val2 ); - }; - }; - }; - return true; -}; - -void cortexImage::border(uint32_t border){ - //image b = make_image(a.w + 2*border, a.h + 2*border, a.c); - uint32_t x, y, k; - for(k = 0; k < c; ++k){ - for(y = 0; y < h; ++y){ - for(x = 0; x < w; ++x){ - float val = getPixelEX(x - border, y - border, k); - if(x - border < 0 || x - border >= w || y - border < 0 || y - border >= h) val = 1; - setPixel( x, y, k, val ); - }; - }; - }; -}; - -void cortexImage::drawLabel(uint32_t r, uint32_t c, cortexImage *label, const float *rgb){ - uint32_t tW = label->w; - uint32_t tH = label->h; - if (r - tH >= 0) r = r - tH; - uint32_t i, j, k; - for(j = 0; j < tH && ( j + r )< h; ++j){ - for(i = 0; i < tW && (i + c )< w; ++i){ - for(k = 0; k < label->c; ++k){ - float val = label->getPixel( i, j, k); - setPixel( i+c, j+r, k, rgb[k]*val ); - }; - }; - }; -}; - -void cortexImage::drawBoxEX(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, float r, float g, float b){ - //normalize_image(a); - uint32_t i, a; - if(x1 < 0) x1 = 0; - if(x1 >= w) x1 = w-1; - if(x2 < 0) x2 = 0; - if(x2 >= w) x2 = w-1; - - if(y1 < 0) y1 = 0; - if(y1 >= h) y1 = h-1; - if(y2 < 0) y2 = 0; - if(y2 >= h) y2 = h-1; - a = w*h; - for(i = x1; i <= x2; ++i){ - data[i + y1*w] = r; - data[i + y2*w] = r; - - data[i + y1*w + a] = g; - data[i + y2*w + a] = g; - - data[i + y1*w + (a<<1)] = b; - data[i + y2*w + (a<<1)] = b; - }; - for(i = y1; i <= y2; ++i){ - data[x1 + i*w] = r; - data[x2 + i*w] = r; - - data[x1 + i*w + a] = g; - data[x2 + i*w + a] = g; - - data[x1 + i*w + (a<<1)] = b; - data[x2 + i*w + (a<<1)] = b; - }; -}; - -void cortexImage::drawBoxWEX(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint32_t w, float r, float g, float b){ - uint32_t i; - for(i = 0; i < w; ++i){ - drawBoxEX( x1+i, y1+i, x2-i, y2-i, r, g, b); - }; -}; - -void cortexImage::drawBox(cortexBOX *bbox, uint32_t w, float r, float g, float b){ - uint32_t left = (int)((bbox->x - bbox->w) / 2)*w; - uint32_t right = (int)((bbox->x + bbox->w) / 2)*w; - uint32_t top = (int)((bbox->y - bbox->h) / 2)*h; - uint32_t bot = (int)((bbox->y + bbox->h) / 2)*h; - drawBoxWEX( left, top, right, bot, 2, r, g, b); -}; - -/* - void draw_detections(image im, int num, float thresh, box *boxes, float **probs, float **masks, char **names, image **alphabet, int classes){ - int i; - - for(i = 0; i < num; ++i){ - int cls = max_index(probs[i], classes); - float prob = probs[i][cls]; - if(prob > thresh){ - int width = (int)(im.h * .006); - - if(0){ - width = (int)( pow(prob, (float)(1./2.))*10+1); - alphabet = 0; - } - - //printf("%d %s: %.0f%%\n", i, names[class], prob*100); - printf("%s: %.0f%%\n", names[cls], prob*100); - int offset = cls*123457 % classes; - float red = get_color(2,offset,classes); - float green = get_color(1,offset,classes); - float blue = get_color(0,offset,classes); - float rgb[3]; - - //width = prob*20+2; - - rgb[0] = red; - rgb[1] = green; - rgb[2] = blue; - box b = boxes[i]; - - int left = (int)(b.x - b.w / 2.)*im.w; - int right = (int)(b.x + b.w / 2.)*im.w; - int top = (int)(b.y - b.h / 2.)*im.h; - int bot = (int)(b.y + b.h / 2.)*im.h; - - if(left < 0) left = 0; - if(right > im.w-1) right = im.w-1; - if(top < 0) top = 0; - if(bot > im.h-1) bot = im.h-1; - - draw_box_width(im, left, top, right, bot, width, red, green, blue); - if (alphabet) { - image label = get_label(alphabet, names[cls], (int)((im.h*.03) / 10)); - draw_label(im, top + width, left, label, rgb); - free_image(label); - } - if (masks){ - image mask = float_to_image(14, 14, 1, masks[i]); - image resized_mask = resize_image(mask, (int)(b.w*im.w), (int)(b.h*im.h)); - image tmask = threshold_image(resized_mask, .5); - embed_image(tmask, im, left, top); - free_image(mask); - free_image(resized_mask); - free_image(tmask); - } - } - } - } - - void drawMAT_detections(cv::Mat &im, int num, float thresh, box *boxes, float **probs, float **masks, char **names, int classes){ +#error "This source file seems to be unused. 2026-02-20" + +//---------------------------------------------------------------------------- +// Dr.Frucht Systems Ltd. +// Copyright © 2000-2017. All Rights Reserved. +// Version 2.0 9/14/2017 +// Last modification: 9/14/2017 +// PROJECT: +// SUBSYSTEM: +// FILE: basicImage.cpp +// AUTHOR: gagua +// OVERVIEW +// ~~~~~~~~ +// Header file of basicImage.cpp +//---------------------------------------------------------------------------- +// + + +#include "basicImage.h" +//#include "utils.h" +//#include "blas.h" +//#include "dn_cuda.h" + +#define STB_IMAGE_IMPLEMENTATION +#include "../../media/pictures/stb_image.h" +#define STB_IMAGE_WRITE_IMPLEMENTATION +#include "../../media/pictures/stb_image_write.h" + +const uint16_t cortexCOLORS[6][3] = { + { 1, 0, 1 }, + { 0, 0, 1 }, + { 0, 1, 1 }, + + { 0, 1, 0 }, + { 1, 1, 0 }, + { 1, 0, 0 } +}; + +using namespace cv; + +cortexImage::cortexImage(){ + h = 0; + w = 0; + c = 0; + step = 0; + data = NULL; +}; + + +cortexImage::cortexImage(IplImage* src){ + if( src !=NULL ) { + uchar *ipldata = (uchar *)src->imageData; + h = src->height; + w = src->width; + c = src->nChannels; + step = src->widthStep; + data = (float*)calloc(h*w*c, sizeof(float)); + size = h*w*c; + if( data != NULL ) { + int i, j, k; + for(i = 0; i < h; ++i){ + for(k= 0; k < c; ++k){ + for(j = 0; j < w; ++j){ + data[k*w*h + i*w + j] = ((float)ipldata[i*step + j*c + k]) / 255.f; + }; + }; + }; + } else { + h = 0; + w = 0; + c = 0; + size = 0; + step = 0; + }; + } else { + h = 0; + w = 0; + c = 0; + size = 0; + step = 0; + }; +}; + +cortexImage::cortexImage(cv::Mat* src){ + if( src!=NULL ) { + h= src->rows; + w= src->cols; + c= src->channels(); + step = w*c; + size = h*w*c; + data = (float*)calloc(h*w*c, sizeof(float)); + if( data != NULL ) { + int i, j, k; + for(i = 0; i < h; ++i){ + for(k= 0; k < c; ++k){ + for(j = 0; j < w; ++j){ + data[k*w*h + i*w + j] = ((float)src->data[i*step + j*c + k])/255.f; + }; + }; + }; + } else { + h=0; + w=0; + c=0; + size = 0; + step = 0; + }; + } else { + h=0; + w=0; + c=0; + size = 0; + step = 0; + }; +}; + +bool cortexImage::fillFromMat(cv::Mat* src){ + if (src != NULL && src!= NULL) { + if ( h == src->rows && + w == src->cols && + c == src->channels()){ + int i, j, k; + for (i = 0; i < h; ++i){ + for (k = 0; k < c; ++k){ + for (j = 0; j < w; ++j){ + data[k*w*h + i*w + j] = ((float)src->data[i*step + j*c + k]) / 255.f; + }; + }; + }; + }; + }; + return false; +}; + +cortexImage::cortexImage( const cortexImage &CC ){ + h=CC.h; + w=CC.w; + c=CC.c; + step=CC.step; + size = CC.size; + data = (float*)calloc(h*w*c, sizeof(float)); + if( data != NULL ) { + memcpy( data, CC.data, h*w*c*sizeof(float)); + } else { + h=0; + w=0; + c=0; + size = 0; + step = 0; + }; +}; + +cortexImage::cortexImage( const cortexImage *CC ){ + h = CC->h; + w = CC->w; + c = CC->c; + step = CC->step; + size = CC->size; + data = (float*)calloc(h*w*c, sizeof(float)); + if (data != NULL) { + memcpy(data, CC->data, h*w*c*sizeof(float)); + } else { + h = 0; + w = 0; + c = 0; + step = 0; + size = 0; + }; +}; + +/* +cortexImage::cortexImage( cortexImage &CC ){ + h = CC.h; + w = CC.w; + c = CC.c; + step = CC.step; + data = (float*)calloc(h*w*c, sizeof(float)); + if (data != NULL) { + memcpy(data, CC.data, h*w*c*sizeof(float)); + } else { + h = 0; + w = 0; + c = 0; + step = 0; + }; +}; + +cortexImage::cortexImage( cortexImage *CC ){ + h = CC->h; + w = CC->w; + c = CC->c; + step = CC->step; + data = (float*)calloc(h*w*c, sizeof(float)); + if (data != NULL) { + memcpy(data, CC->data, h*w*c*sizeof(float)); + } else { + h = 0; + w = 0; + c = 0; + step = 0; + }; +}; +*/ + +cortexImage::~cortexImage(){ + freeData(); +}; + +float cortexImage::getColor(uint32_t c, uint32_t x, uint32_t max){ + float ratio = ((float)x/max)*5; + int i = (int)floor(ratio); + int j = (int)ceil(ratio); + ratio -= i; + float r = ((1.f - ratio)* cortexCOLORS[i][c])+ ( ratio * cortexCOLORS[j][c]); + return r; +}; + +void cortexImage::toMat(cv::Mat &dst){ + int mW, mH; + mW = (w >= dst.cols) ? dst.cols : w; + mH = (h >= dst.rows) ? dst.rows : h; + int index = 0; + for (int y = 0; y < mW; y++){ + for (int x = 0; x < mW*dst.channels(); x++){ + index = x + y*w*c; + if(dst.channels()==1){ + uchar& BGR = dst.at(y, x); + BGR = (uchar)(((uint32_t)(255.f*data[index])) & 0xFF); + } else if (dst.channels() == 3){ + Vec3b& BGR = dst.at(y, x); + BGR[0] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 0)])) & 0xFF); + BGR[1] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 1)])) & 0xFF); + BGR[2] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 2)])) & 0xFF); + } else if (dst.channels() == 4){ + Vec4b& BGR = dst.at(y, x); + BGR[0] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 0)])) & 0xFF); + BGR[1] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 1)])) & 0xFF); + BGR[2] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 2)])) & 0xFF); + BGR[3] = 0xFF; + } else return; + }; + }; +}; + +void cortexImage::toMat(cv::Mat *dst){ + if (dst != NULL)toMat(*dst); +}; + +cv::Mat *cortexImage::toMat(void){ + cv::Mat *RetVal; + RetVal = new cv::Mat(h, w, CV_8UC3); + if (RetVal != NULL)toMat(*RetVal); + return RetVal; +}; +/* +cv::Mat *mask_to_rgb( cortexImage *mask ){ + int n = mask->c; + cv::Mat *im; + im = new cv::Mat( mask->h, mask->w, CV_32FC3 ); + int i, j; + for (j = 0; j < n; ++j){ + int offset = j * 123457 % n; + float red = getColor(2, offset, n); + float green = getColor(1, offset, n); + float blue = getColor(0, offset, n); + for (i = 0; i < im.cols*im.rows; ++i){ + im.data[i + 0 * im.cols*im.rows] += mask.data[j*im.cols*im.rows + i] * red; + im.data[i + 1 * im.cols*im.rows] += mask.data[j*im.cols*im.rows + i] * green; + im.data[i + 2 * im.cols*im.rows] += mask.data[j*im.cols*im.rows + i] * blue; + } + } + return im; +}; +*/ + +bool cortexImage::composite(cortexImage *source, uint32_t dx, uint32_t dy){ + uint32_t x,y,k; + for(k = 0; k < source->c; ++k){ + for(y = 0; y < source->h; ++y){ + for(x = 0; x < source->w; ++x){ + float val = source->getPixel( x, y, k ); + float val2 = getPixelEX( dx+x, dy+y, k ); + setPixel( dx+x, dy+y, k, val*val2 ); + }; + }; + }; + return true; +}; + +void cortexImage::border(uint32_t border){ + //image b = make_image(a.w + 2*border, a.h + 2*border, a.c); + uint32_t x, y, k; + for(k = 0; k < c; ++k){ + for(y = 0; y < h; ++y){ + for(x = 0; x < w; ++x){ + float val = getPixelEX(x - border, y - border, k); + if(x - border < 0 || x - border >= w || y - border < 0 || y - border >= h) val = 1; + setPixel( x, y, k, val ); + }; + }; + }; +}; + +void cortexImage::drawLabel(uint32_t r, uint32_t c, cortexImage *label, const float *rgb){ + uint32_t tW = label->w; + uint32_t tH = label->h; + if (r - tH >= 0) r = r - tH; + uint32_t i, j, k; + for(j = 0; j < tH && ( j + r )< h; ++j){ + for(i = 0; i < tW && (i + c )< w; ++i){ + for(k = 0; k < label->c; ++k){ + float val = label->getPixel( i, j, k); + setPixel( i+c, j+r, k, rgb[k]*val ); + }; + }; + }; +}; + +void cortexImage::drawBoxEX(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, float r, float g, float b){ + //normalize_image(a); + uint32_t i, a; + if(x1 < 0) x1 = 0; + if(x1 >= w) x1 = w-1; + if(x2 < 0) x2 = 0; + if(x2 >= w) x2 = w-1; + + if(y1 < 0) y1 = 0; + if(y1 >= h) y1 = h-1; + if(y2 < 0) y2 = 0; + if(y2 >= h) y2 = h-1; + a = w*h; + for(i = x1; i <= x2; ++i){ + data[i + y1*w] = r; + data[i + y2*w] = r; + + data[i + y1*w + a] = g; + data[i + y2*w + a] = g; + + data[i + y1*w + (a<<1)] = b; + data[i + y2*w + (a<<1)] = b; + }; + for(i = y1; i <= y2; ++i){ + data[x1 + i*w] = r; + data[x2 + i*w] = r; + + data[x1 + i*w + a] = g; + data[x2 + i*w + a] = g; + + data[x1 + i*w + (a<<1)] = b; + data[x2 + i*w + (a<<1)] = b; + }; +}; + +void cortexImage::drawBoxWEX(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint32_t w, float r, float g, float b){ + uint32_t i; + for(i = 0; i < w; ++i){ + drawBoxEX( x1+i, y1+i, x2-i, y2-i, r, g, b); + }; +}; + +void cortexImage::drawBox(cortexBOX *bbox, uint32_t w, float r, float g, float b){ + uint32_t left = (int)((bbox->x - bbox->w) / 2)*w; + uint32_t right = (int)((bbox->x + bbox->w) / 2)*w; + uint32_t top = (int)((bbox->y - bbox->h) / 2)*h; + uint32_t bot = (int)((bbox->y + bbox->h) / 2)*h; + drawBoxWEX( left, top, right, bot, 2, r, g, b); +}; + +/* + void draw_detections(image im, int num, float thresh, box *boxes, float **probs, float **masks, char **names, image **alphabet, int classes){ + int i; + + for(i = 0; i < num; ++i){ + int cls = max_index(probs[i], classes); + float prob = probs[i][cls]; + if(prob > thresh){ + int width = (int)(im.h * .006); + + if(0){ + width = (int)( pow(prob, (float)(1./2.))*10+1); + alphabet = 0; + } + + //printf("%d %s: %.0f%%\n", i, names[class], prob*100); + printf("%s: %.0f%%\n", names[cls], prob*100); + int offset = cls*123457 % classes; + float red = get_color(2,offset,classes); + float green = get_color(1,offset,classes); + float blue = get_color(0,offset,classes); + float rgb[3]; + + //width = prob*20+2; + + rgb[0] = red; + rgb[1] = green; + rgb[2] = blue; + box b = boxes[i]; + + int left = (int)(b.x - b.w / 2.)*im.w; + int right = (int)(b.x + b.w / 2.)*im.w; + int top = (int)(b.y - b.h / 2.)*im.h; + int bot = (int)(b.y + b.h / 2.)*im.h; + + if(left < 0) left = 0; + if(right > im.w-1) right = im.w-1; + if(top < 0) top = 0; + if(bot > im.h-1) bot = im.h-1; + + draw_box_width(im, left, top, right, bot, width, red, green, blue); + if (alphabet) { + image label = get_label(alphabet, names[cls], (int)((im.h*.03) / 10)); + draw_label(im, top + width, left, label, rgb); + free_image(label); + } + if (masks){ + image mask = float_to_image(14, 14, 1, masks[i]); + image resized_mask = resize_image(mask, (int)(b.w*im.w), (int)(b.h*im.h)); + image tmask = threshold_image(resized_mask, .5); + embed_image(tmask, im, left, top); + free_image(mask); + free_image(resized_mask); + free_image(tmask); + } + } + } + } + + void drawMAT_detections(cv::Mat &im, int num, float thresh, box *boxes, float **probs, float **masks, char **names, int classes){ char Buffer[220]; sprintf(Buffer, "Detected %d objects", num ); rectangle( im, Point(im.cols - 120, 5), Point(im.cols - 10, 15), Scalar(0, 0, 255), CV_FILLED); - putText( im, Buffer, Point(im.cols - 115, 13), FONT_HERSHEY_SIMPLEX,.3, Scalar(255, 255, 0), 1); - for (int i = 0; i < num; ++i){ - int cls = max_index(probs[i], classes); - float prob = probs[i][cls]; - if (prob > thresh){ - //int width = (int)(im.rows * .006); - - //printf("%d %s: %.0f%%\n", i, names[class], prob*100); - sprintf(Buffer, "%s: %.0f%%", names[cls], prob * 100); - int offset = cls * 123457 % classes; - float red = get_color(2, offset, classes); - float green = get_color(1, offset, classes); - float blue = get_color(0, offset, classes); - //float rgb[3]; - - //width = prob*20+2; - - //rgb[0] = red; - //rgb[1] = green; - //rgb[2] = blue; - box b = boxes[i]; - - int left = (int)((b.x - (b.w / 2.f))*im.cols); - int right = (int)((b.x + (b.w / 2.f))*im.cols); - int top = (int)((b.y - (b.h / 2.f))*im.rows); - int bot = (int)((b.y + (b.h / 2.f))*im.rows); - - if (left < 0) left = 0; - if (right > im.cols - 1) right = im.cols - 1; - if (top < 0) top = 0; - if (bot > im.rows - 1) bot = im.rows - 1; - putText(im, Buffer, Point(b.x*im.cols, b.y*im.rows), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 255, 0), 2); - rectangle(im, Point(left, top), Point(right, bot), Scalar(0, 255, 0), 2); - //draw_box_width(im, left, top, right, bot, width, red, green, blue); - }; - }; - } -*/ - -void cortexImage::transpose(void){ - assert(w == h); - uint16_t i, j, k; - for(i = 0; i < c; ++i){ - for(j = 0; j < w-1; ++j){ - for(k = j + 1; k < w; ++k){ - float swap = data[k + w*(j + h*i)]; - data[k + w*(j + h*i)] = data[j + w*(k + h*i)]; - data[j + w*(k + h*i)] = swap; - }; - }; - }; -}; - -void cortexImage::rotateCW(uint32_t times){ - assert(w == h); - times = (times + 400) % 4; - uint32_t i, x, y, l; - uint32_t n = w; - for(i = 0; i < times; ++i){ - for(l = 0; l < c; ++l){ - for(x = 0; x < n/2; ++x){ - for(y = 0; y < (n-1)/2 + 1; ++y){ - float temp = data[y + w*(x + h*l)]; - data[y + w*(x + h*l)] = data[n-1-x + w*(y + h*l)]; - data[n-1-x + w*(y + h*l)] = data[n-1-y + w*(n-1-x + h*l)]; - data[n-1-y + w*(n-1-x + h*l)] = data[x + w*(n-1-y + h*l)]; - data[x + w*(n-1-y + h*l)] = temp; - }; - }; - }; - }; -}; - -void cortexImage::flip(void){ - uint16_t i, j, k; - for(k = 0; k < c; ++k){ - for(i = 0; i < h; ++i){ - for(j = 0; j < w/2; ++j){ - int index = j + w*(i + h*(k)); - int flip = (w - j - 1) + w*(i + h*(k)); - float swap = data[flip]; - data[flip] = data[index]; - data[index] = swap; - }; - }; - }; -}; - -void cortexImage::distance(cortexImage *b){ - uint16_t i, j; - for(i = 0; i < c; ++i){ - for(j = 0; j < h*w; ++j){ - data[j] += pow( data[i*h*w+j]-data[i*h*w+j], 2 ); - }; - }; - for(j = 0; j < h*w; ++j){ - data[j] = sqrt(data[j]); - }; -}; - -void cortexImage::ghost( cortexImage *source, uint32_t dx, uint32_t dy){ - uint16_t x, y, k; - float max_dist = (float)sqrt((-source->w/2.f + .5)*(-source->w/2.f + .5)); - for(k = 0; k < source->c; ++k){ - for(y = 0; y < source->h; ++y){ - for(x = 0; x < source->w; ++x){ - float dist = (float)sqrt((x - source->w/2.f + .5f)*(x - source->w/2.f + .5f) + (y - source->h/2.f + .5f)*(y - source->h/2.f + .5f)); - float alpha = (1 - dist/max_dist); - if(alpha < 0) alpha = 0; - float v1 = source->getPixel(x,y,k); - float v2 = getPixel(dx+x,dy+y,k); - float val = alpha*v1 + (1-alpha)*v2; - setPixel(dx+x, dy+y, k, val); - }; - }; - }; -}; - -void cortexImage::embed(cortexImage *source, uint32_t dx, uint32_t dy){ - uint32_t x, y, k; - for(k = 0; k < source->c; ++k){ - for(y = 0; y < source->h; ++y){ - for(x = 0; x < source->w; ++x){ - float val = source->getPixel(x,y,k); - setPixel(dx+x, dy+y, k, val); - }; - }; - }; -}; - -/* -cortexImage *collapse_image_layers(cortexImage *source, uint32_t border){ - uint32_t h = source.h; - h = (h+border)*source.c - border; - image dest = make_image(source.w, h, 1); - uint32_t i; - for(i = 0; i < source.c; ++i){ - image layer = get_image_layer(source, i); - uint32_t h_offset = i*(source.h + border); - embed_image(layer, dest, 0, h_offset); - free_image(layer); - } - return dest; -}; -*/ -cortexImage *cortexImage::getLayer(uint32_t l){ - cortexImage *out; - out = new cortexImage(); - out->makeWithData(w, h, 1);// make_image(m.w, m.h, 1); - int i; - for (i = 0; i < h*w; ++i){ - out->data[i] = data[i + l*h*w]; - }; - return out; -}; - -void cortexImage::constrain(void){ - uint16_t i; - for(i = 0; i < w*h*c; ++i){ - if(data[i] < 0) data[i] = 0; - if(data[i] > 1) data[i] = 1; - }; -}; - -void cortexImage::normalize(void){ - uint16_t i; - float min = 9999999.f; - float max = -999999.f; - - for(i = 0; i < h*w*c; ++i){ - float v = data[i]; - if(v < min) min = v; - if(v > max) max = v; - } - if(max - min < .000000001f){ - min = 0.f; - max = 1.f; - } - for(i = 0; i < c*w*h; ++i){ - data[i] = (data[i] - min)/(max-min); - }; -}; - -void cortexImage::normalize2( void ){ - float *min = (float*)calloc(c, sizeof(float)); - float *max = (float*)calloc(c, sizeof(float)); - uint16_t i, j; - for(i = 0; i < c; ++i) min[i] = max[i] = data[i*h*w]; - - for(j = 0; j < c; ++j){ - for(i = 0; i < h*w; ++i){ - float v = data[i+j*h*w]; - if(v < min[j]) min[j] = v; - if(v > max[j]) max[j] = v; - }; - }; - for(i = 0; i < c; ++i){ - if(max[i] - min[i] < .000000001f){ - min[i] = 0; - max[i] = 1; - }; - }; - for(j = 0; j < c; ++j){ - for(i = 0; i < w*h; ++i){ - data[i+j*h*w] = (data[i+j*h*w] - min[j])/(max[j]-min[j]); - }; - }; - free(min); - free(max); -}; - -void cortexImage::copyInto(cortexImage *dest){ - if (dest->data != NULL) free(dest->data); - dest->data = (float*)calloc(h*w*c, sizeof(float)); - dest->size = h*w*c; - dest->step = w*c; - if (dest->data != NULL){ - dest->h = h; - dest->w = w; - dest->c = c; - memcpy(dest->data, data, h*w*c*sizeof(float)); - } else { - dest->h = 0; - dest->w = 0; - dest->c = 0; - dest->size = 0; - dest->step = 0; - }; -}; - -void cortexImage::rgb_to_bgr(void){ - uint16_t i; - for(i = 0; i < w*h; ++i){ - float swap = data[i]; - data[i] = data[i+w*h*2]; - data[i+w*h*2] = swap; - }; -}; - -bool cortexImage::toIpl(IplImage* dst){ - unsigned char *idata = (unsigned char *)dst->imageData; - uint32_t hh = dst->height; - uint32_t ww = dst->width; - uint32_t cc = dst->nChannels; - uint32_t sstep = dst->widthStep; - uint32_t i, j, k; - for (i = 0; i < hh; ++i){ - for (k = 0; k < cc; ++k){ - for (j = 0; j < ww; ++j){ - idata[i*sstep + j*cc + k] = ((int)( 255.f * data[k*w*h + i*w + j])) & 0xFF; - }; - }; - }; - return true; -}; - -/* - - void flush_stream_buffer(CvCapture *cap, int n) - { - int i; - for(i = 0; i < n; ++i) { - cvQueryFrame(cap); - } - } - - image get_image_from_stream(CvCapture *cap) - { - IplImage* src = cvQueryFrame(cap); - if (!src) return make_empty_image(0,0,0); - image im = ipl_to_image(src); - rgbgr_image(im); - return im; - } - - int fill_image_from_stream(CvCapture *cap, image im) - { - IplImage* src = cvQueryFrame(cap); - if (!src) return 0; - ipl_into_image(src, im); - rgbgr_image(im); - return 1 - } - - -*/ - - -/* -void cortexImage::saveJPG(const char *name){ - char buff[256]; - uint32_t x, y, k; - cortexImage *copy; - copy = new cortexImage( this ); - if (c == 3) copy->rgb_to_bgr(); - sprintf(buff, "%s.jpg", name); - IplImage *disp = cvCreateImage(cvSize(w,h), IPL_DEPTH_8U, c); - int step = disp->widthStep; - for(y = 0; y < h; ++y){ - for(x = 0; x < w; ++x){ - for(k= 0; k < c; ++k){ - disp->imageData[y*step + x*c + k] = (unsigned char)(copy->getPixel(x, y, k) * 255); - }; - }; - }; - cvSaveImage( buff, disp, 0 ); - cvReleaseImage(&disp); - delete(copy); -}; -*/ - -void cortexImage::saveJPG(const char *name, int Quality ){ - uint16_t i, k; - char buff[256]; - sprintf(buff, "%s.png", name); - unsigned char *Zdata = (unsigned char*)calloc(w*h*c, sizeof(char)); - for (k = 0; k < c; ++k){ - for (i = 0; i < w*h; ++i){ - Zdata[i*c + k] = (unsigned char)(255 * data[i + k*w*h]); - }; - }; - int success = stbi_write_jpg(buff, w, h, c, Zdata, Quality ); - free(Zdata); - if (!success) fprintf(stderr, "Failed to write image %s\n", buff); -}; - -void cortexImage::savePNG(const char *name){ - uint16_t i, k; - char buff[256]; - sprintf(buff, "%s.png", name); - unsigned char *Zdata = (unsigned char*)calloc(w*h*c, sizeof(char)); - for(k = 0; k < c; ++k){ - for(i = 0; i < w*h; ++i){ - Zdata[i*c+k] = (unsigned char) (255*data[i + k*w*h]); - }; - }; - int success = stbi_write_png( buff, w, h, c, Zdata, w*c ); - free(Zdata); - if(!success) fprintf(stderr, "Failed to write image %s\n", buff ); -}; - -void cortexImage::makeEmpty(uint32_t ww, uint32_t hh, uint32_t cc){ - if (data != NULL) free(data); - h = hh; - w = ww; - c = cc; - step = 0; - size = 0; -}; - -void cortexImage::freeData(void){ - if (data != NULL) free(data); -}; - -void cortexImage::makeWithData(uint32_t ww, uint32_t hh, uint32_t cc){ - makeEmpty( ww, hh, cc ); - size = h*w*c; - data = (float*)calloc(size, sizeof(float)); -}; - -void cortexImage::fillRandom(void){ - freeData(); - data = (float*)calloc(size, sizeof(float)); - for (uint16_t i = 0; i < size; ++i){ - data[i] = (float)((rand_normal() * .25) + .5); - }; -}; - -void cortexImage::setData(uint32_t ww, uint32_t hh, uint32_t cc, float *ddata){ - freeData(); - w = ww; - h = hh; - c = cc; - data = ddata; -}; - -void cortexImage::place(uint32_t ww, uint32_t hh, uint32_t dx, uint32_t dy, cortexImage *canvas){ - uint32_t x, y, cc; - for(cc = 0; cc < c; ++cc){ - for(y = 0; y < hh; ++y){ - for(x = 0; x < ww; ++x){ - uint32_t rx = (uint32_t)((float)x / ww) * w; - uint32_t ry = (uint32_t)((float)y / hh) * h; - float val = bilinear_interpolate( (float)rx, (float)ry, cc ); - canvas->setPixel( x + dx, y + dy, cc, val); - }; - }; - }; -}; - -void cortexImage::centerCrop(uint32_t ww, uint32_t hh){ - int m = (w < h) ? w : h; - crop((w - m) / 2, (h - m) / 2, m, m); - resize(ww, hh); -}; - -void cortexImage::rotateCrop( float rad, float s, uint32_t ww, uint32_t hh, float dx, float dy, float aspect){ - uint32_t x, y, cc; - float cx = (float)(w/2.); - float cy = (float)(h/2.); - cortexImage *rot; - rot = new cortexImage(); - rot->makeWithData(ww, hh, c); - for(cc = 0; cc < c; ++cc){ - for(y = 0; y < hh; ++y){ - for(x = 0; x < ww; ++x){ - float rx = (float)(cos(rad)*((x - ww/2.)/s*aspect + dx/s*aspect) - sin(rad)*((y - hh/2.)/s + dy/s) + cx); - float ry = (float)(sin(rad)*((x - ww/2.)/s*aspect + dx/s*aspect) + cos(rad)*((y - hh/2.)/s + dy/s) + cy); - float val = bilinear_interpolate( rx, ry, cc); - rot->setPixel(x, y, cc, val); - }; - }; - }; - setData(rot->w, rot->h, rot->c, rot->data); - delete(rot); -}; - -void cortexImage::rotate(float rad){ - uint32_t x, y, cc; - float cx = (float)(w / 2.f); - float cy = (float)(h / 2.f); - cortexImage *rot; - rot = new cortexImage(); - rot->makeWithData(w, h, c ); - for (cc = 0; cc < c; ++c){ - for (y = 0; y < h; ++y){ - for (x = 0; x < w; ++x){ - float rx = (float)(cos(rad)*(x-cx) - sin(rad)*(y-cy) + cx); - float ry = (float)(sin(rad)*(x-cx) + cos(rad)*(y-cy) + cy); - float val = bilinear_interpolate( rx, ry, cc); - rot->setPixel( x, y, cc, val); - }; - }; - }; - setData(rot->w, rot->h, rot->c, rot->data); - delete(rot); -}; - -void cortexImage::fill(float s){ - for (uint16_t i = 0; i < h*w*c; ++i) data[i] = s; -}; - -void cortexImage::translate(float s){ - for (uint16_t i = 0; i < h*w*c; ++i) data[i] += s; -}; - -void cortexImage::scale(float s){ - for (uint16_t i = 0; i < h*w*c; ++i) data[i] *= s; -}; - -void cortexImage::crop(uint32_t dx, uint32_t dy, uint32_t ww, uint32_t hh){ - cortexImage *cropped; - cropped = new cortexImage(); - cropped->makeWithData(ww, hh, c ); - uint32_t i, j, k; - for(k = 0; k < c; ++k){ - for(j = 0; j < hh; ++j){ - for(i = 0; i < ww; ++i){ - uint32_t r = j + dy; - uint32_t cc = i + dx; - float val = 0; - r = constrain_int(r, 0, h-1); - cc = constrain_int(cc, 0, w-1); - val = getPixel( cc, r, k); - cropped->setPixel(i, j, k, val); - }; - }; - }; - setData(cropped->w, cropped->h, cropped->c, cropped->data); - delete(cropped); -}; - -int best3dShiftR(cortexImage *a, cortexImage *b, int min, int max){ - if(min == max) return min; - int mid = (int)floor((min + max) / 2.); - cortexImage *c1; - cortexImage *c2; - c1 = new cortexImage(b); - c2 = new cortexImage(b); - c1->crop(0, mid, b->w, b->h); - c2->crop(0, mid + 1, b->w, b->h); - float d1 = dist_array(c1->data, a->data, a->w*a->h*a->c, 10); - float d2 = dist_array(c2->data, a->data, a->w*a->h*a->c, 10); - c1->freeData(); - c2->freeData(); - delete(c1); - delete(c2); - if( d1 < d2) return best3dShiftR(a, b, min, mid); - else return best3dShiftR(a, b, mid+1, max); -}; - -int best3dShift(cortexImage *a, cortexImage *b, int min, int max){ - uint16_t i; - int best = 0; - float best_distance = FLT_MAX; - for(i = min; i <= max; i += 2){ - cortexImage *c; - c = new cortexImage(b); - c->crop(0, i, b->w, b->h); - float d = dist_array(c->data, a->data, a->w*a->h*a->c, 100); - if(d < best_distance){ - best_distance = d; - best = i; - } - printf("%d %f\n", i, d); - c->freeData(); - delete(c); - }; - return best; -}; - -void composite3d(char *f1, char *f2, char *out, int delta){ - if(!out) out = "out"; - cortexImage *a; - a = new cortexImage(); - a->fillFromFile(f1, 0, 0, 3); - cortexImage *b; - b = new cortexImage(); - b->fillFromFile(f2, 0, 0, 3); - int shift = best3dShiftR(a, b, -a->h/100, a->h/100); - - cortexImage *c1; - c1 = new cortexImage(b); - c1->crop( 10, shift, b->w, b->h); - float d1 = dist_array(c1->data, a->data, a->w*a->h*a->c, 100); - cortexImage *c2; - c2 = new cortexImage(b); - c2->crop(-10, shift, b->w, b->h); - float d2 = dist_array(c2->data, a->data, a->w*a->h*a->c, 100); - if(d2 < d1 && 0){ - cortexImage *swap = a; - a = b; - b = swap; - shift = -shift; - printf("swapped, %d\n", shift); - } else{ - printf("%d\n", shift); - } - cortexImage *c; - c = new cortexImage(b); - c->crop( delta, shift, a->w, a->h); - int i; - for(i = 0; i < c->w*c->h; ++i){ - c->data[i] = a->data[i]; - }; - c->saveJPG(out); - c->freeData(); - c1->freeData(); - c2->freeData(); - a->freeData(); - b->freeData(); - delete(c); - delete(c1); - delete(c2); - delete(a); - delete(b); -}; - -void cortexImage::letterboxInto(cortexImage *boxed){ - int new_w = w; - int new_h = h; - if (((float)boxed->w / w) < ((float)boxed->h / h)) { - new_w = boxed->w; - new_h = (h * boxed->w) / w; - } else { - new_h = boxed->h; - new_w = (w * boxed->h) / h; - }; - cortexImage *resized; - resized = new cortexImage(this); - resized->resize(new_w, new_h); - boxed->embed(resized, (boxed->w-new_w)/2, (boxed->h-new_h)/2); - resized->freeData(); - delete(resized); -}; - -void cortexImage::letterbox(uint32_t ww, uint32_t hh){ - int new_w = w; - int new_h = h; - if (((float)ww/w) < ((float)hh/h)) { - new_w = ww; - new_h = (h * ww)/w; - } else { - new_h = hh; - new_w = (w * hh)/h; - } - resize( new_w, new_h); - cortexImage *boxed; - boxed = new cortexImage(); - boxed->makeWithData(ww, hh, c); - boxed->fill(.5f); - //int i; - //for(i = 0; i < boxed.w*boxed.h*boxed.c; ++i) boxed.data[i] = 0; - boxed->embed(this, (ww-new_w)/2, (hh-new_h)/2); - setData(ww, hh, c, boxed->data); - delete(boxed); -}; - -void cortexImage::resizeMax(uint32_t max){ - int ww = w; - int hh = h; - if(ww > hh){ - hh = (hh * max) / ww; - ww = max; - } else { - ww = (ww * max) / hh; - hh = max; - } - if(ww == w && hh == h) return; - resize( ww, hh ); -}; - -void cortexImage::resizeMin(uint32_t min){ - int ww = w; - int hh = h; - if(ww < hh){ - hh = (hh * min) / ww; - ww = min; - } else { - ww = (ww * min) / hh; - hh = min; - }; - if(ww == w && hh == h) return; - resize(ww, hh); -}; - -void cortexImage::randomCrop(uint32_t ww, uint32_t hh){ - int dx = rand_int(0, w - ww); - int dy = rand_int(0, h - hh); - crop(dx, dy, ww, hh); -}; - -cortexARGS cortexImage::randomAugmentArgs(float angle, float aspect, uint32_t low, uint32_t high, uint32_t ww, uint32_t hh){ - cortexARGS a = { 0 }; - aspect = rand_scale(aspect); - uint32_t r = rand_int(low, high); - uint32_t min = (int)((h < w*aspect) ? h : w*aspect); - float scale = (float)r / min; - - float rad = (float)(rand_uniform(-angle, angle) * TWO_PI / 360.f); - - float dx = (float)((w*scale/aspect - ww) / 2.); - float dy = (float)((h*scale - ww) / 2.); - //if(dx < 0) dx = 0; - //if(dy < 0) dy = 0; - dx = rand_uniform(-dx, dx); - dy = rand_uniform(-dy, dy); - - a.rad = rad; - a.scale = scale; - a.w = ww; - a.h = hh; - a.dx = dx; - a.dy = dy; - a.aspect = aspect; - return a; -} - -void cortexImage::randomAugment(float angle, float aspect, uint32_t low, uint32_t high, uint32_t w, uint32_t h){ - cortexARGS a = randomAugmentArgs(angle, aspect, low, high, w, h); - rotateCrop( a.rad, a.scale, a.w, a.h, a.dx, a.dy, a.aspect); -}; - -float three_way_max(float a, float b, float c){ - return (a > b) ? ( (a > c) ? a : c) : ( (b > c) ? b : c) ; -}; - -float three_way_min(float a, float b, float c){ - return (a < b) ? ( (a < c) ? a : c) : ( (b < c) ? b : c) ; -}; - -void cortexImage::yuv_to_rgb(void){ - assert( c == 3); - float r, g, b; - float y, u, v; - for (uint16_t j = 0; j < h; ++j){ - for (uint16_t i = 0; i < w; ++i){ - y = getPixel( i , j, 0); - u = getPixel( i , j, 1); - v = getPixel( i , j, 2); - - r = (float)(y + 1.13983*v); - g = (float)(y + -.39465*u + -.58060*v); - b = (float)(y + 2.03211*u); - - setPixel( i, j, 0, r); - setPixel( i, j, 1, g); - setPixel( i, j, 2, b); - }; - }; -}; - -void cortexImage::rgb_to_yuv(void){ - assert(c == 3); - float r, g, b; - float y, u, v; - for (uint16_t j = 0; j < h; ++j){ - for (uint16_t i = 0; i < w; ++i){ - r = getPixel( i , j, 0); - g = getPixel( i , j, 1); - b = getPixel( i , j, 2); - - y = (float)(.299*r + .587*g + .114*b); - u = (float)(-.14713*r + -.28886*g + .436*b); - v = (float)(.615*r + -.51499*g + -.10001*b); - - setPixel( i, j, 0, y); - setPixel( i, j, 1, u); - setPixel( i, j, 2, v); - }; - }; -}; - - // http://www.cs.rit.edu/~ncs/color/t_convert.html - -void cortexImage::rgb_to_hsv(void){ - assert( c == 3); - float r, g, b; - float hh, s, v; - for (uint16_t j = 0; j < h; ++j){ - for (uint16_t i = 0; i < w; ++i){ - r = getPixel( i , j, 0); - g = getPixel( i , j, 1); - b = getPixel( i , j, 2); - float max = three_way_max(r,g,b); - float min = three_way_min(r,g,b); - float delta = max - min; - v = max; - if(max == 0){ - s = 0; - hh = 0; - }else{ - s = delta/max; - if(r == max){ - hh = (g - b) / delta; - } else if (g == max) { - hh = 2 + (b - r) / delta; - } else { - hh = 4 + (r - g) / delta; - } - if (hh < 0) hh += 6; - hh = (float)(hh/6.); - } - setPixel( i, j, 0, hh); - setPixel( i, j, 1, s); - setPixel( i, j, 2, v); - }; - }; -}; - -void cortexImage::hsv_to_rgb( void ){ - assert(c == 3); - float r, g, b; - float hh, s, v; - float f, p, q, t; - for (uint16_t j = 0; j < h; ++j){ - for (uint16_t i = 0; i < w; ++i){ - hh = 6 * getPixel( i , j, 0); - s = getPixel( i , j, 1); - v = getPixel( i , j, 2); - if (s == 0) { - r = g = b = v; - } else { - int index = (int)floor(hh); - f = hh - index; - p = v*(1-s); - q = v*(1-s*f); - t = v*(1-s*(1-f)); - if(index == 0){ - r = v; g = t; b = p; - } else if(index == 1){ - r = q; g = v; b = p; - } else if(index == 2){ - r = p; g = v; b = t; - } else if(index == 3){ - r = p; g = q; b = v; - } else if(index == 4){ - r = t; g = p; b = v; - } else { - r = v; g = p; b = q; - }; - }; - setPixel( i, j, 0, r); - setPixel( i, j, 1, g); - setPixel( i, j, 2, b); - }; - }; -}; - -void cortexImage::grayscale_3C(void){ - assert( c == 3); - float scale[] = {(float)0.299, (float)0.587, 0.114f}; - for (uint16_t j = 0; j < h; ++j){ - for (uint16_t i = 0; i < w; ++i){ - float val = 0; - for (uint8_t k = 0; k < 3; ++k){ - val += scale[k]*getPixel( i, j, k); - } - data[0*h*w + w*j + i] = val; - data[1*h*w + w*j + i] = val; - data[2*h*w + w*j + i] = val; - }; - }; -}; - -void cortexImage::grayscale(void){ - assert(c == 3); - //image gray = make_image(im.w, im.h, 1); - float scale[] = {0.299f, 0.587f, 0.114f}; - for (uint16_t k = 0; k < c; ++k){ - for (uint16_t j = 0; j < h; ++j){ - for (uint16_t i = 0; i < w; ++i){ - data[i+w*j] += scale[k]*getPixel( i, j, k); - }; - }; - }; -}; - -void cortexImage::threshold(float thresh){ - for (uint16_t i = 0; i < w*h*c; ++i){ - data[i] = (float)(data[i]>thresh ? 1 : 0); - }; -}; - -void cortexImage::blendAsFront(cortexImage *back, float alpha){ - assert(w == back->w && h == back->h && c == back->c); - //image blend = make_image(fore.w, fore.h, fore.c); - for (uint16_t k = 0; k < c; ++k){ - for (uint16_t j = 0; j < h; ++j){ - for (uint16_t i = 0; i < w; ++i){ - float val = alpha * getPixel( i, j, k) + - (1 - alpha)* back->getPixel(i, j, k); - setPixel( i, j, k, val ); - }; - }; - }; -}; - -void cortexImage::blendAsBack(cortexImage *fore, float alpha){ - assert(fore->w == w && fore->h == h && fore->c == c); - //image blend = make_image(fore.w, fore.h, fore.c); - for (uint16_t k = 0; k < fore->c; ++k){ - for (uint16_t j = 0; j < fore->h; ++j){ - for (uint16_t i = 0; i < fore->w; ++i){ - float val = alpha * fore->getPixel(i, j, k) + - (1 - alpha)* getPixel(i, j, k); - setPixel( i, j, k, val ); - }; - }; - }; -}; - -void cortexImage::scale_channel(uint32_t cc, float v){ - for (uint16_t j = 0; j < h; ++j){ - for (uint16_t i = 0; i < w; ++i){ - float pix = getPixel( i, j, cc); - pix = pix*v; - setPixel( i, j, cc, pix); - }; - }; -}; - -void cortexImage::translate_channel(uint32_t cc, float v){ - for (uint16_t j = 0; j < h; ++j){ - for (uint16_t i = 0; i < w; ++i){ - float pix = getPixel( i, j, cc); - pix = pix+v; - setPixel( i, j, cc, pix); - }; - }; -}; - -void cortexImage::binarize(void){ - for (uint16_t i = 0; i < w * h * c; ++i){ - if(data[i] > .5) data[i] = 1; else data[i] = 0; - }; -}; - -void cortexImage::saturate(float sat){ - rgb_to_hsv(); - scale_channel(1, sat); - hsv_to_rgb(); - constrain(); -}; - -void cortexImage::hue(float hue){ - rgb_to_hsv(); - for (uint16_t i = 0; i < w*h; ++i){ - data[i] = data[i] + hue; - if (data[i] > 1) data[i] -= 1; - if (data[i] < 0) data[i] += 1; - } - hsv_to_rgb(); - constrain(); -}; - -void cortexImage::exposure(float sat){ - rgb_to_hsv(); - scale_channel( 2, sat); - hsv_to_rgb(); - constrain(); -}; - -void cortexImage::distort( float hue, float sat, float val ){ - rgb_to_hsv(); - scale_channel( 1, sat); - scale_channel( 2, val); - for (uint16_t i = 0; i < w*h; ++i){ - data[i] = data[i] + hue; - if (data[i] > 1) data[i] -= 1; - if (data[i] < 0) data[i] += 1; - } - hsv_to_rgb(); - constrain(); -}; - -void cortexImage::randomDistort(float hue, float saturation, float exposure){ - float dhue = rand_uniform(-hue, hue); - float dsat = rand_scale(saturation); - float dexp = rand_scale(exposure); - distort(dhue, dsat, dexp); -}; - -void cortexImage::saturateExposure(float sat, float exposure){ - rgb_to_hsv(); - scale_channel(1, sat); - scale_channel(2, exposure); - hsv_to_rgb(); - constrain(); -}; - -float cortexImage::bilinear_interpolate(float x, float y, uint32_t cc){ - int ix = (int) floorf(x); - int iy = (int) floorf(y); - - float dx = x - ix; - float dy = y - iy; - - float val = (1-dy) * (1-dx) * getPixelEX(ix, iy, cc) + - dy * (1-dx) * getPixelEX(ix, iy+1, cc) + - (1-dy) * dx * getPixelEX(ix+1, iy, cc) + - dy * dx * getPixelEX(ix+1, iy+1, cc); - return val; -}; - -void cortexImage::resize(uint32_t ww, uint32_t hh){ - cortexImage resized; - cortexImage part; - resized.makeEmpty(ww, hh, c); - part.makeEmpty(ww, h, c); - uint32_t r, cc, k; - float w_scale = (float)(w - 1) / (ww - 1); - float h_scale = (float)(h - 1) / (hh - 1); - for(k = 0; k < c; ++k){ - for(r = 0; r < h; ++r){ - for(cc = 0; cc < ww; ++cc){ - float val = 0; - if(cc == ww-1 || w == 1){ - val = getPixel(w-1, r, k); - } else { - float sx = cc*w_scale; - int ix = (int) sx; - float dx = sx - ix; - val = (1 - dx) * getPixel(ix, r, k) + dx * getPixel(ix+1, r, k); - }; - part.setPixel(cc, r, k, val); - }; - }; - }; - for(k = 0; k < c; ++k){ - for(r = 0; r < hh; ++r){ - float sy = r*h_scale; - int iy = (int) sy; - float dy = sy - iy; - for(cc = 0; cc < ww; ++cc){ - float val = (1 - dy) * part.getPixel(cc, iy, k); - resized.setPixel(cc, r, k, val); - }; - if(r == hh-1 || h == 1) continue; - for(cc = 0; cc < ww; ++cc){ - float val = dy * part.getPixel(cc, iy + 1, k); - resized.addPixel(cc, r, k, val); - }; - }; - }; - part.freeData(); - setData( ww, hh, resized.c, resized.data ); -}; - -bool cortexImage::fillFromFile(char* path, uint32_t x, uint32_t y, uint32_t channels){ - int ww, hh, cc; - unsigned char *Zdata = stbi_load(path, &ww, &hh, &cc, channels); - if (!data) { - fprintf(stderr, "Cannot load image \"%s\"\nSTB Reason: %s\n", path, stbi_failure_reason()); - return false; - }; - if(channels) cc = channels; - int i,j,k; - makeWithData(ww, hh, cc); - for(k = 0; k < cc; ++k){ - for(j = 0; j < hh; ++j){ - for(i = 0; i < ww; ++i){ - int dst_index = i + ww*j + ww*hh*k; - int src_index = k + cc*i + cc*ww*j; - data[dst_index] = (float)(Zdata[src_index]/255.); - }; - }; - }; - free(Zdata); - return true; -}; - - -float cortexImage::getPixel(uint32_t x, uint32_t y, uint32_t cc){ - assert(x < w && y < h && cc < c); - return data[cc*h*w + y*w + x]; -} - -float cortexImage::getPixelEX(uint32_t x, uint32_t y, uint32_t cc){ - if(x < 0 || x >= w || y < 0 || y >= h) return 0; - /* - if(x < 0) x = 0; - if(x >= m.w) x = m.w-1; - if(y < 0) y = 0; - if(y >= m.h) y = m.h-1; - */ - if(cc < 0 || cc >= c) return 0; - return getPixel( x, y, cc); -}; - -void cortexImage::setPixel(uint32_t x, uint32_t y, uint32_t cc, float val){ - if (x < 0 || y < 0 || c < 0 || x >= w || y >= h || cc >= c) return; - assert(x < w && y < h && cc < c); - data[cc*h*w + y*w + x] = val; -}; - -void cortexImage::addPixel(uint32_t x, uint32_t y, uint32_t cc, float val){ - assert(x < w && y < h && cc < c); - data[cc*h*w + y*w + x] += val; -}; - -void cortexImage::collapseImagesVert(cortexImageList *imgList){ - int color = 1; - int border = 1; - int hh,ww,cc,ch; - ww = (*imgList->begin())->w; - ch = ((*imgList->begin())->h + border); - hh = ch * imgList->size() - border; - cc = (*imgList->begin())->c; - if(cc != 3 || !color){ - ww = (ww+border)*cc - border; - cc = 1; - }; - makeWithData(ww, hh, cc); - int i,j; - i = 0; - for ( cortexImageList::iterator index = imgList->begin(); index < imgList->end(); index++ ){ - int h_offset = i*ch; - //(*index)->normalize(); - if( cc == 3 && color){ - embed( (*index), 0, h_offset); - } else{ - for (j = 0; j < (*index)->c; ++j){ - int w_offset = j*((*index)->w + border); - cortexImage *layer; - layer = (*index)->getLayer(j); - embed(layer, w_offset, h_offset); - layer->freeData(); - delete(layer); - }; - }; - }; -}; - -void cortexImage::collapseImagesHorz(cortexImageList *imgList){ - int color = 1; - int border = 1; - int hh,cw,ww,cc; - int size = (*imgList->begin())->h; - hh = size; - cw = ((*imgList->begin())->w + border); - ww = cw * imgList->size() - border; - cc = (*imgList->begin())->c; - if(cc != 3 || !color){ - hh = (hh+border)*cc - border; - cc = 1; - }; - makeWithData(ww, hh, cc); - int i,j; - i = 0; - for( cortexImageList::iterator index = imgList->begin(); index < imgList->end(); index++ ){ - int w_offset = i*cw; - //(*index)->normalize(); - if(cc == 3 && color){ - embed((*index), w_offset, 0); - } else { - for (j = 0; j < (*index)->c; ++j){ - int h_offset = j*(size+border); - cortexImage *layer; - layer = (*index)->getLayer(j); - embed(layer, w_offset, h_offset); - layer->freeData(); - delete(layer); - }; - }; - }; -}; + putText( im, Buffer, Point(im.cols - 115, 13), FONT_HERSHEY_SIMPLEX,.3, Scalar(255, 255, 0), 1); + for (int i = 0; i < num; ++i){ + int cls = max_index(probs[i], classes); + float prob = probs[i][cls]; + if (prob > thresh){ + //int width = (int)(im.rows * .006); + + //printf("%d %s: %.0f%%\n", i, names[class], prob*100); + sprintf(Buffer, "%s: %.0f%%", names[cls], prob * 100); + int offset = cls * 123457 % classes; + float red = get_color(2, offset, classes); + float green = get_color(1, offset, classes); + float blue = get_color(0, offset, classes); + //float rgb[3]; + + //width = prob*20+2; + + //rgb[0] = red; + //rgb[1] = green; + //rgb[2] = blue; + box b = boxes[i]; + + int left = (int)((b.x - (b.w / 2.f))*im.cols); + int right = (int)((b.x + (b.w / 2.f))*im.cols); + int top = (int)((b.y - (b.h / 2.f))*im.rows); + int bot = (int)((b.y + (b.h / 2.f))*im.rows); + + if (left < 0) left = 0; + if (right > im.cols - 1) right = im.cols - 1; + if (top < 0) top = 0; + if (bot > im.rows - 1) bot = im.rows - 1; + putText(im, Buffer, Point(b.x*im.cols, b.y*im.rows), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 255, 0), 2); + rectangle(im, Point(left, top), Point(right, bot), Scalar(0, 255, 0), 2); + //draw_box_width(im, left, top, right, bot, width, red, green, blue); + }; + }; + } +*/ + +void cortexImage::transpose(void){ + assert(w == h); + uint16_t i, j, k; + for(i = 0; i < c; ++i){ + for(j = 0; j < w-1; ++j){ + for(k = j + 1; k < w; ++k){ + float swap = data[k + w*(j + h*i)]; + data[k + w*(j + h*i)] = data[j + w*(k + h*i)]; + data[j + w*(k + h*i)] = swap; + }; + }; + }; +}; + +void cortexImage::rotateCW(uint32_t times){ + assert(w == h); + times = (times + 400) % 4; + uint32_t i, x, y, l; + uint32_t n = w; + for(i = 0; i < times; ++i){ + for(l = 0; l < c; ++l){ + for(x = 0; x < n/2; ++x){ + for(y = 0; y < (n-1)/2 + 1; ++y){ + float temp = data[y + w*(x + h*l)]; + data[y + w*(x + h*l)] = data[n-1-x + w*(y + h*l)]; + data[n-1-x + w*(y + h*l)] = data[n-1-y + w*(n-1-x + h*l)]; + data[n-1-y + w*(n-1-x + h*l)] = data[x + w*(n-1-y + h*l)]; + data[x + w*(n-1-y + h*l)] = temp; + }; + }; + }; + }; +}; + +void cortexImage::flip(void){ + uint16_t i, j, k; + for(k = 0; k < c; ++k){ + for(i = 0; i < h; ++i){ + for(j = 0; j < w/2; ++j){ + int index = j + w*(i + h*(k)); + int flip = (w - j - 1) + w*(i + h*(k)); + float swap = data[flip]; + data[flip] = data[index]; + data[index] = swap; + }; + }; + }; +}; + +void cortexImage::distance(cortexImage *b){ + uint16_t i, j; + for(i = 0; i < c; ++i){ + for(j = 0; j < h*w; ++j){ + data[j] += pow( data[i*h*w+j]-data[i*h*w+j], 2 ); + }; + }; + for(j = 0; j < h*w; ++j){ + data[j] = sqrt(data[j]); + }; +}; + +void cortexImage::ghost( cortexImage *source, uint32_t dx, uint32_t dy){ + uint16_t x, y, k; + float max_dist = (float)sqrt((-source->w/2.f + .5)*(-source->w/2.f + .5)); + for(k = 0; k < source->c; ++k){ + for(y = 0; y < source->h; ++y){ + for(x = 0; x < source->w; ++x){ + float dist = (float)sqrt((x - source->w/2.f + .5f)*(x - source->w/2.f + .5f) + (y - source->h/2.f + .5f)*(y - source->h/2.f + .5f)); + float alpha = (1 - dist/max_dist); + if(alpha < 0) alpha = 0; + float v1 = source->getPixel(x,y,k); + float v2 = getPixel(dx+x,dy+y,k); + float val = alpha*v1 + (1-alpha)*v2; + setPixel(dx+x, dy+y, k, val); + }; + }; + }; +}; + +void cortexImage::embed(cortexImage *source, uint32_t dx, uint32_t dy){ + uint32_t x, y, k; + for(k = 0; k < source->c; ++k){ + for(y = 0; y < source->h; ++y){ + for(x = 0; x < source->w; ++x){ + float val = source->getPixel(x,y,k); + setPixel(dx+x, dy+y, k, val); + }; + }; + }; +}; + +/* +cortexImage *collapse_image_layers(cortexImage *source, uint32_t border){ + uint32_t h = source.h; + h = (h+border)*source.c - border; + image dest = make_image(source.w, h, 1); + uint32_t i; + for(i = 0; i < source.c; ++i){ + image layer = get_image_layer(source, i); + uint32_t h_offset = i*(source.h + border); + embed_image(layer, dest, 0, h_offset); + free_image(layer); + } + return dest; +}; +*/ +cortexImage *cortexImage::getLayer(uint32_t l){ + cortexImage *out; + out = new cortexImage(); + out->makeWithData(w, h, 1);// make_image(m.w, m.h, 1); + int i; + for (i = 0; i < h*w; ++i){ + out->data[i] = data[i + l*h*w]; + }; + return out; +}; + +void cortexImage::constrain(void){ + uint16_t i; + for(i = 0; i < w*h*c; ++i){ + if(data[i] < 0) data[i] = 0; + if(data[i] > 1) data[i] = 1; + }; +}; + +void cortexImage::normalize(void){ + uint16_t i; + float min = 9999999.f; + float max = -999999.f; + + for(i = 0; i < h*w*c; ++i){ + float v = data[i]; + if(v < min) min = v; + if(v > max) max = v; + } + if(max - min < .000000001f){ + min = 0.f; + max = 1.f; + } + for(i = 0; i < c*w*h; ++i){ + data[i] = (data[i] - min)/(max-min); + }; +}; + +void cortexImage::normalize2( void ){ + float *min = (float*)calloc(c, sizeof(float)); + float *max = (float*)calloc(c, sizeof(float)); + uint16_t i, j; + for(i = 0; i < c; ++i) min[i] = max[i] = data[i*h*w]; + + for(j = 0; j < c; ++j){ + for(i = 0; i < h*w; ++i){ + float v = data[i+j*h*w]; + if(v < min[j]) min[j] = v; + if(v > max[j]) max[j] = v; + }; + }; + for(i = 0; i < c; ++i){ + if(max[i] - min[i] < .000000001f){ + min[i] = 0; + max[i] = 1; + }; + }; + for(j = 0; j < c; ++j){ + for(i = 0; i < w*h; ++i){ + data[i+j*h*w] = (data[i+j*h*w] - min[j])/(max[j]-min[j]); + }; + }; + free(min); + free(max); +}; + +void cortexImage::copyInto(cortexImage *dest){ + if (dest->data != NULL) free(dest->data); + dest->data = (float*)calloc(h*w*c, sizeof(float)); + dest->size = h*w*c; + dest->step = w*c; + if (dest->data != NULL){ + dest->h = h; + dest->w = w; + dest->c = c; + memcpy(dest->data, data, h*w*c*sizeof(float)); + } else { + dest->h = 0; + dest->w = 0; + dest->c = 0; + dest->size = 0; + dest->step = 0; + }; +}; + +void cortexImage::rgb_to_bgr(void){ + uint16_t i; + for(i = 0; i < w*h; ++i){ + float swap = data[i]; + data[i] = data[i+w*h*2]; + data[i+w*h*2] = swap; + }; +}; + +bool cortexImage::toIpl(IplImage* dst){ + unsigned char *idata = (unsigned char *)dst->imageData; + uint32_t hh = dst->height; + uint32_t ww = dst->width; + uint32_t cc = dst->nChannels; + uint32_t sstep = dst->widthStep; + uint32_t i, j, k; + for (i = 0; i < hh; ++i){ + for (k = 0; k < cc; ++k){ + for (j = 0; j < ww; ++j){ + idata[i*sstep + j*cc + k] = ((int)( 255.f * data[k*w*h + i*w + j])) & 0xFF; + }; + }; + }; + return true; +}; + +/* + + void flush_stream_buffer(CvCapture *cap, int n) + { + int i; + for(i = 0; i < n; ++i) { + cvQueryFrame(cap); + } + } + + image get_image_from_stream(CvCapture *cap) + { + IplImage* src = cvQueryFrame(cap); + if (!src) return make_empty_image(0,0,0); + image im = ipl_to_image(src); + rgbgr_image(im); + return im; + } + + int fill_image_from_stream(CvCapture *cap, image im) + { + IplImage* src = cvQueryFrame(cap); + if (!src) return 0; + ipl_into_image(src, im); + rgbgr_image(im); + return 1 + } + + +*/ + + +/* +void cortexImage::saveJPG(const char *name){ + char buff[256]; + uint32_t x, y, k; + cortexImage *copy; + copy = new cortexImage( this ); + if (c == 3) copy->rgb_to_bgr(); + sprintf(buff, "%s.jpg", name); + IplImage *disp = cvCreateImage(cvSize(w,h), IPL_DEPTH_8U, c); + int step = disp->widthStep; + for(y = 0; y < h; ++y){ + for(x = 0; x < w; ++x){ + for(k= 0; k < c; ++k){ + disp->imageData[y*step + x*c + k] = (unsigned char)(copy->getPixel(x, y, k) * 255); + }; + }; + }; + cvSaveImage( buff, disp, 0 ); + cvReleaseImage(&disp); + delete(copy); +}; +*/ + +void cortexImage::saveJPG(const char *name, int Quality ){ + uint16_t i, k; + char buff[256]; + sprintf(buff, "%s.png", name); + unsigned char *Zdata = (unsigned char*)calloc(w*h*c, sizeof(char)); + for (k = 0; k < c; ++k){ + for (i = 0; i < w*h; ++i){ + Zdata[i*c + k] = (unsigned char)(255 * data[i + k*w*h]); + }; + }; + int success = stbi_write_jpg(buff, w, h, c, Zdata, Quality ); + free(Zdata); + if (!success) fprintf(stderr, "Failed to write image %s\n", buff); +}; + +void cortexImage::savePNG(const char *name){ + uint16_t i, k; + char buff[256]; + sprintf(buff, "%s.png", name); + unsigned char *Zdata = (unsigned char*)calloc(w*h*c, sizeof(char)); + for(k = 0; k < c; ++k){ + for(i = 0; i < w*h; ++i){ + Zdata[i*c+k] = (unsigned char) (255*data[i + k*w*h]); + }; + }; + int success = stbi_write_png( buff, w, h, c, Zdata, w*c ); + free(Zdata); + if(!success) fprintf(stderr, "Failed to write image %s\n", buff ); +}; + +void cortexImage::makeEmpty(uint32_t ww, uint32_t hh, uint32_t cc){ + if (data != NULL) free(data); + h = hh; + w = ww; + c = cc; + step = 0; + size = 0; +}; + +void cortexImage::freeData(void){ + if (data != NULL) free(data); +}; + +void cortexImage::makeWithData(uint32_t ww, uint32_t hh, uint32_t cc){ + makeEmpty( ww, hh, cc ); + size = h*w*c; + data = (float*)calloc(size, sizeof(float)); +}; + +void cortexImage::fillRandom(void){ + freeData(); + data = (float*)calloc(size, sizeof(float)); + for (uint16_t i = 0; i < size; ++i){ + data[i] = (float)((rand_normal() * .25) + .5); + }; +}; + +void cortexImage::setData(uint32_t ww, uint32_t hh, uint32_t cc, float *ddata){ + freeData(); + w = ww; + h = hh; + c = cc; + data = ddata; +}; + +void cortexImage::place(uint32_t ww, uint32_t hh, uint32_t dx, uint32_t dy, cortexImage *canvas){ + uint32_t x, y, cc; + for(cc = 0; cc < c; ++cc){ + for(y = 0; y < hh; ++y){ + for(x = 0; x < ww; ++x){ + uint32_t rx = (uint32_t)((float)x / ww) * w; + uint32_t ry = (uint32_t)((float)y / hh) * h; + float val = bilinear_interpolate( (float)rx, (float)ry, cc ); + canvas->setPixel( x + dx, y + dy, cc, val); + }; + }; + }; +}; + +void cortexImage::centerCrop(uint32_t ww, uint32_t hh){ + int m = (w < h) ? w : h; + crop((w - m) / 2, (h - m) / 2, m, m); + resize(ww, hh); +}; + +void cortexImage::rotateCrop( float rad, float s, uint32_t ww, uint32_t hh, float dx, float dy, float aspect){ + uint32_t x, y, cc; + float cx = (float)(w/2.); + float cy = (float)(h/2.); + cortexImage *rot; + rot = new cortexImage(); + rot->makeWithData(ww, hh, c); + for(cc = 0; cc < c; ++cc){ + for(y = 0; y < hh; ++y){ + for(x = 0; x < ww; ++x){ + float rx = (float)(cos(rad)*((x - ww/2.)/s*aspect + dx/s*aspect) - sin(rad)*((y - hh/2.)/s + dy/s) + cx); + float ry = (float)(sin(rad)*((x - ww/2.)/s*aspect + dx/s*aspect) + cos(rad)*((y - hh/2.)/s + dy/s) + cy); + float val = bilinear_interpolate( rx, ry, cc); + rot->setPixel(x, y, cc, val); + }; + }; + }; + setData(rot->w, rot->h, rot->c, rot->data); + delete(rot); +}; + +void cortexImage::rotate(float rad){ + uint32_t x, y, cc; + float cx = (float)(w / 2.f); + float cy = (float)(h / 2.f); + cortexImage *rot; + rot = new cortexImage(); + rot->makeWithData(w, h, c ); + for (cc = 0; cc < c; ++c){ + for (y = 0; y < h; ++y){ + for (x = 0; x < w; ++x){ + float rx = (float)(cos(rad)*(x-cx) - sin(rad)*(y-cy) + cx); + float ry = (float)(sin(rad)*(x-cx) + cos(rad)*(y-cy) + cy); + float val = bilinear_interpolate( rx, ry, cc); + rot->setPixel( x, y, cc, val); + }; + }; + }; + setData(rot->w, rot->h, rot->c, rot->data); + delete(rot); +}; + +void cortexImage::fill(float s){ + for (uint16_t i = 0; i < h*w*c; ++i) data[i] = s; +}; + +void cortexImage::translate(float s){ + for (uint16_t i = 0; i < h*w*c; ++i) data[i] += s; +}; + +void cortexImage::scale(float s){ + for (uint16_t i = 0; i < h*w*c; ++i) data[i] *= s; +}; + +void cortexImage::crop(uint32_t dx, uint32_t dy, uint32_t ww, uint32_t hh){ + cortexImage *cropped; + cropped = new cortexImage(); + cropped->makeWithData(ww, hh, c ); + uint32_t i, j, k; + for(k = 0; k < c; ++k){ + for(j = 0; j < hh; ++j){ + for(i = 0; i < ww; ++i){ + uint32_t r = j + dy; + uint32_t cc = i + dx; + float val = 0; + r = constrain_int(r, 0, h-1); + cc = constrain_int(cc, 0, w-1); + val = getPixel( cc, r, k); + cropped->setPixel(i, j, k, val); + }; + }; + }; + setData(cropped->w, cropped->h, cropped->c, cropped->data); + delete(cropped); +}; + +int best3dShiftR(cortexImage *a, cortexImage *b, int min, int max){ + if(min == max) return min; + int mid = (int)floor((min + max) / 2.); + cortexImage *c1; + cortexImage *c2; + c1 = new cortexImage(b); + c2 = new cortexImage(b); + c1->crop(0, mid, b->w, b->h); + c2->crop(0, mid + 1, b->w, b->h); + float d1 = dist_array(c1->data, a->data, a->w*a->h*a->c, 10); + float d2 = dist_array(c2->data, a->data, a->w*a->h*a->c, 10); + c1->freeData(); + c2->freeData(); + delete(c1); + delete(c2); + if( d1 < d2) return best3dShiftR(a, b, min, mid); + else return best3dShiftR(a, b, mid+1, max); +}; + +int best3dShift(cortexImage *a, cortexImage *b, int min, int max){ + uint16_t i; + int best = 0; + float best_distance = FLT_MAX; + for(i = min; i <= max; i += 2){ + cortexImage *c; + c = new cortexImage(b); + c->crop(0, i, b->w, b->h); + float d = dist_array(c->data, a->data, a->w*a->h*a->c, 100); + if(d < best_distance){ + best_distance = d; + best = i; + } + printf("%d %f\n", i, d); + c->freeData(); + delete(c); + }; + return best; +}; + +void composite3d(char *f1, char *f2, char *out, int delta){ + if(!out) out = "out"; + cortexImage *a; + a = new cortexImage(); + a->fillFromFile(f1, 0, 0, 3); + cortexImage *b; + b = new cortexImage(); + b->fillFromFile(f2, 0, 0, 3); + int shift = best3dShiftR(a, b, -a->h/100, a->h/100); + + cortexImage *c1; + c1 = new cortexImage(b); + c1->crop( 10, shift, b->w, b->h); + float d1 = dist_array(c1->data, a->data, a->w*a->h*a->c, 100); + cortexImage *c2; + c2 = new cortexImage(b); + c2->crop(-10, shift, b->w, b->h); + float d2 = dist_array(c2->data, a->data, a->w*a->h*a->c, 100); + if(d2 < d1 && 0){ + cortexImage *swap = a; + a = b; + b = swap; + shift = -shift; + printf("swapped, %d\n", shift); + } else{ + printf("%d\n", shift); + } + cortexImage *c; + c = new cortexImage(b); + c->crop( delta, shift, a->w, a->h); + int i; + for(i = 0; i < c->w*c->h; ++i){ + c->data[i] = a->data[i]; + }; + c->saveJPG(out); + c->freeData(); + c1->freeData(); + c2->freeData(); + a->freeData(); + b->freeData(); + delete(c); + delete(c1); + delete(c2); + delete(a); + delete(b); +}; + +void cortexImage::letterboxInto(cortexImage *boxed){ + int new_w = w; + int new_h = h; + if (((float)boxed->w / w) < ((float)boxed->h / h)) { + new_w = boxed->w; + new_h = (h * boxed->w) / w; + } else { + new_h = boxed->h; + new_w = (w * boxed->h) / h; + }; + cortexImage *resized; + resized = new cortexImage(this); + resized->resize(new_w, new_h); + boxed->embed(resized, (boxed->w-new_w)/2, (boxed->h-new_h)/2); + resized->freeData(); + delete(resized); +}; + +void cortexImage::letterbox(uint32_t ww, uint32_t hh){ + int new_w = w; + int new_h = h; + if (((float)ww/w) < ((float)hh/h)) { + new_w = ww; + new_h = (h * ww)/w; + } else { + new_h = hh; + new_w = (w * hh)/h; + } + resize( new_w, new_h); + cortexImage *boxed; + boxed = new cortexImage(); + boxed->makeWithData(ww, hh, c); + boxed->fill(.5f); + //int i; + //for(i = 0; i < boxed.w*boxed.h*boxed.c; ++i) boxed.data[i] = 0; + boxed->embed(this, (ww-new_w)/2, (hh-new_h)/2); + setData(ww, hh, c, boxed->data); + delete(boxed); +}; + +void cortexImage::resizeMax(uint32_t max){ + int ww = w; + int hh = h; + if(ww > hh){ + hh = (hh * max) / ww; + ww = max; + } else { + ww = (ww * max) / hh; + hh = max; + } + if(ww == w && hh == h) return; + resize( ww, hh ); +}; + +void cortexImage::resizeMin(uint32_t min){ + int ww = w; + int hh = h; + if(ww < hh){ + hh = (hh * min) / ww; + ww = min; + } else { + ww = (ww * min) / hh; + hh = min; + }; + if(ww == w && hh == h) return; + resize(ww, hh); +}; + +void cortexImage::randomCrop(uint32_t ww, uint32_t hh){ + int dx = rand_int(0, w - ww); + int dy = rand_int(0, h - hh); + crop(dx, dy, ww, hh); +}; + +cortexARGS cortexImage::randomAugmentArgs(float angle, float aspect, uint32_t low, uint32_t high, uint32_t ww, uint32_t hh){ + cortexARGS a = { 0 }; + aspect = rand_scale(aspect); + uint32_t r = rand_int(low, high); + uint32_t min = (int)((h < w*aspect) ? h : w*aspect); + float scale = (float)r / min; + + float rad = (float)(rand_uniform(-angle, angle) * TWO_PI / 360.f); + + float dx = (float)((w*scale/aspect - ww) / 2.); + float dy = (float)((h*scale - ww) / 2.); + //if(dx < 0) dx = 0; + //if(dy < 0) dy = 0; + dx = rand_uniform(-dx, dx); + dy = rand_uniform(-dy, dy); + + a.rad = rad; + a.scale = scale; + a.w = ww; + a.h = hh; + a.dx = dx; + a.dy = dy; + a.aspect = aspect; + return a; +} + +void cortexImage::randomAugment(float angle, float aspect, uint32_t low, uint32_t high, uint32_t w, uint32_t h){ + cortexARGS a = randomAugmentArgs(angle, aspect, low, high, w, h); + rotateCrop( a.rad, a.scale, a.w, a.h, a.dx, a.dy, a.aspect); +}; + +float three_way_max(float a, float b, float c){ + return (a > b) ? ( (a > c) ? a : c) : ( (b > c) ? b : c) ; +}; + +float three_way_min(float a, float b, float c){ + return (a < b) ? ( (a < c) ? a : c) : ( (b < c) ? b : c) ; +}; + +void cortexImage::yuv_to_rgb(void){ + assert( c == 3); + float r, g, b; + float y, u, v; + for (uint16_t j = 0; j < h; ++j){ + for (uint16_t i = 0; i < w; ++i){ + y = getPixel( i , j, 0); + u = getPixel( i , j, 1); + v = getPixel( i , j, 2); + + r = (float)(y + 1.13983*v); + g = (float)(y + -.39465*u + -.58060*v); + b = (float)(y + 2.03211*u); + + setPixel( i, j, 0, r); + setPixel( i, j, 1, g); + setPixel( i, j, 2, b); + }; + }; +}; + +void cortexImage::rgb_to_yuv(void){ + assert(c == 3); + float r, g, b; + float y, u, v; + for (uint16_t j = 0; j < h; ++j){ + for (uint16_t i = 0; i < w; ++i){ + r = getPixel( i , j, 0); + g = getPixel( i , j, 1); + b = getPixel( i , j, 2); + + y = (float)(.299*r + .587*g + .114*b); + u = (float)(-.14713*r + -.28886*g + .436*b); + v = (float)(.615*r + -.51499*g + -.10001*b); + + setPixel( i, j, 0, y); + setPixel( i, j, 1, u); + setPixel( i, j, 2, v); + }; + }; +}; + + // http://www.cs.rit.edu/~ncs/color/t_convert.html + +void cortexImage::rgb_to_hsv(void){ + assert( c == 3); + float r, g, b; + float hh, s, v; + for (uint16_t j = 0; j < h; ++j){ + for (uint16_t i = 0; i < w; ++i){ + r = getPixel( i , j, 0); + g = getPixel( i , j, 1); + b = getPixel( i , j, 2); + float max = three_way_max(r,g,b); + float min = three_way_min(r,g,b); + float delta = max - min; + v = max; + if(max == 0){ + s = 0; + hh = 0; + }else{ + s = delta/max; + if(r == max){ + hh = (g - b) / delta; + } else if (g == max) { + hh = 2 + (b - r) / delta; + } else { + hh = 4 + (r - g) / delta; + } + if (hh < 0) hh += 6; + hh = (float)(hh/6.); + } + setPixel( i, j, 0, hh); + setPixel( i, j, 1, s); + setPixel( i, j, 2, v); + }; + }; +}; + +void cortexImage::hsv_to_rgb( void ){ + assert(c == 3); + float r, g, b; + float hh, s, v; + float f, p, q, t; + for (uint16_t j = 0; j < h; ++j){ + for (uint16_t i = 0; i < w; ++i){ + hh = 6 * getPixel( i , j, 0); + s = getPixel( i , j, 1); + v = getPixel( i , j, 2); + if (s == 0) { + r = g = b = v; + } else { + int index = (int)floor(hh); + f = hh - index; + p = v*(1-s); + q = v*(1-s*f); + t = v*(1-s*(1-f)); + if(index == 0){ + r = v; g = t; b = p; + } else if(index == 1){ + r = q; g = v; b = p; + } else if(index == 2){ + r = p; g = v; b = t; + } else if(index == 3){ + r = p; g = q; b = v; + } else if(index == 4){ + r = t; g = p; b = v; + } else { + r = v; g = p; b = q; + }; + }; + setPixel( i, j, 0, r); + setPixel( i, j, 1, g); + setPixel( i, j, 2, b); + }; + }; +}; + +void cortexImage::grayscale_3C(void){ + assert( c == 3); + float scale[] = {(float)0.299, (float)0.587, 0.114f}; + for (uint16_t j = 0; j < h; ++j){ + for (uint16_t i = 0; i < w; ++i){ + float val = 0; + for (uint8_t k = 0; k < 3; ++k){ + val += scale[k]*getPixel( i, j, k); + } + data[0*h*w + w*j + i] = val; + data[1*h*w + w*j + i] = val; + data[2*h*w + w*j + i] = val; + }; + }; +}; + +void cortexImage::grayscale(void){ + assert(c == 3); + //image gray = make_image(im.w, im.h, 1); + float scale[] = {0.299f, 0.587f, 0.114f}; + for (uint16_t k = 0; k < c; ++k){ + for (uint16_t j = 0; j < h; ++j){ + for (uint16_t i = 0; i < w; ++i){ + data[i+w*j] += scale[k]*getPixel( i, j, k); + }; + }; + }; +}; + +void cortexImage::threshold(float thresh){ + for (uint16_t i = 0; i < w*h*c; ++i){ + data[i] = (float)(data[i]>thresh ? 1 : 0); + }; +}; + +void cortexImage::blendAsFront(cortexImage *back, float alpha){ + assert(w == back->w && h == back->h && c == back->c); + //image blend = make_image(fore.w, fore.h, fore.c); + for (uint16_t k = 0; k < c; ++k){ + for (uint16_t j = 0; j < h; ++j){ + for (uint16_t i = 0; i < w; ++i){ + float val = alpha * getPixel( i, j, k) + + (1 - alpha)* back->getPixel(i, j, k); + setPixel( i, j, k, val ); + }; + }; + }; +}; + +void cortexImage::blendAsBack(cortexImage *fore, float alpha){ + assert(fore->w == w && fore->h == h && fore->c == c); + //image blend = make_image(fore.w, fore.h, fore.c); + for (uint16_t k = 0; k < fore->c; ++k){ + for (uint16_t j = 0; j < fore->h; ++j){ + for (uint16_t i = 0; i < fore->w; ++i){ + float val = alpha * fore->getPixel(i, j, k) + + (1 - alpha)* getPixel(i, j, k); + setPixel( i, j, k, val ); + }; + }; + }; +}; + +void cortexImage::scale_channel(uint32_t cc, float v){ + for (uint16_t j = 0; j < h; ++j){ + for (uint16_t i = 0; i < w; ++i){ + float pix = getPixel( i, j, cc); + pix = pix*v; + setPixel( i, j, cc, pix); + }; + }; +}; + +void cortexImage::translate_channel(uint32_t cc, float v){ + for (uint16_t j = 0; j < h; ++j){ + for (uint16_t i = 0; i < w; ++i){ + float pix = getPixel( i, j, cc); + pix = pix+v; + setPixel( i, j, cc, pix); + }; + }; +}; + +void cortexImage::binarize(void){ + for (uint16_t i = 0; i < w * h * c; ++i){ + if(data[i] > .5) data[i] = 1; else data[i] = 0; + }; +}; + +void cortexImage::saturate(float sat){ + rgb_to_hsv(); + scale_channel(1, sat); + hsv_to_rgb(); + constrain(); +}; + +void cortexImage::hue(float hue){ + rgb_to_hsv(); + for (uint16_t i = 0; i < w*h; ++i){ + data[i] = data[i] + hue; + if (data[i] > 1) data[i] -= 1; + if (data[i] < 0) data[i] += 1; + } + hsv_to_rgb(); + constrain(); +}; + +void cortexImage::exposure(float sat){ + rgb_to_hsv(); + scale_channel( 2, sat); + hsv_to_rgb(); + constrain(); +}; + +void cortexImage::distort( float hue, float sat, float val ){ + rgb_to_hsv(); + scale_channel( 1, sat); + scale_channel( 2, val); + for (uint16_t i = 0; i < w*h; ++i){ + data[i] = data[i] + hue; + if (data[i] > 1) data[i] -= 1; + if (data[i] < 0) data[i] += 1; + } + hsv_to_rgb(); + constrain(); +}; + +void cortexImage::randomDistort(float hue, float saturation, float exposure){ + float dhue = rand_uniform(-hue, hue); + float dsat = rand_scale(saturation); + float dexp = rand_scale(exposure); + distort(dhue, dsat, dexp); +}; + +void cortexImage::saturateExposure(float sat, float exposure){ + rgb_to_hsv(); + scale_channel(1, sat); + scale_channel(2, exposure); + hsv_to_rgb(); + constrain(); +}; + +float cortexImage::bilinear_interpolate(float x, float y, uint32_t cc){ + int ix = (int) floorf(x); + int iy = (int) floorf(y); + + float dx = x - ix; + float dy = y - iy; + + float val = (1-dy) * (1-dx) * getPixelEX(ix, iy, cc) + + dy * (1-dx) * getPixelEX(ix, iy+1, cc) + + (1-dy) * dx * getPixelEX(ix+1, iy, cc) + + dy * dx * getPixelEX(ix+1, iy+1, cc); + return val; +}; + +void cortexImage::resize(uint32_t ww, uint32_t hh){ + cortexImage resized; + cortexImage part; + resized.makeEmpty(ww, hh, c); + part.makeEmpty(ww, h, c); + uint32_t r, cc, k; + float w_scale = (float)(w - 1) / (ww - 1); + float h_scale = (float)(h - 1) / (hh - 1); + for(k = 0; k < c; ++k){ + for(r = 0; r < h; ++r){ + for(cc = 0; cc < ww; ++cc){ + float val = 0; + if(cc == ww-1 || w == 1){ + val = getPixel(w-1, r, k); + } else { + float sx = cc*w_scale; + int ix = (int) sx; + float dx = sx - ix; + val = (1 - dx) * getPixel(ix, r, k) + dx * getPixel(ix+1, r, k); + }; + part.setPixel(cc, r, k, val); + }; + }; + }; + for(k = 0; k < c; ++k){ + for(r = 0; r < hh; ++r){ + float sy = r*h_scale; + int iy = (int) sy; + float dy = sy - iy; + for(cc = 0; cc < ww; ++cc){ + float val = (1 - dy) * part.getPixel(cc, iy, k); + resized.setPixel(cc, r, k, val); + }; + if(r == hh-1 || h == 1) continue; + for(cc = 0; cc < ww; ++cc){ + float val = dy * part.getPixel(cc, iy + 1, k); + resized.addPixel(cc, r, k, val); + }; + }; + }; + part.freeData(); + setData( ww, hh, resized.c, resized.data ); +}; + +bool cortexImage::fillFromFile(char* path, uint32_t x, uint32_t y, uint32_t channels){ + int ww, hh, cc; + unsigned char *Zdata = stbi_load(path, &ww, &hh, &cc, channels); + if (!data) { + fprintf(stderr, "Cannot load image \"%s\"\nSTB Reason: %s\n", path, stbi_failure_reason()); + return false; + }; + if(channels) cc = channels; + int i,j,k; + makeWithData(ww, hh, cc); + for(k = 0; k < cc; ++k){ + for(j = 0; j < hh; ++j){ + for(i = 0; i < ww; ++i){ + int dst_index = i + ww*j + ww*hh*k; + int src_index = k + cc*i + cc*ww*j; + data[dst_index] = (float)(Zdata[src_index]/255.); + }; + }; + }; + free(Zdata); + return true; +}; + + +float cortexImage::getPixel(uint32_t x, uint32_t y, uint32_t cc){ + assert(x < w && y < h && cc < c); + return data[cc*h*w + y*w + x]; +} + +float cortexImage::getPixelEX(uint32_t x, uint32_t y, uint32_t cc){ + if(x < 0 || x >= w || y < 0 || y >= h) return 0; + /* + if(x < 0) x = 0; + if(x >= m.w) x = m.w-1; + if(y < 0) y = 0; + if(y >= m.h) y = m.h-1; + */ + if(cc < 0 || cc >= c) return 0; + return getPixel( x, y, cc); +}; + +void cortexImage::setPixel(uint32_t x, uint32_t y, uint32_t cc, float val){ + if (x < 0 || y < 0 || c < 0 || x >= w || y >= h || cc >= c) return; + assert(x < w && y < h && cc < c); + data[cc*h*w + y*w + x] = val; +}; + +void cortexImage::addPixel(uint32_t x, uint32_t y, uint32_t cc, float val){ + assert(x < w && y < h && cc < c); + data[cc*h*w + y*w + x] += val; +}; + +void cortexImage::collapseImagesVert(cortexImageList *imgList){ + int color = 1; + int border = 1; + int hh,ww,cc,ch; + ww = (*imgList->begin())->w; + ch = ((*imgList->begin())->h + border); + hh = ch * imgList->size() - border; + cc = (*imgList->begin())->c; + if(cc != 3 || !color){ + ww = (ww+border)*cc - border; + cc = 1; + }; + makeWithData(ww, hh, cc); + int i,j; + i = 0; + for ( cortexImageList::iterator index = imgList->begin(); index < imgList->end(); index++ ){ + int h_offset = i*ch; + //(*index)->normalize(); + if( cc == 3 && color){ + embed( (*index), 0, h_offset); + } else{ + for (j = 0; j < (*index)->c; ++j){ + int w_offset = j*((*index)->w + border); + cortexImage *layer; + layer = (*index)->getLayer(j); + embed(layer, w_offset, h_offset); + layer->freeData(); + delete(layer); + }; + }; + }; +}; + +void cortexImage::collapseImagesHorz(cortexImageList *imgList){ + int color = 1; + int border = 1; + int hh,cw,ww,cc; + int size = (*imgList->begin())->h; + hh = size; + cw = ((*imgList->begin())->w + border); + ww = cw * imgList->size() - border; + cc = (*imgList->begin())->c; + if(cc != 3 || !color){ + hh = (hh+border)*cc - border; + cc = 1; + }; + makeWithData(ww, hh, cc); + int i,j; + i = 0; + for( cortexImageList::iterator index = imgList->begin(); index < imgList->end(); index++ ){ + int w_offset = i*cw; + //(*index)->normalize(); + if(cc == 3 && color){ + embed((*index), w_offset, 0); + } else { + for (j = 0; j < (*index)->c; ++j){ + int h_offset = j*(size+border); + cortexImage *layer; + layer = (*index)->getLayer(j); + embed(layer, w_offset, h_offset); + layer->freeData(); + delete(layer); + }; + }; + }; +}; diff --git a/lib/data/darknet/darknetWraper.cpp b/lib/data/darknet/darknetWraper.cpp index 4e4d9470..7854d716 100644 --- a/lib/data/darknet/darknetWraper.cpp +++ b/lib/data/darknet/darknetWraper.cpp @@ -1,9 +1,13 @@ +#error "This source file seems to be unused. 2026-02-20" + + #include "darknetWraper.h" #include #define DARKNET_INCLUDE_ORIGINAL_API #include //#include + darknetWraper::darknetWraper(){ m_pNET = NULL; }; diff --git a/lib/data/media/video/videoAVLIBSource.cpp b/lib/data/media/video/videoAVLIBSource.cpp index 66fdaa06..d2791cec 100644 --- a/lib/data/media/video/videoAVLIBSource.cpp +++ b/lib/data/media/video/videoAVLIBSource.cpp @@ -762,33 +762,7 @@ void videoAVLIBSource::m_fThreadProc( void ){ /** @todo Valgrind is reporting several important memory leaks in this method. * * The first one has to do with @p av_frame_alloc(): - * - * ~~~~ - ==00:00:04:04.219 26021== 2,074,168 (536 direct, 2,073,632 indirect) bytes in 1 blocks are definitely lost in loss record 3,970 of 3,972 - ==00:00:04:04.219 26021== at 0x483AEB8: memalign (vg_replace_malloc.c:906) - ==00:00:04:04.219 26021== by 0x483AFCE: posix_memalign (vg_replace_malloc.c:1070) - ==00:00:04:04.219 26021== by 0x66E2244: av_malloc (in /usr/lib/x86_64-linux-gnu/libavutil.so.56.70.100) - ==00:00:04:04.219 26021== by 0x66E25FD: av_mallocz (in /usr/lib/x86_64-linux-gnu/libavutil.so.56.70.100) - ==00:00:04:04.219 26021== by 0x66BF6BF: av_frame_alloc (in /usr/lib/x86_64-linux-gnu/libavutil.so.56.70.100) - ==00:00:04:04.219 26021== by 0x248B0E: videoAVLIBSource::m_fThreadProc() (videoAVLIBSource.cpp:802) - ==00:00:04:04.219 26021== by 0x131B54: basicThread::m_fThreadEntryPoint(void*) (basicThread.cpp:62) - ==00:00:04:04.219 26021== by 0x49B7EA6: start_thread (pthread_create.c:477) - ==00:00:04:04.219 26021== by 0x7874ADE: clone (clone.S:95) - * ~~~~ - * * The next one has to do with @p av_image_alloc(): - * - * ~~~~ - ==00:00:04:04.219 26021== 2,073,632 bytes in 1 blocks are indirectly lost in loss record 3,969 of 3,972 - ==00:00:04:04.219 26021== at 0x483AEB8: memalign (vg_replace_malloc.c:906) - ==00:00:04:04.219 26021== by 0x483AFCE: posix_memalign (vg_replace_malloc.c:1070) - ==00:00:04:04.219 26021== by 0x66E2244: av_malloc (in /usr/lib/x86_64-linux-gnu/libavutil.so.56.70.100) - ==00:00:04:04.219 26021== by 0x66DCEB5: av_image_alloc (in /usr/lib/x86_64-linux-gnu/libavutil.so.56.70.100) - ==00:00:04:04.219 26021== by 0x248BCB: videoAVLIBSource::m_fThreadProc() (videoAVLIBSource.cpp:807) - ==00:00:04:04.219 26021== by 0x131B54: basicThread::m_fThreadEntryPoint(void*) (basicThread.cpp:62) - ==00:00:04:04.219 26021== by 0x49B7EA6: start_thread (pthread_create.c:477) - ==00:00:04:04.219 26021== by 0x7874ADE: clone (clone.S:95) - * ~~~~ */ m_bThreadStatus = TH_WORKING;