因為Hi3559AV100後期深度學習開發需要用到.bgr格式的圖片,而目前在手的一般為.jpg或.bmp格式的圖片,下面隨筆將給出基於OpenCV-2.4.13的格式轉換,實現Hi35xx .jpg/.bmp格式轉.bgr格式轉換。
VS2015上OpenCV-2.4.13安裝
環境
Win10
VS2015 win32 Debug
操作步驟
現在給出OpenCV-2.4.13安裝具體細節:
首先給出下載地址,OpenCV是開源免費的,通過網站直接下載:https://opencv.org/releases/#
下載好之後,把下載包壓縮到資料夾下(注意沒有中文等),壓縮好之後,先在win10上配置好環境變數,具體如下操作:
在系統環境PATH變數下新增opencv\build\x86\vc14\bin路徑:(若VS2015需要x64 Debug,則新增x64的bin路徑,也可以兩個都新增,方便後面使用) 記得一定要重啟
新增完系統路徑後,開啟VS2015,建立Win32控制檯應用程式,點選完成之後,下一步選擇空專案建立:
建立完成之後,選擇屬性管理器:
隨後在屬性管理器介面點開Debug|Win32,雙擊Microsoft.Cpp.Win32.user:
隨後在VC++目錄下新增包含目錄、庫目錄,新增具體內容如下:
隨後在連結器下面選擇輸入,新增附加依賴項:
附加依賴項新增具體內容如下:(帶D表示為Debug模式,否則為Release模式)
1 opencv_calib3d2413d.lib 2 opencv_contrib2413d.lib 3 opencv_core2413d.lib 4 opencv_features2d2413d.lib 5 opencv_flann2413d.lib 6 opencv_gpu2413d.lib 7 opencv_highgui2413d.lib 8 opencv_imgproc2413d.lib 9 opencv_legacy2413d.lib 10 opencv_ml2413d.lib 11 opencv_nonfree2413d.lib 12 opencv_objdetect2413d.lib 13 opencv_ocl2413d.lib 14 opencv_photo2413d.lib 15 opencv_stitching2413d.lib 16 opencv_superres2413d.lib 17 opencv_ts2413d.lib 18 opencv_video2413d.lib 19 opencv_videostab2413d.lib
雖然在工程下建立原始檔即可編輯,輸入如下內容進行測試:
1 #include <opencv.hpp> 2 using namespace cv; 3 int main() 4 { 5 Mat srcImage; 6 srcImage = imread("C:/Users/PC/Desktop/jpg_bgr/10_MariaCallas_28_f.jpg"); 7 8 imshow("顯示影像", srcImage); 9 10 waitKey(); 11 12 return 0; 13 }
測試通過後,進行Hi35xx .jpg/.bmp格式轉.bgr格式實現。
Hi35xx .jpg/.bmp格式轉.bgr OpenCV實現
下面直接給出程式碼,供大家使用:
程式碼實現
1 #include <stdio.h> 2 #include <windows.h> 3 #include <math.h> 4 #include <iostream> 5 #include <string> 6 7 #include "opencv2/opencv.hpp" 8 #include "opencv2/highgui/highgui.hpp" 9 #include "opencv2/imgproc/imgproc.hpp" 10 11 using namespace cv; 12 13 typedef unsigned char U_CHAR; 14 15 int main() 16 { 17 const char *filename = "C:/Users/PC/Desktop/jpg_bgr/10_MariaCallas_28_f.jpg"; 18 char *outname = "C:/Users/PC/Desktop/jpg_bgr/10_MariaCallas_28_f.bgr"; 19 int flag = 1; 20 21 cv::Mat img = cv::imread(filename); 22 if (!img.data) 23 { 24 printf("read image error\n"); 25 return -1; 26 } 27 28 //縮放 29 resize(img, img, Size(112, 112)); //224x224 30 //imshow("img",img); 31 //waitKey(0); 32 33 U_CHAR *data = (U_CHAR*)img.data; 34 int step = img.step; 35 printf("Step: %d, height: %d, width: %d\n", 36 step, img.rows, img.cols); 37 38 FILE *fp = fopen(outname, "wb"); 39 int h = img.rows; 40 int w = img.cols; 41 int c = img.channels(); 42 43 for (int k = 0; k<c; k++) { 44 for (int i = 0; i<h; i++) { 45 for (int j = 0; j<w; j++) { 46 fwrite(&data[i*step + j*c + k], sizeof(U_CHAR), 1, fp); 47 } 48 } 49 } 50 fclose(fp); 51 52 return 0; 53 }
112×112 轉換.bgr測試結果