31_OpenCV中关于文字的操作

本文主要是关于绘制文本以及获取文字字体大小的说明。

1. 绘制文本 cv::putText()

cv::putText()是OpenCV中的一个主要文字绘制的方法,可以简单地在图像上绘制一些文字。函数声明:

void cv::putText(
	cv::Mat& img,  // image to be drawn on
	const string& text,  // write this (often from cv::format)
	cv::Point origin,  // upper-left corner of text box
	int fontFace,  // font,e.r., cv::FONT_HERSHEY_PLAIN
	double fontScale,  // size (a multiplier,not points!
	cv::Scalar color,  // color,BGR form
	int thickness = 1, // thickness of line
	int lineType = 8,  // connectedness, 4 or 8
	bool bottomLeftOrigin = false  // true = origin at lower left
);

函数说明:由text指定的文字将在以左上角为原点的文字框中以color指定的颜色绘制出来,除非bottomLeftOrigin标志被设置为真,这种情况下文字框将以左下角为原点。使用的字体由fontFace参数据决定,可取的值如下表:

标识符描述
cv::FONT_HERSHEY_SIMPLEX普通大小无衬线字体
cv::FONT_HERSHEY_PLAIN小号无衬线字体
cv::FONT_HERSHEY_DUPLEX普通大小无衬线字体,比cv::FONT_HERSHEY_SIMPLE更复杂
cv::FONT_HERSHEY_COMPLEX普通大小无衬线字体,比cv::FONT_HERSHEY_DUPLEX更复杂
cv::FONT_HERSHEY_TRIPLEX普通大小无衬线字体,比cv::FONT_HERSHEY_COMPLEX更复杂
cv::FONT_HERSHEY_COMPLEX_SMALL小号版本的cv::FONT_HERSHEY_COMPLEX
cv::FONT_HERSHEY_SCRIPT_SIMPLE手写字体
cv::FONT_HERSHEY_SCRIPT_COMPLEX比cv::FONT_HERSHEY_SCRIPT_SIMPLEX更复杂的变体

上表中的字体都可以和cv::FONT_HERSHEY_ITALIC组合使用(通过或操作)来得到斜体。每种字体都有一个自然大小,当fontScale不是1.0时,在文字绘制之前字体大小将由这个数缩放。

2. 获取文字大小 cv::getTextSize()

cv::getTextSize()回答了如果把文字绘制出来将有多大的问题,不用实际将文字绘制到图上。cv::getTextSize的新参数是baseLine,是一个输出参数,baseLine是和文字最低点相关的文字基线的y坐标值。函数声明如下:

cv::Size cv::getTextSize(
	const string& text,
	cv::Point origin,
	int fontFace,
	double fontScale,
	int thickness,
	int* baseLine
);

上面两个函数使用示例:

	cv::Mat image(400, 1100, CV_8UC3, cv::Scalar(128,128, 128));
	int ny,nHeight;
	ny = 35;
	std::string text = "cv::FONT_HERSHEY_SIMPLEX";
	cv::putText(image, text, cv::Point(10, ny), cv::FONT_HERSHEY_SIMPLEX,1.0, CV_RGB(255, 255, 0),1);
	cv::getTextSize(text, cv::FONT_HERSHEY_SIMPLEX, 1.0, 1, &nHeight);

	text = "cv::FONT_HERSHEY_PLAIN";
	ny += nHeight+10;
	cv::putText(image, text, cv::Point(10, ny), cv::FONT_HERSHEY_PLAIN, 1.0, CV_RGB(255, 255, 0), 1);
	cv::getTextSize(text, cv::FONT_HERSHEY_PLAIN, 1.0, 1, &nHeight);

	text = "cv::FONT_HERSHEY_DUPLEX";
	ny += nHeight+nHeight*4;
	cv::putText(image, text, cv::Point(10, ny), cv::FONT_HERSHEY_DUPLEX, 1.0, CV_RGB(255, 255, 0), 1);
	cv::getTextSize(text, cv::FONT_HERSHEY_DUPLEX, 1.0, 1, &nHeight);

	text = "cv::FONT_HERSHEY_COMPLEX";
	ny += nHeight + nHeight * 4;
	cv::putText(image, text, cv::Point(10, ny), cv::FONT_HERSHEY_COMPLEX, 1.0, CV_RGB(255, 255, 0), 1);
	cv::getTextSize(text, cv::FONT_HERSHEY_COMPLEX, 1.0, 1, &nHeight);

	text = "cv::FONT_HERSHEY_TRIPLEX";
	ny += nHeight + nHeight * 4;
	cv::putText(image, text, cv::Point(10, ny), cv::FONT_HERSHEY_TRIPLEX, 1.0, CV_RGB(255, 255, 0), 1);
	cv::getTextSize(text, cv::FONT_HERSHEY_TRIPLEX, 1.0, 1, &nHeight);

	text = "cv::FONT_HERSHEY_COMPLEX_SMALL small version cv::FONT_HERSHEY_COMPLEX";
	ny += nHeight + nHeight * 4;
	cv::putText(image, text, cv::Point(10, ny), cv::FONT_HERSHEY_COMPLEX_SMALL, 1.0, CV_RGB(255, 255, 0), 1);
	cv::getTextSize(text, cv::FONT_HERSHEY_COMPLEX_SMALL, 1.0, 1, &nHeight);

	text = "cv::FONT_HERSHEY_SCRIPT_SIMPLEX";
	ny += nHeight + nHeight * 4;
	cv::putText(image, text, cv::Point(10, ny), cv::FONT_HERSHEY_SCRIPT_SIMPLEX, 1.0, CV_RGB(255, 255, 0), 1);
	cv::getTextSize(text, cv::FONT_HERSHEY_SCRIPT_SIMPLEX, 1.0, 1, &nHeight);

	text = "cv::FONT_HERSHEY_SCRIPT_COMPLEX";
	ny += nHeight + nHeight * 4;
	cv::putText(image, text, cv::Point(10, ny), cv::FONT_HERSHEY_SCRIPT_COMPLEX, 1.0, CV_RGB(255, 255, 0), 1);
	cv::getTextSize(text, cv::FONT_HERSHEY_SCRIPT_COMPLEX, 1.0, 1, &nHeight);

	cv::namedWindow("image", cv::WINDOW_AUTOSIZE);
	cv::imshow("image", image);
	cv::waitKey(0);
	cv::destroyAllWindows();

显示结果:

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要过滤掉图片文字和数字,可以使用以下步骤: 1. 将图片转换为灰度图像。 2. 通过二值化将图像转换为黑白图像。可以使用 Otsu's 二值化算法自动确定阈值,也可以手动设置阈值。 3. 使用形态学操作(如腐蚀和膨胀)去除噪声,并将字符和数字分离开来。 4. 使用轮廓检测来识别和过滤掉字符和数字的轮廓。可以使用 OpenCV 的 findContours 函数来实现。 代码示例: ```java import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfPoint; import org.opencv.core.Scalar; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; import java.util.ArrayList; import java.util.List; public class FilterTextAndNumbers { public static void main(String[] args) { // 加载 OpenCV 库 System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // 读取图片 Mat image = Imgcodecs.imread("example.jpg", Imgcodecs.IMREAD_GRAYSCALE); // 二值化图像 Mat binaryImage = new Mat(); Imgproc.threshold(image, binaryImage, 0, 255, Imgproc.THRESH_BINARY_INV + Imgproc.THRESH_OTSU); // 形态学操作 Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new org.opencv.core.Size(3, 3)); Imgproc.erode(binaryImage, binaryImage, kernel); Imgproc.dilate(binaryImage, binaryImage, kernel); // 轮廓检测 List<MatOfPoint> contours = new ArrayList<>(); Mat hierarchy = new Mat(); Imgproc.findContours(binaryImage, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE); // 过滤字符和数字轮廓 for (int i = 0; i < contours.size(); i++) { MatOfPoint contour = contours.get(i); double area = Imgproc.contourArea(contour); if (area < 100 || area > 1000) { // 根据轮廓面积过滤 continue; } MatOfPoint2f curve = new MatOfPoint2f(contour.toArray()); double perimeter = Imgproc.arcLength(curve, true); if (perimeter < 100 || perimeter > 300) { // 根据轮廓周长过滤 continue; } // 将字符和数字部分涂成白色 Imgproc.drawContours(image, contours, i, new Scalar(255, 255, 255), -1); } // 显示处理后的图像 Imgcodecs.imwrite("result.jpg", image); } } ``` 在这个示例,我们首先将图片转换为灰度图像,然后使用 Otsu's 二值化算法将其转换为黑白图像。接下来,我们使用腐蚀和膨胀操作去除噪声,并使用轮廓检测来识别和过滤掉字符和数字的轮廓。最后,我们将字符和数字部分涂成白色,以便于在最终的结果图像过滤掉它们。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值