【OpenCV】去除图片黑色边框及折角部分的黑色边框(彩色图像和灰度图像均可)

图片处理:去除图片周围的黑色边框及折角产生的边框

/************************************************************************/
/* 消除图片四周的黑色边框                                               */
/************************************************************************/
void RemoveBlackBorder(Mat &iplImg, Mat &dstImg)
{
	int width = iplImg.size().width;
	int height = iplImg.size().height;
	int a = 0, b = 0, c = 0, d = 0;
	int i = 0, j = 0;

	if (iplImg.channels() == 1)	//灰度图片
	{
		//消除黑色边框:上
		for (j = 0; j(j, i)<30)
				{
					;
				}
				else
				{
					flag = true;
					a = j;
					break;
				}
			}
			if (flag) break;
		}
		//消除黑色边框:下
		for (j = height - 1; j >= a; j--)
		{
			bool flag = false;
			for (i = 0; i(j, i)<30)
				{
					;
				}
				else
				{
					flag = true;
					b = j;
					break;
				}
			}
			if (flag) break;
		}
		//消除黑色边框:左
		for (i = 0; i(j, i)<30)
				{
					;
				}
				else
				{
					flag = true;
					c = i;
					break;
				}
			}
			if (flag) break;
		}
		//消除黑色边框:右
		for (i = width - 1; i >= c; i--)
		{
			bool flag = false;
			for (j = 0; j(j, i)<30)
				{
					;
				}
				else
				{
					flag = true;
					d = i;
					break;
				}
			}
			if (flag) break;
		}
	}
	else if (iplImg.channels() == 3)	//彩色图片
	{
		//消除黑色边框:上
		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 <= 30 && tmpg <= 30 && tmpr <= 30)
				{
					;
				}
				else
				{
					flag = true;
					a = j;
					break;
				}
			}
			if (flag) break;
		}
		//printf("上 a: %d\n", a);

		//消除黑色边框:下
		for (j = height - 1; j >= a; 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 <= 30 && tmpg <= 30 && tmpr <= 30)
				{
					;
				}
				else
				{
					flag = true;
					b = j;
					break;
				}
			}
			if (flag) break;
		}
		//printf("下 b: %d\n", b);

		//消除黑色边框:左
		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 <= 30 && tmpg <= 30 && tmpr <= 30)
				{
					;
				}
				else
				{
					flag = true;
					c = i;
					break;
				}
			}
			if (flag) break;
		}
		//printf("左 c: %d\n", c);

		//消除黑色边框:右
		for (i = width - 1; i >= c; 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 <= 30 && tmpg <= 30 && tmpr <= 30)
				{
					;
				}
				else
				{
					flag = true;
					d = i;
					break;
				}
			}
			if (flag) break;
		}
		//printf("右 d: %d\n", d);
	}

	//复制图像
	int w = d - c + 1, h = b - a + 1;
	dstImg = Mat(iplImg, Rect(c, a, w, h));

	return;
}

/************************************************************************/
/* 消除图片四周的黑色边角区域                                           */
/************************************************************************/
Mat RemoveBlackCorner(Mat img)
{
	int i, j;
	int h = img.size().height;
	int w = img.size().width;

	if (img.channels() == 1)	//灰度图片
	{
		for (j = 0; j(j, i)<30)
				{
					img.at(j, i) = 255;
				}
				else
				{
					break;
				}
			}
			for (i = w - 1; i >= 0; i--)
			{
				if (img.at(j, i)<30)
				{
					img.at(j, i) = 255;
				}
				else
				{
					break;
				}
			}
		}
		for (i = 0; i(j, i)<30)
				{
					img.at(j, i) = 255;
				}
				else
				{
					break;
				}
			}
			for (j = h - 1; j >= 0; j--)
			{
				if (img.at(j, i)<30)
				{
					img.at(j, i) = 255;
				}
				else
				{
					break;
				}
			}
		}
	}
	else if (img.channels() == 3)	//彩色图片
	{
		for (j = 0; j= 0; i--)
			{
				int tmpb, tmpg, tmpr;
				tmpb = cvGet2D(&(IplImage)img, j, i).val[0];
				tmpg = cvGet2D(&(IplImage)img, j, i).val[1];
				tmpr = cvGet2D(&(IplImage)img, j, i).val[2];
				if (tmpb <= 30 && tmpg <= 30 && tmpr <= 30)
				{
					cvSet2D(&(IplImage)img, j, i, CvScalar(255, 255, 255, 0));
				}
				else
				{
					break;
				}
			}
		}
		for (i = 0; i= 0; j--)
			{
				int tmpb, tmpg, tmpr;
				tmpb = cvGet2D(&(IplImage)img, j, i).val[0];
				tmpg = cvGet2D(&(IplImage)img, j, i).val[1];
				tmpr = cvGet2D(&(IplImage)img, j, i).val[2];
				if (tmpb <= 30 && tmpg <= 30 && tmpr <= 30)
				{
					cvSet2D(&(IplImage)img, j, i, CvScalar(255, 255, 255, 0));
				}
				else
				{
					break;
				}
			}
		}
	}

	return img;
}

int main()
{
	Mat iplImg = imread("ori.jpg", CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH);
	Mat img;
	RemoveBlackBorder(iplImg, img);
	imwrite("1.png", img);

	img = RemoveBlackCorner(img);
	imwrite("2.png", img);

	cout << "OK" << endl;
	getchar();
	return 0;
}

 

 

程序效果如下:

 

 

原图:

【OpenCV】去除图片黑色边框及折角部分的黑色边框(彩色图像和灰度图像均可)_第1张图片

去除黑色边框后(1.png):

【OpenCV】去除图片黑色边框及折角部分的黑色边框(彩色图像和灰度图像均可)_第2张图片

去除折角产生的黑色边框(2.png):

【OpenCV】去除图片黑色边框及折角部分的黑色边框(彩色图像和灰度图像均可)_第3张图片

你可能感兴趣的:(图像处理,OpenCV,OpenCV)