最近因为公司研发在准备深度学习,所以在截图的工作需求测试了诸多版本的效果,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