@@ -214,6 +214,13 @@ cv::Mat ipl_to_mat(IplImage *ipl)
214214 return m;
215215}
216216
217+ IplImage *mat_to_ipl (cv::Mat mat)
218+ {
219+ IplImage *ipl = new IplImage;
220+ *ipl = mat;
221+ return ipl;
222+ }
223+
217224Mat image_to_mat (image im)
218225{
219226 image copy = copy_image (im);
@@ -335,13 +342,9 @@ void show_image_cv_ipl(mat_cv *disp, const char *name)
335342{
336343 if (disp == NULL ) return ;
337344 char buff[256 ];
338- // sprintf(buff, "%s (%d)", name, windows);
339345 sprintf (buff, " %s" , name);
340346 cv::namedWindow (buff, WINDOW_NORMAL);
341- // cvMoveWindow(buff, 100*(windows%10) + 200*(windows/10), 100*(windows%10));
342- // ++windows;
343347 cvShowImage (buff, disp);
344- // cvReleaseImage(&disp);
345348}
346349
347350
@@ -688,6 +691,23 @@ image get_image_from_stream_letterbox(cap_cv *cap, int w, int h, int c, mat_cv**
688691extern int stbi_write_png (char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
689692extern int stbi_write_jpg (char const *filename, int x, int y, int comp, const void *data, int quality);
690693
694+ void save_mat_png (cv::Mat img_src, const char *name)
695+ {
696+ cv::Mat img_rgb;
697+ cv::cvtColor (img_src, img_rgb, CV_RGB2BGR);
698+ stbi_write_png (name, img_rgb.cols , img_rgb.rows , 3 , (char *)img_rgb.data , 0 );
699+ }
700+ // ----------------------------------------
701+
702+ void save_mat_jpg (cv::Mat img_src, const char *name)
703+ {
704+ cv::Mat img_rgb;
705+ cv::cvtColor (img_src, img_rgb, CV_RGB2BGR);
706+ stbi_write_jpg (name, img_rgb.cols , img_rgb.rows , 3 , (char *)img_rgb.data , 80 );
707+ }
708+ // ----------------------------------------
709+
710+
691711void save_cv_png (mat_cv *img_src, const char *name)
692712{
693713 IplImage* img = (IplImage* )img_src;
@@ -841,93 +861,88 @@ mat_cv* draw_train_chart(float max_img_loss, int max_batches, int number_of_line
841861{
842862 int img_offset = 50 ;
843863 int draw_size = img_size - img_offset;
844- IplImage* img = cvCreateImage (cvSize (img_size, img_size), 8 , 3 );
845- cvSet (img, CV_RGB (255 , 255 , 255 ), 0 );
846- CvPoint pt1, pt2, pt_text;
847- CvFont font;
848- cvInitFont (&font, CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7 , 0.7 , 0 , 1 , CV_AA);
864+ cv::Mat *img_ptr = new cv::Mat (img_size, img_size, CV_8UC3, CV_RGB (255 , 255 , 255 ));
865+ cv::Mat &img = *img_ptr;
866+ cv::Point pt1, pt2, pt_text;
867+
849868 char char_buff[100 ];
850869 int i;
851870 // vertical lines
852871 pt1.x = img_offset; pt2.x = img_size, pt_text.x = 10 ;
853872 for (i = 1 ; i <= number_of_lines; ++i) {
854873 pt1.y = pt2.y = (float )i * draw_size / number_of_lines;
855- cvLine (img, pt1, pt2, CV_RGB (224 , 224 , 224 ), 1 , 8 , 0 );
874+ cv::line (img, pt1, pt2, CV_RGB (224 , 224 , 224 ), 1 , 8 , 0 );
856875 if (i % 10 == 0 ) {
857876 sprintf (char_buff, " %2.1f" , max_img_loss*(number_of_lines - i) / number_of_lines);
858877 pt_text.y = pt1.y + 5 ;
859- cvPutText (img, char_buff, pt_text, &font, CV_RGB (0 , 0 , 0 ));
860- cvLine (img, pt1, pt2, CV_RGB (128 , 128 , 128 ), 1 , 8 , 0 );
878+
879+ cv::putText (img, char_buff, pt_text, CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7 , CV_RGB (0 , 0 , 0 ), 1 , CV_AA);
880+ cv::line (img, pt1, pt2, CV_RGB (128 , 128 , 128 ), 1 , 8 , 0 );
861881 }
862882 }
863883 // horizontal lines
864884 pt1.y = draw_size; pt2.y = 0 , pt_text.y = draw_size + 15 ;
865885 for (i = 0 ; i <= number_of_lines; ++i) {
866886 pt1.x = pt2.x = img_offset + (float )i * draw_size / number_of_lines;
867- cvLine (img, pt1, pt2, CV_RGB (224 , 224 , 224 ), 1 , 8 , 0 );
887+ cv::line (img, pt1, pt2, CV_RGB (224 , 224 , 224 ), 1 , 8 , 0 );
868888 if (i % 10 == 0 ) {
869889 sprintf (char_buff, " %d" , max_batches * i / number_of_lines);
870890 pt_text.x = pt1.x - 20 ;
871- cvPutText (img, char_buff, pt_text, &font, CV_RGB (0 , 0 , 0 ));
872- cvLine (img, pt1, pt2, CV_RGB (128 , 128 , 128 ), 1 , 8 , 0 );
891+ cv::putText (img, char_buff, pt_text, CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7 , CV_RGB (0 , 0 , 0 ), 1 , CV_AA );
892+ cv::line (img, pt1, pt2, CV_RGB (128 , 128 , 128 ), 1 , 8 , 0 );
873893 }
874894 }
875895
876- cvPutText (img, " Loss" , cvPoint (0 , 35 ), &font, CV_RGB (0 , 0 , 255 ) );
877- cvPutText (img, " Iteration number" , cvPoint (draw_size / 2 , img_size - 10 ), &font, CV_RGB (0 , 0 , 0 ));
896+ cv::putText (img, " Loss" , cvPoint (0 , 35 ), CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7 , CV_RGB (0 , 0 , 0 ), 1 , CV_AA );
897+ cv::putText (img, " Iteration number" , cvPoint (draw_size / 2 , img_size - 10 ), CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7 , CV_RGB (0 , 0 , 0 ), 1 , CV_AA );
878898 char max_batches_buff[100 ];
879899 sprintf (max_batches_buff, " in cfg max_batches=%d" , max_batches);
880- cvPutText (img, max_batches_buff, cvPoint (draw_size - 195 , img_size - 10 ), &font, CV_RGB (0 , 0 , 0 ));
881- cvPutText (img, " Press 's' to save: chart.png" , cvPoint (5 , img_size - 10 ), &font, CV_RGB (0 , 0 , 0 ));
900+ cv::putText (img, max_batches_buff, cvPoint (draw_size - 195 , img_size - 10 ), CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7 , CV_RGB (0 , 0 , 0 ), 1 , CV_AA );
901+ cv::putText (img, " Press 's' to save : chart.png" , cvPoint (5 , img_size - 10 ), CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7 , CV_RGB (0 , 0 , 0 ), 1 , CV_AA );
882902 if (!dont_show) {
883903 printf (" If error occurs - run training with flag: -dont_show \n " );
884- cvNamedWindow (" average loss" , CV_WINDOW_NORMAL);
885- cvMoveWindow (" average loss" , 0 , 0 );
886- cvResizeWindow (" average loss" , img_size, img_size);
887- cvShowImage (" average loss" , img);
888- cvWaitKey (20 );
904+ cv::namedWindow (" average loss" , CV_WINDOW_NORMAL);
905+ cv::moveWindow (" average loss" , 0 , 0 );
906+ cv::resizeWindow (" average loss" , img_size, img_size);
907+ cv::imshow (" average loss" , img);
908+ cv::waitKey (20 );
889909 }
890- return (mat_cv*)img ;
910+ return (mat_cv*)img_ptr ;
891911}
892912// ----------------------------------------
893913
894914void draw_train_loss (mat_cv* img_src, int img_size, float avg_loss, float max_img_loss, int current_batch, int max_batches,
895915 float precision, int draw_precision, char *accuracy_name, int dont_show, int mjpeg_port)
896916{
897- IplImage* img = (IplImage *)img_src;
917+ cv::Mat & img = *(cv::Mat *)img_src;
898918 int img_offset = 50 ;
899919 int draw_size = img_size - img_offset;
900- CvFont font;
901- cvInitFont (&font, CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7 , 0.7 , 0 , 1 , CV_AA);
902920 char char_buff[100 ];
903921 CvPoint pt1, pt2;
904922 pt1.x = img_offset + draw_size * (float )current_batch / max_batches;
905923 pt1.y = draw_size * (1 - avg_loss / max_img_loss);
906924 if (pt1.y < 0 ) pt1.y = 1 ;
907- cvCircle (img, pt1, 1 , CV_RGB (0 , 0 , 255 ), CV_FILLED, 8 , 0 );
925+ cv::circle (img, pt1, 1 , CV_RGB (0 , 0 , 255 ), CV_FILLED, 8 , 0 );
908926
909927 // precision
910928 if (draw_precision) {
911929 static float old_precision = 0 ;
912930 static int iteration_old = 0 ;
913931 static int text_iteration_old = 0 ;
914- if (iteration_old == 0 ) cvPutText (img, accuracy_name, cvPoint (0 , 12 ), &font, CV_RGB (255 , 0 , 0 ));
932+ if (iteration_old == 0 )
933+ cv::putText (img, accuracy_name, cvPoint (0 , 12 ), CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7 , CV_RGB (255 , 0 , 0 ), 1 , CV_AA);
915934
916- cvLine (img,
935+ cv::line (img,
917936 cvPoint (img_offset + draw_size * (float )iteration_old / max_batches, draw_size * (1 - old_precision)),
918937 cvPoint (img_offset + draw_size * (float )current_batch / max_batches, draw_size * (1 - precision)),
919938 CV_RGB (255 , 0 , 0 ), 1 , 8 , 0 );
920939
921940 if (((int )(old_precision * 10 ) != (int )(precision * 10 )) || (current_batch - text_iteration_old) >= max_batches / 10 ) {
922941 text_iteration_old = current_batch;
923942 sprintf (char_buff, " %2.0f%% " , precision * 100 );
924- CvFont font3;
925- cvInitFont (&font3, CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7 , 0.7 , 0 , 5 , CV_AA);
926- cvPutText (img, char_buff, cvPoint (pt1.x - 30 , draw_size * (1 - precision) + 15 ), &font3, CV_RGB (255 , 255 , 255 ));
943+ cv::putText (img, char_buff, cvPoint (pt1.x - 30 , draw_size * (1 - precision) + 15 ), CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7 , CV_RGB (255 , 255 , 255 ), 5 , CV_AA);
927944
928- CvFont font2;
929- cvInitFont (&font2, CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7 , 0.7 , 0 , 1 , CV_AA);
930- cvPutText (img, char_buff, cvPoint (pt1.x - 30 , draw_size * (1 - precision) + 15 ), &font2, CV_RGB (200 , 0 , 0 ));
945+ cv::putText (img, char_buff, cvPoint (pt1.x - 30 , draw_size * (1 - precision) + 15 ), CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7 , CV_RGB (200 , 0 , 0 ), 1 , CV_AA);
931946 }
932947 old_precision = precision;
933948 iteration_old = current_batch;
@@ -936,23 +951,23 @@ void draw_train_loss(mat_cv* img_src, int img_size, float avg_loss, float max_im
936951 sprintf (char_buff, " current avg loss = %2.4f iteration = %d" , avg_loss, current_batch);
937952 pt1.x = 55 , pt1.y = 10 ;
938953 pt2.x = pt1.x + 460 , pt2.y = pt1.y + 20 ;
939- cvRectangle (img, pt1, pt2, CV_RGB (255 , 255 , 255 ), CV_FILLED, 8 , 0 );
954+ cv::rectangle (img, pt1, pt2, CV_RGB (255 , 255 , 255 ), CV_FILLED, 8 , 0 );
940955 pt1.y += 15 ;
941- cvPutText (img, char_buff, pt1, &font, CV_RGB (0 , 0 , 0 ));
956+ cv::putText (img, char_buff, pt1, CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7 , CV_RGB (0 , 0 , 0 ), 1 , CV_AA );
942957
943958 int k = 0 ;
944959 if (!dont_show) {
945- cvShowImage (" average loss" , img);
946- k = cvWaitKey (20 );
960+ cv::imshow (" average loss" , img);
961+ k = cv::waitKey (20 );
947962 }
948963 if (k == ' s' || current_batch == (max_batches - 1 ) || current_batch % 100 == 0 ) {
949- save_cv_png ((mat_cv *) img, " chart.png" );
950- cvPutText (img, " - Saved" , cvPoint (250 , img_size - 10 ), &font, CV_RGB (255 , 0 , 0 ));
964+ save_mat_png ( img, " chart.png" );
965+ cv::putText (img, " - Saved" , cvPoint (260 , img_size - 10 ), CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7 , CV_RGB (255 , 0 , 0 ), 1 , CV_AA );
951966 }
952967 else
953- cvPutText (img, " - Saved" , cvPoint (250 , img_size - 10 ), &font, CV_RGB (255 , 255 , 255 ));
968+ cv::putText (img, " - Saved" , cvPoint (260 , img_size - 10 ), CV_FONT_HERSHEY_COMPLEX_SMALL, 0.7 , CV_RGB (255 , 255 , 255 ), 1 , CV_AA );
954969
955- if (mjpeg_port > 0 ) send_mjpeg ((mat_cv *)img, mjpeg_port, 500000 , 100 );
970+ if (mjpeg_port > 0 ) send_mjpeg ((mat_cv *)mat_to_ipl ( img) , mjpeg_port, 500000 , 100 );
956971}
957972// ----------------------------------------
958973
@@ -1034,57 +1049,50 @@ image image_data_augmentation(mat_cv* ipl, int w, int h,
10341049// ====================================================================
10351050void show_acnhors (int number_of_boxes, int num_of_clusters, float *rel_width_height_array, model anchors_data, int width, int height)
10361051{
1037- CvMat* labels = cvCreateMat (number_of_boxes, 1 , CV_32SC1);
1038- CvMat* points = cvCreateMat (number_of_boxes, 2 , CV_32FC1);
1039- CvMat* centers = cvCreateMat (num_of_clusters, 2 , CV_32FC1);
1052+ cv::Mat labels = cv::Mat (number_of_boxes, 1 , CV_32SC1);
1053+ cv::Mat points = cv::Mat (number_of_boxes, 2 , CV_32FC1);
1054+ cv::Mat centers = cv::Mat (num_of_clusters, 2 , CV_32FC1);
10401055
1041- int i, j;
1042- for (i = 0 ; i < number_of_boxes; ++i) {
1043- points->data .fl [i * 2 ] = rel_width_height_array[i * 2 ];
1044- points->data .fl [i * 2 + 1 ] = rel_width_height_array[i * 2 + 1 ];
1045- // cvSet1D(points, i * 2, cvScalar(rel_width_height_array[i * 2], 0, 0, 0));
1046- // cvSet1D(points, i * 2 + 1, cvScalar(rel_width_height_array[i * 2 + 1], 0, 0, 0));
1056+ for (int i = 0 ; i < number_of_boxes; ++i) {
1057+ points.at <float >(i, 0 ) = rel_width_height_array[i * 2 ];
1058+ points.at <float >(i, 1 ) = rel_width_height_array[i * 2 + 1 ];
10471059 }
10481060
1049- for (i = 0 ; i < num_of_clusters; ++i) {
1050- centers-> data . fl [i * 2 ] = anchors_data.centers .vals [i][0 ];
1051- centers-> data . fl [i * 2 + 1 ] = anchors_data.centers .vals [i][1 ];
1061+ for (int i = 0 ; i < num_of_clusters; ++i) {
1062+ centers. at < float >(i, 0 ) = anchors_data.centers .vals [i][0 ];
1063+ centers. at < float >(i, 1 ) = anchors_data.centers .vals [i][1 ];
10521064 }
10531065
1054- for (i = 0 ; i < number_of_boxes; ++i) {
1055- labels-> data . i [i] = anchors_data.assignments [i];
1066+ for (int i = 0 ; i < number_of_boxes; ++i) {
1067+ labels. at < int >(i, 0 ) = anchors_data.assignments [i];
10561068 }
10571069
10581070 size_t img_size = 700 ;
1059- IplImage* img = cvCreateImage ( cvSize ( img_size, img_size), 8 , 3 );
1060- cvZero (img);
1061- for (i = 0 ; i < number_of_boxes; ++i) {
1071+ cv::Mat img = cv::Mat ( img_size, img_size, CV_8UC3 );
1072+
1073+ for (int i = 0 ; i < number_of_boxes; ++i) {
10621074 CvPoint pt;
1063- pt.x = points-> data . fl [i * 2 ] * img_size / width;
1064- pt.y = points-> data . fl [i * 2 + 1 ] * img_size / height;
1065- int cluster_idx = labels-> data . i [i] ;
1075+ pt.x = points. at < float >(i, 0 ) * img_size / width;
1076+ pt.y = points. at < float >(i, 1 ) * img_size / height;
1077+ int cluster_idx = labels. at < int >(i, 0 ) ;
10661078 int red_id = (cluster_idx * (uint64_t )123 + 55 ) % 255 ;
10671079 int green_id = (cluster_idx * (uint64_t )321 + 33 ) % 255 ;
10681080 int blue_id = (cluster_idx * (uint64_t )11 + 99 ) % 255 ;
1069- cvCircle (img, pt, 1 , CV_RGB (red_id, green_id, blue_id), CV_FILLED, 8 , 0 );
1081+ cv::circle (img, pt, 1 , CV_RGB (red_id, green_id, blue_id), CV_FILLED, 8 , 0 );
10701082 // if(pt.x > img_size || pt.y > img_size) printf("\n pt.x = %d, pt.y = %d \n", pt.x, pt.y);
10711083 }
10721084
1073- for (j = 0 ; j < num_of_clusters; ++j) {
1085+ for (int j = 0 ; j < num_of_clusters; ++j) {
10741086 CvPoint pt1, pt2;
10751087 pt1.x = pt1.y = 0 ;
1076- pt2.x = centers-> data . fl [j * 2 ] * img_size / width;
1077- pt2.y = centers-> data . fl [j * 2 + 1 ] * img_size / height;
1078- cvRectangle (img, pt1, pt2, CV_RGB (255 , 255 , 255 ), 1 , 8 , 0 );
1088+ pt2.x = centers. at < float >(j, 0 ) * img_size / width;
1089+ pt2.y = centers. at < float >(j, 1 ) * img_size / height;
1090+ cv::rectangle (img, pt1, pt2, CV_RGB (255 , 255 , 255 ), 1 , 8 , 0 );
10791091 }
1080- save_cv_png ((mat_cv *)img, " cloud.png" );
1081- cvShowImage (" clusters" , img);
1082- cvWaitKey (0 );
1083- cvReleaseImage (&img);
1084- cvDestroyAllWindows ();
1085- cvReleaseMat (&labels);
1086- cvReleaseMat (&points);
1087- cvReleaseMat (¢ers);
1092+ save_mat_png (img, " cloud.png" );
1093+ cv::imshow (" clusters" , img);
1094+ cv::waitKey (0 );
1095+ cv::destroyAllWindows ();
10881096}
10891097
10901098} // extern "C"
0 commit comments