欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

openCV提取圖像中的矩形區(qū)域

 更新時(shí)間:2020年07月21日 11:30:28   作者:c19961227  
這篇文章主要為大家詳細(xì)介紹了openCV提取圖像中的矩形區(qū)域,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

改編自詳解利用OpenCV提取圖像中的矩形區(qū)域(PPT屏幕等) 原文是c++版,我改成了python版,供大家參考學(xué)習(xí)。
主要思想:邊緣檢測(cè)—》輪廓檢測(cè)—》找出最大的面積的輪廓—》找出頂點(diǎn)—》投影變換

import numpy as np
import cv2
# 這個(gè)成功的扣下了ppt白板
srcPic = cv2.imread('2345.jpg')
length=srcPic.shape[0]
depth=srcPic.shape[1]
polyPic = srcPic
shrinkedPic = srcPic
greyPic = cv2.cvtColor(shrinkedPic, cv2.COLOR_BGR2GRAY)
ret, binPic = cv2.threshold(greyPic, 130, 255, cv2.THRESH_BINARY)
print(binPic.shape)
median = cv2.medianBlur(binPic, 5)
# 進(jìn)行邊緣檢測(cè)
cannyPic = cv2.Canny(median, 10, 200)

cv2.namedWindow("binary", 0)
cv2.namedWindow("binary2", 0)
cv2.imshow("binary", cannyPic)
# 找出輪廓
contours, hierarchy = cv2.findContours(cannyPic, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
cv2.imwrite('binary2.png', cannyPic)
cv2.imshow("binary2", cannyPic)
i = 0
maxArea = 0
# 挨個(gè)檢查看那個(gè)輪廓面積最大
for i in range(len(contours)):
 if cv2.contourArea(contours[i]) > cv2.contourArea(contours[maxArea]):
 maxArea = i
#檢查輪廓得到分布在四個(gè)角上的點(diǎn)
hull = cv2.convexHull(contours[maxArea])
s = [[1,2]]
z = [[2,3]]
for i in hull:
 s.append([i[0][0],i[0][1]])
 z.append([i[0][0],i[0][1]])
del s[0]
del z[0]

#現(xiàn)在的目標(biāo)是從一堆點(diǎn)中挑出分布在四個(gè)角落的點(diǎn),決定把圖片分為四等份,每個(gè)區(qū)域的角度來(lái)劃分點(diǎn),
#默認(rèn)四個(gè)角分別分布在圖像的四等分的區(qū)間上,也就是矩形在圖像中央
# 我們把所有點(diǎn)的坐標(biāo),都減去圖片中央的那個(gè)點(diǎn)(當(dāng)成原點(diǎn)),然后按照x y坐標(biāo)值的正負(fù) 判斷屬于哪一個(gè)區(qū)間

center=[length/2,depth/2]

# 可以得到小數(shù)
for i in range(len(s)):
 s[i][0] = s[i][0] - center[0]
 s[i][1] = s[i][1] - center[1]
one = []
two = []
three = []
four = []
# 判斷是那個(gè)區(qū)間的
for i in range(len(z)):
 if s[i][0] <= 0 and s[i][1] <0 :
 one.append(i)
 elif s[i][0] > 0 and s[i][1] <0 :
 two.append(i)
 elif s[i][0] >= 0 and s[i][1] > 0:
 four.append(i)
 else:three.append(i)

p=[]
distance=0
temp = 0
# 下面開(kāi)始判斷每個(gè)區(qū)間的極值,要選擇距離中心點(diǎn)最遠(yuǎn)的點(diǎn),就是角點(diǎn)
for i in one :
 x=z[i][0]-center[0]
 y=z[i][1]-center[1]
 d=x*x+y*y
 if d > distance :
 temp = i
 distance = d
p.append([z[temp][0],z[temp][1]])
distance=0
temp=0
for i in two :
 x=z[i][0]-center[0]
 y=z[i][1]-center[1]
 d=x*x+y*y
 if d > distance :
 temp = i
 distance = d
p.append([z[temp][0],z[temp][1]])
distance=0
temp=0
for i in three :
 x=z[i][0]-center[0]
 y=z[i][1]-center[1]
 d=x*x+y*y
 if d > distance :
 temp = i
 distance = d
p.append([z[temp][0],z[temp][1]])
distance=0
temp=0
for i in four :
 x=z[i][0]-center[0]
 y=z[i][1]-center[1]
 d=x*x+y*y
 if d > distance :
 temp = i
 distance = d
p.append([z[temp][0],z[temp][1]])


for i in p:
 cv2.circle(polyPic, (i[0],i[1]),2,(0,255,0),2)
# 給四個(gè)點(diǎn)排一下順序
a=[]
b=[]
st=[]
for i in p:
 a.append(i[0])
 b.append(i[1])
index=np.lexsort((b, a))
for i in index:
 st.append(p[i])
p = st
print(p)
pts1 = np.float32([[p[0][0],p[0][1]],[p[1][0],p[1][1]],[p[2][0],p[2][1]],[p[3][0],p[3][1]]])
# dst=np.float32([[0,0],[0,srcPic.shape[1]],[srcPic.shape[0],0],[srcPic.shape[0],srcPic.shape[1]]])
dst=np.float32([[0,0],[0,600],[400,0],[400,600]])

# 投影變換
M = cv2.getPerspectiveTransform(pts1,dst)
cv2.namedWindow("srcPic2", 0)
cv2.imshow("srcPic2", srcPic)
#dstImage = cv2.warpPerspective(srcPic,M,(srcPic.shape[0],srcPic.shape[1]))
dstImage = cv2.warpPerspective(srcPic,M,(400,600))


# 在原圖上畫(huà)出紅色的檢測(cè)痕跡,先生成一個(gè)黑色圖
black = np.zeros((shrinkedPic.shape[0], shrinkedPic.shape[1]), dtype=np.uint8)
# 二值圖轉(zhuǎn)為三通道圖
black3 = cv2.merge([black, black, black])
# black=black2
cv2.drawContours(black, contours, maxArea, 255, 11)
cv2.drawContours(black3, contours, maxArea, (255, 0, 0), 11)
cv2.imwrite('cv.png', black)

cv2.namedWindow("cannyPic", 0)
cv2.imshow("cannyPic", black)
cv2.namedWindow("shrinkedPic", 0)
cv2.imshow("shrinkedPic", polyPic)
cv2.namedWindow("dstImage", 0)
cv2.imshow("dstImage", dstImage)
# 等待一個(gè)按下鍵盤(pán)事件
cv2.waitKey(0)
# 銷毀所有創(chuàng)建出的窗口

運(yùn)行效果

用到的圖片

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python使用5行代碼批量做小姐姐的素描圖

    Python使用5行代碼批量做小姐姐的素描圖

    本文主要介紹了Python使用5行代碼批量做小姐姐的素描圖,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Python中類的創(chuàng)建和實(shí)例化操作示例

    Python中類的創(chuàng)建和實(shí)例化操作示例

    這篇文章主要介紹了Python中類的創(chuàng)建和實(shí)例化操作,涉及Python面向?qū)ο蟪绦蛟O(shè)計(jì)中類的定義、實(shí)例化、方法調(diào)用等相關(guān)操作技巧,需要的朋友可以參考下
    2019-02-02
  • python與sqlite3實(shí)現(xiàn)解密chrome cookie實(shí)例代碼

    python與sqlite3實(shí)現(xiàn)解密chrome cookie實(shí)例代碼

    這篇文章主要介紹了python與sqlite3實(shí)現(xiàn)解密chrome cookie實(shí)例代碼,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • Python Selenium Cookie 繞過(guò)驗(yàn)證碼實(shí)現(xiàn)登錄示例代碼

    Python Selenium Cookie 繞過(guò)驗(yàn)證碼實(shí)現(xiàn)登錄示例代碼

    這篇文章主要介紹了Python Selenium Cookie 繞過(guò)驗(yàn)證碼實(shí)現(xiàn)登錄示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • Python-flask調(diào)用接口返回中文數(shù)據(jù)問(wèn)題

    Python-flask調(diào)用接口返回中文數(shù)據(jù)問(wèn)題

    這篇文章主要介紹了Python-flask調(diào)用接口返回中文數(shù)據(jù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • pandas 實(shí)現(xiàn)分組后取第N行

    pandas 實(shí)現(xiàn)分組后取第N行

    這篇文章主要介紹了pandas 實(shí)現(xiàn)分組后取第N行的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-03-03
  • jupyter實(shí)現(xiàn)重新加載模塊

    jupyter實(shí)現(xiàn)重新加載模塊

    這篇文章主要介紹了jupyter實(shí)現(xiàn)重新加載模塊,具有很好的價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-04-04
  • python實(shí)現(xiàn)搜索文本文件內(nèi)容腳本

    python實(shí)現(xiàn)搜索文本文件內(nèi)容腳本

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)搜索文本文件內(nèi)容的腳本,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • python字典翻轉(zhuǎn)的實(shí)現(xiàn)

    python字典翻轉(zhuǎn)的實(shí)現(xiàn)

    本文主要介紹了python字典翻轉(zhuǎn)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • Python入門(mén)教程(二十九)Python的RegEx正則表達(dá)式

    Python入門(mén)教程(二十九)Python的RegEx正則表達(dá)式

    這篇文章主要介紹了Python入門(mén)教程(二十九)Python的RegEx,RegEx 或正則表達(dá)式是形成搜索模式的字符序列。RegEx 可用于檢查字符串是否包含指定的搜索模式,需要的朋友可以參考下
    2023-04-04

最新評(píng)論