我门要对某个文件夹下全部图像文件进行统一处理,假设图像的数量过多。那么手动地一张张处理就会显得有些麻烦。本文使用OpenCV和bash来完毕我们指定的任务。

任务

将文件夹A下的全部统一格式的jpg图像变成统一尺寸的图像,输出到文件夹B中。A文件夹下图像的宽度和高度须要去掉最后一列、最后一行,而且使得输出图像的高度小于宽度。

技术

OpenCV读取图像;訪问图像中的元素。OpenCV写图像到磁盘。

BASH扫描每一个输入图像;确定输出图像名称。

OpenCV对图像进行处理

源码例如以下:

#include <cassert>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include "cv.hpp"
#include "opencv2/opencv.hpp"
//#include "opencv2/core.hpp"
#include "opencv2/highgui/highgui_c.h" using namespace std;
using namespace cv; int main(int argc, char **argv)
{
if (3 != argc){
cerr << "input error\n";
cerr << "Usage : " << argv[0] << " <input image> <output directory>" << endl;
return -1;
}
// reading the input image
Mat oim = imread(argv[1]);
if (oim.empty())
return -1; const int rows = oim.rows;
const int cols = oim.cols;
Mat fim(rows-1, cols-1, CV_8UC3); for (int r = 0; r < (rows-1); r++){
for (int c = 0; c < (cols-1); c++){
fim.at<Vec3b>(r,c) = oim.at<Vec3b>(r,c);
}}
// rotate 90'
Mat lim;
if (rows > cols){
lim.create(cols-1, rows-1, CV_8UC3);
for (int r = 0; r < (cols-1); r++){
for (int c = 0; c < (rows-1); c++){
lim.at<Vec3b>(r,c) = fim.at<Vec3b>(c,cols-2-r);
}}
}
else{
lim = fim;
}
// saving
string filename(argv[1]);
int dirpos = filename.find_last_of('/');
if (string::npos == dirpos){
dirpos = 0;
}
else{
dirpos += 1;
}
string wfn = &filename[dirpos];
string outdir = string(argv[2]);
string outfile = outdir+string("/")+wfn; vector<int> compression_params;
compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
compression_params.push_back(100);
imwrite(outfile, lim, compression_params);
if(lim.cols != 480 || lim.rows != 320)
cerr << "size error" << endl; return 0;
}

程序分三大步骤完毕:读如程序选项中的输入图像;对输入图像去除最后一行和最后一列。推断高度和宽度的要求(是否进行反转90度)。将图像写入磁盘。

写入磁盘时。使用了jpeg压缩方式,压缩的參数设置为100,表示无失真压缩。

输入图像的名称和输出图像的名称使用同一个。

bash处理

用bash对某个文件夹下的全部图像都处理一次,而且输出到指定的文件夹。源码例如以下:

SPS="input/"

DFS=`ls -A ${SPS}*.jpg`

JPGDIR="../output/jpg"

mkdir -p ${JPGDIR} 

for fn in $DFS
do
echo $fn
./rmRowACols.exe $fn $JPGDIR
done

总结

BASH+C/C++ 合作来完毕一个完整的任务,各取所长,兼顾性能和开发难度,眼下被我觉得是比較简单的方式。

这样作另一个优点:C\C++语言能够做很多其它细节,别调用别人的程序要任意一点。

最新文章

  1. Java虚拟机5:Java垃圾回收(GC)机制详解
  2. MyEclipse中拷贝J2EE项目,发布到tomcat中名字一样的解决办法
  3. oc调用rest api
  4. Trie树 &amp; 01Trie
  5. OAF_OAF Exception Handling异常处理(概念)
  6. 【LeetCode】104 - Maximum Depth of Binary Tree
  7. MonoBehaviour
  8. TortoiseSVN设置比较工具为BeyondCompare
  9. Mysql 如何做双机热备和负载均衡 (方法一)
  10. [POJ2054]Color a Tree (并查集+贪心)
  11. Spark-Unit1-spark概述与安装部署
  12. SpringBoot面试题及答案整理
  13. 经验分享 | 如何拿到自己满意的offer?
  14. Expires和Cache-Control的理解
  15. OSINT系列:网站信任评估WOT
  16. MapGis如何实现WebGIS分布式大数据存储的
  17. jquery.jCal.js显示日历插件
  18. php开启与关闭错误提示
  19. Java多线程——sychronized
  20. Python: 正则表达式匹配多行,实现多行匹配模式

热门文章

  1. 引用类型 (Reference Type Matters)、扩展与派发方式
  2. 【VBA研究】如何用Base64 编解码方法实现简单的加解密
  3. JDK1.8中的Stream详解
  4. js里的稀疏数组
  5. 【计算机网络】2.2 Web和HTTP
  6. 字符集匹配:\s 匹配一个空格,一边后面加量词表示多个空格,\s*表示0个以上空格,\s+表示1个以上空格,\s相当于[\f\r\n\t ]5种空白字符。
  7. ORA-28000: the account is locked-详细解决方案
  8. 亲测可用)html5 file调用手机摄像头
  9. 全国绿色计算大赛 模拟赛第二阶段 (Python)
  10. mysql通配符进行模糊查询