前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python3+openCV 获取图片中文本区域的最小外接矩形实例

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

作者头像
砸漏
发布2020-11-02 10:20:13
1.9K0
发布2020-11-02 10:20:13
举报
文章被收录于专栏:恩蓝脚本

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

代码语言:javascript
复制
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个顶点坐标。

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

不明白他们什么关系啊?

代码语言:javascript
复制
#  根据四点画原矩形
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)

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

附实验问题截图:

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

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

代码语言:javascript
复制
#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 获取图片中文本区域的最小外接矩形实例就是小编分享给大家的全部内容了,希望能给大家一个参考。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档