python3+openCV 获取图片中文本区域的最小外接矩形实例


Posted in Python onJune 02, 2020

我就废话不多说了,大家还是直接看代码吧!

print("thresh =",thresh)
coords = np.column_stack(np.where(thresh > 0))//获取thresh二值灰度图片中的白色文字区域的点
print("coords =",coords)

min_rect = cv2.minAreaRect(coords)//由点集获取最小矩形(包含中心坐标点、宽和高、偏转角度)
print("min_rec =",min_rect)
box = cv2.boxPoints(min_rect)//获取最小矩形的4个顶点坐标。

但是通过一下这个绘制矩形函数,画出来上述的最小矩形与文字区域偏差很大,但是获取到的偏转角度是对的。

不明白他们什么关系啊?

#  根据四点画原矩形
def drawRect(img, pt1, pt2, pt3, pt4, color, lineWidth):
  cv2.line(img, tuple(pt1), tuple(pt2), color, lineWidth)
  cv2.line(img, tuple(pt2), tuple(pt3), color, lineWidth)
  cv2.line(img, tuple(pt3), tuple(pt4), color, lineWidth)
  cv2.line(img, tuple(pt1), tuple(pt4), color, lineWidth)

有哪路朋友路过,帮一下忙,给指点一二,多谢朋友

附实验问题截图:

python3+openCV 获取图片中文本区域的最小外接矩形实例

补充知识:opencv2 3.2 类中实现提取蓝天颜色

我就废话不多说了,大家还是直接看代码吧!

#include<iostream>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;

class ColorDetector{

private:
int maxDist; //最小差距
Vec3b target ; //目标颜色
Mat result;
public:
ColorDetector():maxDist(100),target(0,0,0)
{

}
void setColorDistanceThreshold(int distance) //设置颜色差距的阈值
{
if(distance<0)
distance=0;
maxDist=distance;
}
int getColorDistanceThreshold() const //取得颜色差距的阈值
{
return maxDist;
}

void setTargetColor(uchar blue,uchar green,uchar red) //设置需要检测的颜色
{
target=Vec3b(blue,green,red);
}

void setTargetColor(Vec3b color)
{
target=color;
}

Vec3b getTargetColor() const
{
return target;
}
Mat process(const cv::Mat &image) ;
int getDistance(const Vec3b &color) ;
};

Mat ColorDetector::process(const cv::Mat &image) 
{
result.create(image.rows,image.cols,CV_8U);
Mat_<Vec3b>::const_iterator it=image.begin<Vec3b>();
Mat_<Vec3b>::const_iterator itend=image.end<Vec3b>();
Mat_<uchar>::iterator itout=result.begin<uchar>();
for ( ; it!= itend; ++it, ++itout) 
{
if (getDistance(*it)<maxDist) 
{
*itout=255;
} 
else 
{
*itout=0;
}
}
return result;
}

int ColorDetector::getDistance(const Vec3b &color) 
{
return abs(color[0]-target[0])+
abs(color[1]-target[1])+
abs(color[2]-target[2]);
}
void main()
{
ColorDetector cdetect;
Mat img=imread("C:\\Users\\Administrator\\Desktop\\工作\\testp\\boldt.jpg");
if(img.empty())
return;

cdetect.setTargetColor(230,190,130);

imshow("original",img);
imshow("result",cdetect.process(img));
waitKey(0);
}

以上这篇python3+openCV 获取图片中文本区域的最小外接矩形实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
把项目从Python2.x移植到Python3.x的经验总结
Apr 20 Python
用Python计算三角函数之acos()方法的使用
May 15 Python
Python while、for、生成器、列表推导等语句的执行效率测试
Jun 03 Python
python学习笔记之调用eval函数出现invalid syntax错误问题
Oct 18 Python
Python实现动态加载模块、类、函数的方法分析
Jul 18 Python
Django中ORM外键和表的关系详解
May 20 Python
解决python 文本过滤和清理问题
Aug 28 Python
python使用celery实现异步任务执行的例子
Aug 28 Python
wxPython:python首选的GUI库实例分享
Oct 05 Python
django ajax发送post请求的两种方法
Jan 05 Python
Eclipse配置python默认头过程图解
Apr 26 Python
Python几种酷炫的进度条的方式
Apr 11 Python
python编写一个会算账的脚本的示例代码
Jun 02 #Python
使用opencv识别图像红色区域,并输出红色区域中心点坐标
Jun 02 #Python
什么是Python中的顺序表
Jun 02 #Python
opencv 实现特定颜色线条提取与定位操作
Jun 02 #Python
Python爬虫入门有哪些基础知识点
Jun 02 #Python
Python实现进度条和时间预估的示例代码
Jun 02 #Python
python爬虫容易学吗
Jun 02 #Python
You might like
php 方便水印和缩略图的图形类
2009/05/21 PHP
允许phpmyadmin空密码登录的配置方法
2011/05/29 PHP
PHP 绘制网站登录首页图片验证码
2016/04/12 PHP
php生成图片验证码的方法
2016/04/15 PHP
thinkphp3.2.3 分页代码分享
2016/07/28 PHP
JS location几个方法小姐
2008/07/09 Javascript
jquery多行滚动/向左或向上滚动/响应鼠标实现思路及代码
2013/01/23 Javascript
js实现省市联动效果的简单实例
2014/02/10 Javascript
javascript初学者常用技巧
2014/09/02 Javascript
在Linux系统中搭建Node.js开发环境的简单步骤讲解
2016/01/26 Javascript
省市二级联动小案例讲解
2016/07/24 Javascript
JavaScript中push(),join() 函数 实例详解
2016/09/06 Javascript
connection reset by peer问题总结及解决方案
2016/10/21 Javascript
简单实现jQuery多选框功能
2017/01/09 Javascript
select下拉框插件jquery.editable-select详解
2017/01/22 Javascript
vue-axios使用详解
2017/05/10 Javascript
vue.js实现简单轮播图效果
2017/10/10 Javascript
使用js实现将后台传入的json数据放在前台显示
2018/08/06 Javascript
mpvue微信小程序开发之实现一个弹幕评论
2019/11/24 Javascript
js 获取扫码枪输入数据的方法
2020/06/10 Javascript
[02:39]DOTA2国际邀请赛助威团西雅图第一天
2013/08/08 DOTA
[01:09:20]NB vs NAVI Supermajor小组赛A组 BO3 第二场 6.2
2018/06/03 DOTA
使用Python实现博客上进行自动翻页
2017/08/23 Python
查看TensorFlow checkpoint文件中的变量名和对应值方法
2018/06/14 Python
Python用于学习重要算法的模块pygorithm实例浅析
2018/08/16 Python
Python发送邮件的实例代码讲解
2019/10/16 Python
Windows+Anaconda3+PyTorch+PyCharm的安装教程图文详解
2020/04/03 Python
Python如何读写二进制数组数据
2020/08/01 Python
Python使用grequests并发发送请求的示例
2020/11/05 Python
Melijoe美国官网:法国奢侈童装购物网站
2017/04/19 全球购物
C++如何引用一个已经定义过的全局变量
2014/08/25 面试题
幼儿园教师请假制度
2014/01/16 职场文书
投资意向书范本
2014/04/01 职场文书
演讲稿格式
2014/04/30 职场文书
法学专业毕业生求职信
2014/06/12 职场文书
保险公司岗前培训工作总结
2015/10/24 职场文书