python3+openCV 獲取圖片中文本區(qū)域的最小外接矩形實例
我就廢話不多說了,大家還是直接看代碼吧!
print("thresh =",thresh) coords = np.column_stack(np.where(thresh > 0))//獲取thresh二值灰度圖片中的白色文字區(qū)域的點 print("coords =",coords) min_rect = cv2.minAreaRect(coords)//由點集獲取最小矩形(包含中心坐標點、寬和高、偏轉(zhuǎn)角度) print("min_rec =",min_rect) box = cv2.boxPoints(min_rect)//獲取最小矩形的4個頂點坐標。
但是通過一下這個繪制矩形函數(shù),畫出來上述的最小矩形與文字區(qū)域偏差很大,但是獲取到的偏轉(zhuǎn)角度是對的。
不明白他們什么關(guān)系?。?/p>
# 根據(jù)四點畫原矩形 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 類中實現(xiàn)提取藍天顏色
我就廢話不多說了,大家還是直接看代碼吧!
#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) //設(shè)置顏色差距的閾值 { if(distance<0) distance=0; maxDist=distance; } int getColorDistanceThreshold() const //取得顏色差距的閾值 { return maxDist; } void setTargetColor(uchar blue,uchar green,uchar red) //設(shè)置需要檢測的顏色 { 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 獲取圖片中文本區(qū)域的最小外接矩形實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Django contenttypes 框架詳解(小結(jié))
這篇文章主要介紹了Django contenttypes 框架詳解(小結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08es+flask搜索小項目實現(xiàn)分頁+高亮的示例代碼
本文主要介紹了es+flask搜索小項目實現(xiàn)分頁+高亮的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01Python pandas如何根據(jù)指定條件篩選數(shù)據(jù)
這篇文章主要介紹了Python pandas如何根據(jù)指定條件篩選數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02