python3+openCV 獲取圖片中文本區(qū)域的最小外接矩形實(shí)例
我就廢話不多說了,大家還是直接看代碼吧!
print("thresh =",thresh) coords = np.column_stack(np.where(thresh > 0))//獲取thresh二值灰度圖片中的白色文字區(qū)域的點(diǎn) print("coords =",coords) min_rect = cv2.minAreaRect(coords)//由點(diǎn)集獲取最小矩形(包含中心坐標(biāo)點(diǎn)、寬和高、偏轉(zhuǎn)角度) print("min_rec =",min_rect) box = cv2.boxPoints(min_rect)//獲取最小矩形的4個(gè)頂點(diǎn)坐標(biāo)。
但是通過一下這個(gè)繪制矩形函數(shù),畫出來上述的最小矩形與文字區(qū)域偏差很大,但是獲取到的偏轉(zhuǎn)角度是對的。
不明白他們什么關(guān)系啊?
# 根據(jù)四點(diǎn)畫原矩形 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)
有哪路朋友路過,幫一下忙,給指點(diǎn)一二,多謝朋友
附實(shí)驗(yàn)問題截圖:
補(bǔ)充知識:opencv2 3.2 類中實(shí)現(xiàn)提取藍(lá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 ; //目標(biāo)顏色 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ū)域的最小外接矩形實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python進(jìn)行圖片相似度對比的兩種實(shí)現(xiàn)方法
Python提供了一些庫和工具可以用于圖片的相似度比對,本文就詳細(xì)的介紹了兩種實(shí)現(xiàn)方法,感知哈希和結(jié)構(gòu)相似性,下面就來介紹一下,感興趣的可以了解一下2023-10-10Python 多進(jìn)程和數(shù)據(jù)傳遞的理解
這篇文章主要介紹了Python 多進(jìn)程和數(shù)據(jù)傳遞的理解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10Django contenttypes 框架詳解(小結(jié))
這篇文章主要介紹了Django contenttypes 框架詳解(小結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-08-08es+flask搜索小項(xiàng)目實(shí)現(xiàn)分頁+高亮的示例代碼
本文主要介紹了es+flask搜索小項(xiàng)目實(shí)現(xiàn)分頁+高亮的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01Python pandas如何根據(jù)指定條件篩選數(shù)據(jù)
這篇文章主要介紹了Python pandas如何根據(jù)指定條件篩選數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02使用ITK-SNAP進(jìn)行摳圖操作并保存mask的實(shí)例
這篇文章主要介紹了使用ITK-SNAP進(jìn)行摳圖操作并保存mask的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07