luoshijie
这几天基础OpenCV,练习写了一个去黑边程序,新手代码,记录一下。
该方法为扫描线法,遇到非黑边内的(0,0,0)黑色时,用坐标排除(不在边界上跳过)。
PS:这里还提供一种区域增长思路,找图片黑色区域,面积最大的区域为需要去除的黑边。
遍历导入图片,遍历像素,找到黑边所在的矩形框坐标,剪切图片(一分为四)
根据矩形坐标,计算新的地理位置信息
删除带有黑边的图片和tfw文件信息
打开和写入保存tif图片和tfw位置信息
//传入图片 返回图片分割的图片 得到切点坐标
QVector<Mat> separationTif(Mat &iplImg, Point2d &cutPnt)
{
QVector<Mat> vecMat;
int width = iplImg.size().width;
int height = iplImg.size().height;
int begin_wid = 0, end_wid = 0, begin_heig = 0, end_heig = 0; //标记黑边 坐标
int i = 0, j = 0;
int blackNumber = 0;
for (j = 0; j < height; ++j)
{
for (i = 0; i < width; ++i)
{
int tmpb, tmpg, tmpr;
tmpb = cvGet2D(&(IplImage)iplImg, j, i).val[0];
tmpg = cvGet2D(&(IplImage)iplImg, j, i).val[1];
tmpr = cvGet2D(&(IplImage)iplImg, j, i).val[2];
if (tmpb == 0 && tmpg == 0 && tmpr == 0) //这里RGB数字可以灵活变动
{
++blackNumber;
}
}
}
if (blackNumber == 0) //不存在黑边
{
vecMat << iplImg;
return vecMat;
}
else
{
for (j = 0; j < height; ++j)
{
bool flag = false;
for (i = 0; i < width; ++i)
{
int tmpb, tmpg, tmpr;
tmpb = cvGet2D(&(IplImage)iplImg, j, i).val[0];
tmpg = cvGet2D(&(IplImage)iplImg, j, i).val[1];
tmpr = cvGet2D(&(IplImage)iplImg, j, i).val[2];
if (tmpb == 0 && tmpg == 0 && tmpr == 0)
{
if (i > 0 && i < width - 1 && j > 0 && j < height - 1)
;
else
{
flag = true;
begin_heig = j;
break;
}
}
}
if (flag) break;
}
for (j = height - 1; j >= begin_heig; --j)
{
bool flag = false; //判断
for (i = 0; i < width; ++i)
{
int tmpb, tmpg, tmpr;
tmpb = cvGet2D(&(IplImage)iplImg, j, i).val[0];
tmpg = cvGet2D(&(IplImage)iplImg, j, i).val[1];
tmpr = cvGet2D(&(IplImage)iplImg, j, i).val[2];
if (tmpb == 0 && tmpg == 0 && tmpr == 0)
{
if (i > 0 && i < width - 1 && j > 0 && j < height - 1)
;
else
{
flag = true;
end_heig = j;
break;
}
}
}
if (flag) break;
}
for (i = 0; i < width; ++i)
{
bool flag = false;
for (j = 0; j < height; ++j)
{
int tmpb, tmpg, tmpr;
tmpb = cvGet2D(&(IplImage)iplImg, j, i).val[0];
tmpg = cvGet2D(&(IplImage)iplImg, j, i).val[1];
tmpr = cvGet2D(&(IplImage)iplImg, j, i).val[2];
if (tmpb == 0 && tmpg == 0 && tmpr == 0)
{
if (i > 0 && i < width - 1 && j > 0 && j < height - 1)
;
else
{
flag = true;
begin_wid = i;
break;
}
}
}
if (flag) break;
}
for (i = width - 1; i >= begin_wid; --i)
{
bool flag = false;
for (j = 0; j < height; ++j)
{
int tmpb, tmpg, tmpr;
tmpb = cvGet2D(&(IplImage)iplImg, j, i).val[0];
tmpg = cvGet2D(&(IplImage)iplImg, j, i).val[1];
tmpr = cvGet2D(&(IplImage)iplImg, j, i).val[2];
if (tmpb == 0 && tmpg == 0 && tmpr == 0)
{
if (i > 0 && i < width - 1 && j > 0 && j < height - 1)
;
else
{
flag = true;
end_wid = i;
break;
}
}
}
if (flag) break;
}
if (begin_wid > 0 && begin_wid < (width - 1))
cutPnt.x = begin_wid;
else
cutPnt.x = end_wid;
if (begin_heig > 0 && begin_heig < (height - 1))
cutPnt.y = begin_heig;
else
cutPnt.y = end_heig;
if (cutPnt.x != 0 || cutPnt.y != 0)
{ //裁剪的中心点不在(0,0)时 剪切图像
cout << "裁剪的中心点为:" << endl;
qDebug() << cutPnt.x << cutPnt.y;
Mat img0 = Mat(iplImg, Rect(0, 0, cutPnt.x + 1, cutPnt.y + 1)); //左上
Mat img1 = Mat(iplImg, Rect(0, cutPnt.y, cutPnt.x + 1, height - cutPnt.y - 1)); //左下
Mat img2 = Mat(iplImg, Rect(cutPnt.x, 0, width - cutPnt.x - 1, cutPnt.y + 1)); //右上
Mat img3 = Mat(iplImg, Rect(cutPnt.x, cutPnt.y, width - cutPnt.x - 1, height - cutPnt.y - 1)); //右下
vecMat << img0 << img1 << img2 << img3;
}
else
{
qDebug() << "Do not have black_border";
vecMat << iplImg;
}
return vecMat;
}
}