@@ -455,13 +455,32 @@ void *YoloObjectDetector::detectInThread()
455455 return 0 ;
456456}
457457
458+
459+ void ipl_into_image_cp (IplImage* src, image im)
460+ {
461+ unsigned char *data = (unsigned char *)src->imageData ;
462+ int h = src->height ;
463+ int w = src->width ;
464+ int c = src->nChannels ;
465+ int step = src->widthStep ;
466+ int i, j, k;
467+
468+ for (i = 0 ; i < h; ++i){
469+ for (k= 0 ; k < c; ++k){
470+ for (j = 0 ; j < w; ++j){
471+ im.data [k*w*h + i*w + j] = data[i*step + j*c + k]/255 .;
472+ }
473+ }
474+ }
475+ }
476+
458477void *YoloObjectDetector::fetchInThread ()
459478{
460479 {
461480 std::shared_lock<std::shared_mutex> lock (mutexImageCallback_);
462481 IplImageWithHeader_ imageAndHeader = getIplImageWithHeader ();
463482 IplImage* ROS_img = imageAndHeader.image ;
464- ipl_into_image (ROS_img, buff_[buffIndex_]);
483+ ipl_into_image_cp (ROS_img, buff_[buffIndex_]);
465484 headerBuff_[buffIndex_] = imageAndHeader.header ;
466485 buffId_[buffIndex_] = actionId_;
467486 }
@@ -470,9 +489,54 @@ void *YoloObjectDetector::fetchInThread()
470489 return 0 ;
471490}
472491
492+
493+ float get_pixel_cp (image m, int x, int y, int c)
494+ {
495+ assert (x < m.w && y < m.h && c < m.c );
496+ return m.data [c*m.h *m.w + y*m.w + x];
497+ }
498+
499+ int windows = 0 ;
500+
501+ void show_image_cv_cp (image p, const char *name, IplImage *disp)
502+ {
503+ int x,y,k;
504+ if (p.c == 3 ) rgbgr_image (p);
505+ // normalize_image(copy);
506+
507+ char buff[256 ];
508+ // sprintf(buff, "%s (%d)", name, windows);
509+ sprintf (buff, " %s" , name);
510+
511+ int step = disp->widthStep ;
512+ cvNamedWindow (buff, CV_WINDOW_NORMAL);
513+ // cvMoveWindow(buff, 100*(windows%10) + 200*(windows/10), 100*(windows%10));
514+ ++windows;
515+ for (y = 0 ; y < p.h ; ++y){
516+ for (x = 0 ; x < p.w ; ++x){
517+ for (k= 0 ; k < p.c ; ++k){
518+ disp->imageData [y*step + x*p.c + k] = (unsigned char )(get_pixel_cp (p,x,y,k)*255 );
519+ }
520+ }
521+ }
522+ if (0 ){
523+ int w = 448 ;
524+ int h = w*p.h /p.w ;
525+ if (h > 1000 ){
526+ h = 1000 ;
527+ w = h*p.w /p.h ;
528+ }
529+ IplImage *buffer = disp;
530+ disp = cvCreateImage (cvSize (w, h), buffer->depth , buffer->nChannels );
531+ cvResize (buffer, disp, CV_INTER_LINEAR);
532+ cvReleaseImage (&buffer);
533+ }
534+ cvShowImage (buff, disp);
535+ }
536+
473537void *YoloObjectDetector::displayInThread (void *ptr)
474538{
475- show_image_cv (buff_[(buffIndex_ + 1 )%3 ], " YOLO V3" , ipl_);
539+ show_image_cv_cp (buff_[(buffIndex_ + 1 )%3 ], " YOLO V3" , ipl_);
476540 int c = cv::waitKey (waitKeyDelay_);
477541 if (c != -1 ) c = c%256 ;
478542 if (c == 27 ) {
@@ -506,6 +570,26 @@ void *YoloObjectDetector::detectLoop(void *ptr)
506570 }
507571}
508572
573+
574+ image **load_alphabet_with_file_cp (char *datafile) {
575+ int i, j;
576+ const int nsize = 8 ;
577+ image **alphabets = (image**)calloc (nsize, sizeof (image));
578+ char * labels = " /labels/%d_%d.png" ;
579+ char * files = (char *) malloc (1 + strlen (datafile)+ strlen (labels) );
580+ strcpy (files, datafile);
581+ strcat (files, labels);
582+ for (j = 0 ; j < nsize; ++j){
583+ alphabets[j] = (image*)calloc (128 , sizeof (image));
584+ for (i = 32 ; i < 127 ; ++i){
585+ char buff[256 ];
586+ sprintf (buff, files, i, j);
587+ alphabets[j][i] = load_image_color (buff, 0 , 0 );
588+ }
589+ }
590+ return alphabets;
591+ }
592+
509593void YoloObjectDetector::setupNetwork (char *cfgfile, char *weightfile, char *datafile, float thresh,
510594 char **names, int classes,
511595 int delay, char *prefix, int avg_frames, float hier, int w, int h,
@@ -514,7 +598,7 @@ void YoloObjectDetector::setupNetwork(char *cfgfile, char *weightfile, char *dat
514598 demoPrefix_ = prefix;
515599 demoDelay_ = delay;
516600 demoFrame_ = avg_frames;
517- image **alphabet = load_alphabet_with_file (datafile);
601+ image **alphabet = load_alphabet_with_file_cp (datafile);
518602 demoNames_ = names;
519603 demoAlphabet_ = alphabet;
520604 demoClasses_ = classes;
@@ -526,6 +610,32 @@ void YoloObjectDetector::setupNetwork(char *cfgfile, char *weightfile, char *dat
526610 set_batch_network (net_, 1 );
527611}
528612
613+ void generate_image_cp (image p, IplImage *disp)
614+ {
615+ int x,y,k;
616+ if (p.c == 3 ) rgbgr_image (p);
617+ // normalize_image(copy);
618+
619+ int step = disp->widthStep ;
620+ for (y = 0 ; y < p.h ; ++y){
621+ for (x = 0 ; x < p.w ; ++x){
622+ for (k= 0 ; k < p.c ; ++k){
623+ disp->imageData [y*step + x*p.c + k] = (unsigned char )(get_pixel_cp (p,x,y,k)*255 );
624+ }
625+ }
626+ }
627+ }
628+
629+ image ipl_to_image_cp (IplImage* src)
630+ {
631+ int h = src->height ;
632+ int w = src->width ;
633+ int c = src->nChannels ;
634+ image out = make_image (w, h, c);
635+ ipl_into_image_cp (src, out);
636+ return out;
637+ }
638+
529639void YoloObjectDetector::yolo ()
530640{
531641 const auto wait_duration = std::chrono::milliseconds (2000 );
@@ -557,7 +667,7 @@ void YoloObjectDetector::yolo()
557667 std::shared_lock<std::shared_mutex> lock (mutexImageCallback_);
558668 IplImageWithHeader_ imageAndHeader = getIplImageWithHeader ();
559669 IplImage* ROS_img = imageAndHeader.image ;
560- buff_[0 ] = ipl_to_image (ROS_img);
670+ buff_[0 ] = ipl_to_image_cp (ROS_img);
561671 headerBuff_[0 ] = imageAndHeader.header ;
562672 }
563673 buff_[1 ] = copy_image (buff_[0 ]);
@@ -593,7 +703,7 @@ void YoloObjectDetector::yolo()
593703 if (viewImage_) {
594704 displayInThread (0 );
595705 } else {
596- generate_image (buff_[(buffIndex_ + 1 )%3 ], ipl_);
706+ generate_image_cp (buff_[(buffIndex_ + 1 )%3 ], ipl_);
597707 }
598708 publishInThread ();
599709 } else {
@@ -613,7 +723,8 @@ void YoloObjectDetector::yolo()
613723
614724IplImageWithHeader_ YoloObjectDetector::getIplImageWithHeader ()
615725{
616- IplImage* ROS_img = new IplImage (camImageCopy_);
726+ IplImage* ROS_img = new IplImage ();
727+ *ROS_img = cvIplImage (camImageCopy_);
617728 IplImageWithHeader_ header = {.image = ROS_img, .header = imageHeader_};
618729 return header;
619730}
0 commit comments