最近因為公司研發在准備深度學習,所以在截圖的工作需求測試了諸多版本的效果,CImage/OpenCV沒有深入研究,Imagemagick類有安裝使用,很方便;Matlab截圖配置起來尤其簡單,操作也很方便,以下 做部分小結。
1、原本想直接用C++ 配合MFC,在公司原來開發的ADJ程序上實現,原來的程式是建立在IPP庫上,個人對IPP庫研究不深,且一直出現“Error: a nonstatic member reference must be relative to a specific object”的報警,待后續個人能力提升會回來研究,由於目前個人能力有限,暫時放棄。
2、C++ MFC 可以使用Cimage、OpenCV、ImageMagick來實現,但自己並不想直接對公司程序安裝太多庫,因為公司軟件主要針對自動化工業應用,安裝這些類在現場計算機上並不是很方便,所以也是放棄。(期間花了兩天時間安裝和測試imagemagick庫,該庫在公司開發平台VS2008安裝失敗,后來在自己電腦上VS2017 測試ok,且可以很便捷的實現截圖功能)。
PS:關於Imagemagick和Opencv庫,在查閱資料時了解了一些功能差異部分,
- magick:magick主要是以應用的角度展示它的實現,比如圖像切割,圖像融合,圖像模糊,圖像銳化等。
- opencv:opencv主要是以算法的角度來展示它的實現,也就是說,它實際提供的是各種圖像處理算法,如果需要具體的應用,你需要組合它所提供的算法實現來實現某個功能。
詳細內容可以閱讀下面文章做一些了解 http://www.udpwork.com/item/5810.html
3、前幾天有測試一版,MFC程式對圖片做信息采集,在圖片上雙擊來確定彩圖上要截圖的坐標中心和圖片路徑,截圖大小,生成一份文本文件(Test.ini),然后將該資料導入Matlab實現截圖。
該實現方法 不是很方便,但也是一種嘗試。C++讀寫ini文件就不列代碼了,以下分別是讀寫 matlab讀寫ini文件並實現截圖的功能。
matlab本身不提供ml_GetPrivateProfileString.m函數,在官網社區內可以找到分享的。
(1)指定坐標截圖
%需要截圖的數量
cnt = ml_GetPrivateProfileString('COUNT','count','D:\Test.ini');
cnt_no = str2double(cnt);
%截圖size
crop_size = ml_GetPrivateProfileString('COUNT','crop_xy','D:\Test.ini');
crop_size_no = str2double (crop_size)-1;
%創建文件夾 存放截圖
mkdir('D:\ADJ_Crop_Image');
for a = 1:cnt_no
str_a = num2str(a);
sr_name=['Pic',str_a];
source = ml_GetPrivateProfileString(sr_name,'source','D:\Test.ini');
x_co = ml_GetPrivateProfileString(sr_name,'X-Coordinate','D:\Test.ini');
x_co_no = str2double (x_co);
y_co = ml_GetPrivateProfileString(sr_name,'Y-Coordinate','D:\Test.ini');
y_co_no = str2double (y_co);
%文件名稱 路徑+名稱+后綴
[pathstr,name,suffix]=fileparts(source);
target = ['D:\ADJ_Crop_Image\',name,suffix];
%源圖
rgb=imread(source);
%截圖
rgb1=imcrop(rgb,[x_co_no,y_co_no,crop_size_no,crop_size_no]);
imwrite(rgb1,target);
end
(2) 遍歷圖片后彈出圖片鼠標框選范圍實現截圖 (參考下面純matlab截圖)
%需要截圖的數量 % read Test.ini file and then crop image cnt = ml_GetPrivateProfileString('COUNT','count','D:\Test.ini'); cnt_no = str2double(cnt); %截圖size crop_size = ml_GetPrivateProfileString('COUNT','crop_xy','D:\Test.ini'); crop_size_no = str2double (crop_size)-1; %創建文件夾 存放截圖 mkdir('D:\ADJ_Crop_Image'); for a = 1:cnt_no str_a = num2str(a); sr_name=['Pic',str_a]; source = ml_GetPrivateProfileString(sr_name,'source','D:\Test.ini'); x_co = ml_GetPrivateProfileString(sr_name,'X-Coordinate','D:\Test.ini'); x_co_no = str2double (x_co); y_co = ml_GetPrivateProfileString(sr_name,'Y-Coordinate','D:\Test.ini'); y_co_no = str2double (y_co); %文件名稱 路徑+名稱+后綴 [pathstr,name,suffix]=fileparts(source); target = ['D:\ADJ_Crop_Image\',name,suffix]; %源圖 rgb=imread(source); %截圖 %rgb1=imcrop(rgb,[x_co_no,y_co_no,crop_size_no,crop_size_no]); %imwrite(rgb1,target); [rgb2,rect]=imcrop(rgb); imwrite(rgb2,target); end
4、純matlab版本的截圖
讀取某一路徑下的所有jpg格式的圖片,然后可以手動在圖片上框選矩形區域實現截圖功能。
%%Mouse Rect Crop Function file_path = 'D:\CaptureImage\';% file folder img_path_list = dir(strcat(file_path,'*.jpg'));%only read jpg file mkdir(strcat(file_path,'Crop')); %create new folder to save new jpg file img_num = length(img_path_list); I=cell(1,img_num); if img_num > 0 for j = 1:img_num image_name = img_path_list(j).name; image = imread(strcat(file_path,image_name)); I{j}=image; %crop image [rgb,rect]=imcrop(I{j}); % mouse rect crop imwrite(rgb,strcat(file_path,'Crop\',image_name)); %show log if j == img_num h = msgbox('All picture have been croped!') end end end