logo资料库

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

第1页 / 共2页
第2页 / 共2页
资料共2页,全文预览结束
python3+openCV 获取图片中文本区域的最小外接矩形实例 获取图片中文本区域的最小外接矩形实例 主要介绍了python3+openCV 获取图片中文本区域的最小外接矩形实例,具有很好的参考价值,希望对大家有 所帮助。一起跟随小编过来看看吧 我就废话不多说了,大家还是直接看代码吧! 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) 有哪路朋友路过,帮一下忙,给指点一二,多谢朋友 附实验问题截图: 补充知识:opencv2 3.2 类中实现提取蓝天颜色 补充知识: 类中实现提取蓝天颜色 我就废话不多说了,大家还是直接看代码吧! #include #include #include 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_::const_iterator it=image.begin(); Mat_::const_iterator itend=image.end(); Mat_::iterator itout=result.begin(); for ( ; it!= itend; ++it, ++itout) { if (getDistance(*it)
分享到:
收藏