python+opencv瀹炵幇鏈哄櫒瑙嗚鍩虹鎶€鏈?杈圭紭鎻愬彇锛屽浘鍍忔护娉紝杈圭紭妫€娴嬬畻瀛愶紝鎶曞奖锛岃溅鐗屽瓧绗﹀垎鍓?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python+opencv瀹炵幇鏈哄櫒瑙嗚鍩虹鎶€鏈?杈圭紭鎻愬彇锛屽浘鍍忔护娉紝杈圭紭妫€娴嬬畻瀛愶紝鎶曞奖锛岃溅鐗屽瓧绗﹀垎鍓?相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e8%80%8c%e4%b8%94' title='鑰屼笖'>鑰屼笖   license   ann   搴旂敤   cal   鍏朵粬   妯″紡璇嗗埆   mamicode   

??鏈哄櫒瑙嗚鏄汉宸ユ櫤鑳芥鍦ㄥ揩閫熷彂灞曠殑涓€涓垎鏀€傜畝鍗曡鏉ワ紝鏈哄櫒瑙嗚灏辨槸鐢ㄦ満鍣ㄤ唬鏇夸汉鐪兼潵鍋氭祴閲忓拰鍒ゆ柇銆傚畠鏄竴椤圭患鍚堟妧鏈紝鍖呮嫭鍥惧儚澶勭悊銆佹満姊板伐绋嬫妧鏈€佹帶鍒躲€佺數鍏夋簮鐓ф槑銆佸厜瀛︽垚鍍忋€佷紶鎰熷櫒銆佹ā鎷熶笌鏁板瓧瑙嗛鎶€鏈€佽绠楁満杞‖浠舵妧鏈?鍥惧儚澧炲己鍜屽垎鏋愮畻娉曘€佸浘鍍忓崱銆?I/O鍗$瓑)銆?

??涓嬮潰浠嬬粛涓€浜涙満鍣ㄨ瑙夌殑鍩虹鏂规硶锛岀敤鍒扮殑鎶€鏈槸python+opencv銆俻ython鏄竴绉嶅緢鏂逛究鐨勯珮绾х紪绋嬭瑷€锛屼唬鐮侀噺灏戯紝鑰孫penCV鏄竴涓熀浜嶣SD璁稿彲鍙戣鐨勮法骞冲彴璁$畻鏈鸿瑙夊簱锛屽彲浠ヨ繍琛屽湪Linux銆乄indows銆丄ndroid鍜孧ac OS鎿嶄綔绯荤粺涓娿€傚畠杞婚噺绾ц€屼笖楂樻晥鈥斺€旂敱涓€绯诲垪 C 鍑芥暟鍜屽皯閲?C++ 绫绘瀯鎴愶紝鍚屾椂鎻愪緵浜哖ython銆丷uby銆丮ATLAB绛夎瑷€鐨勬帴鍙o紝瀹炵幇浜嗗浘鍍忓鐞嗗拰璁$畻鏈鸿瑙夋柟闈㈢殑寰堝閫氱敤绠楁硶銆?/p>

1.瀵瑰浘鍍忚繘琛岄槇鍊煎垎鍓插苟鍙嶈壊

??棣栧厛闇€瑕佹柊寤轰竴涓猵ython鏂囦欢锛屽鍏v2鐨勫簱锛圤penCV2鐨刾ython搴擄級锛屽苟鏄剧ず涓€寮犲浘鐗囷紝浠g爜涓猴細

import cv2

# 璇诲彇鏈浉瀵硅矾寰勪笅鐨刬nitial.bmp鏂囦欢
image = cv2.imread ("initial.bmp")  
# 灏唅mage瀵瑰簲鍥惧儚鍦ㄥ浘鍍忕獥鍙f樉绀哄嚭鏉?cv2.imshow(鈥榠nitial鈥?image)
# waitKey浣跨獥鍙d繚鎸侀潤鎬佺洿鍒扮敤鎴锋寜涓嬩竴涓敭
cv2.waitKey(0)

鎶€鏈浘鐗? src=

??瀵瑰浘鍍忚繘琛岄槇鍊煎垎鍓诧紝闃堝€艰瀹氫负80锛屽緱鍒颁簩鍊煎寲鐏板害鍥撅紝浠g爜涓猴細

# 瀵瑰浘鍍忚繘琛岄槇鍊煎垎鍓诧紝闃堝€艰瀹氫负80锛屽緱鍒颁簩鍊煎寲鐏板害鍥?ret,image1 = cv2.threshold(image,80,255,cv2.THRESH_BINARY)
cv2.imshow(鈥榞rayscale鈥?image1)

鎶€鏈浘鐗? src=

??灏嗗浘鍍忚繘琛屽弽鑹诧紝浠g爜濡備笅锛?/p>

image2 = image1.copy()		# 澶嶅埗鍥剧墖
for i in range(0,image1.shape[0]):	#image.shape琛ㄧず鍥惧儚鐨勫昂瀵稿拰閫氶亾淇℃伅(楂?瀹?閫氶亾)
for j in range(0,image1.shape[1]):
	image2[i,j]= 255 - image1[i,j]
cv2.imshow(鈥榗olorReverse鈥?image2)

鎶€鏈浘鐗? src=

2.杈圭紭鎻愬彇

??涓嬮潰灏辨槸杈圭紭鎻愬彇浜嗭紝鐢╢indContours宸奖娉曟垨鑰匔anny鏂规硶妫€娴嬭竟缂橈紝鐢ㄥ師鍥惧儚鍑忓幓鑵愯殌鍚庣殑鏀剁缉鍥惧儚锛屾彁鍙栬竟缂樸€備唬鐮佸涓嬶細

# 杈圭紭鎻愬彇
img = cv2.cvtColor(image2,cv2.COLOR_BGR2GRAY)
canny_img_one = cv2.Canny(img,300,150)
canny_img_two = canny_img_one.copy()	# 澶嶅埗鍥剧墖
for i in range(0,canny_img_one.shape[0]):	#image.shape琛ㄧず鍥惧儚鐨勫昂瀵稿拰閫氶亾淇℃伅(楂?瀹?閫氶亾)
	for j in range(0,canny_img_one.shape[1]):
		canny_img_two[i,j]= 255 - canny_img_one[i,j]
cv2.imshow(鈥榚dge鈥?canny_img_two)

??鏈€缁堟垜浠氨鍙互寰楀埌涓€涓竟缂樻彁鍙栧悗鐨勫浘褰紝濡備笅锛?br> 鎶€鏈浘鐗? src=

浜岋細鍥惧儚婊ゆ尝

??鍥惧儚婊ゆ尝锛屽嵆鍦ㄥ敖閲忎繚鐣欏浘鍍忕粏鑺傜壒寰佺殑鏉′欢涓嬪鐩爣鍥惧儚鐨勫櫔澹拌繘琛屾姂鍒讹紝鏄浘鍍忛澶勭悊涓笉鍙己灏戠殑鎿嶄綔锛屽叾澶勭悊鏁堟灉鐨勫ソ鍧忓皢鐩存帴褰卞搷鍒板悗缁浘鍍忓鐞嗗拰鍒嗘瀽鐨勬湁鏁堟€у拰鍙潬鎬с€?

??鍣0灏辨槸鐢变簬鎴愬儚绯荤粺銆佷紶杈撲粙璐ㄥ拰璁板綍璁惧绛夌殑涓嶅畬鍠勶紝鏁板瓧鍥惧儚鍦ㄥ叾褰㈡垚銆佷紶杈撹褰曡繃绋嬩腑鎴栬€呭湪鍥惧儚澶勭悊鐨勬煇浜涚幆鑺傚綋杈撳叆鐨勫儚瀵硅薄骞朵笉濡傞鎯虫椂鍙楀埌鐨勬薄鏌撱€?

??鑰屽浘鍍忔护娉㈡湁寰堝绉嶆护娉㈡柟寮忥紝鏈瀹為獙閲囩敤鐨勬护娉㈡柟寮忔槸鍧囧€兼护娉紝涓€兼护娉㈠拰楂樻柉婊ゆ尝锛屼互鍙婇珮鏂竟缂樻娴嬨€?

??鍧囧€兼护娉㈡槸鍏稿瀷鐨勭嚎鎬ф护娉㈢畻娉曪紝瀹冩槸鎸囧湪鍥惧儚涓婂鐩爣鍍忕礌缁欎竴涓ā鏉匡紝璇ユā鏉垮寘鎷簡鍏跺懆鍥寸殑涓磋繎鍍忕礌锛堜互鐩爣鍍忕礌涓轰腑蹇冪殑鍛ㄥ洿8涓儚绱狅紝鏋勬垚涓€涓护娉㈡ā鏉匡紝鍗冲幓鎺夌洰鏍囧儚绱犳湰韬級锛屽啀鐢ㄦā鏉夸腑鐨勫叏浣撳儚绱犵殑骞冲潎鍊兼潵浠f浛鍘熸潵鍍忕礌鍊笺€?

??涓€兼护娉㈡槸鍩轰簬鎺掑簭缁熻鐞嗚鐨勪竴绉嶈兘鏈夋晥鎶戝埗鍣0鐨勯潪绾挎€т俊鍙峰鐞嗘妧鏈紝涓€兼护娉㈢殑鍩烘湰鍘熺悊鏄妸鏁板瓧鍥惧儚鎴栨暟瀛楀簭鍒椾腑涓€鐐圭殑鍊肩敤璇ョ偣鐨勪竴涓偦鍩熶腑鍚勭偣鍊肩殑涓€间唬鏇匡紝璁╁懆鍥寸殑鍍忕礌鍊兼帴杩戠殑鐪熷疄鍊硷紝浠庤€屾秷闄ゅ绔嬬殑鍣0鐐广€?

??楂樻柉婊ゆ尝鏄竴绉嶇嚎鎬у钩婊戞护娉紝閫傜敤浜庢秷闄ら珮鏂櫔澹帮紝骞挎硾搴旂敤浜庡浘鍍忓鐞嗙殑鍑忓櫔杩囩▼銆傞珮鏂护娉㈠氨鏄鏁村箙鍥惧儚杩涜鍔犳潈骞冲潎鐨勮繃绋嬶紝姣忎竴涓儚绱犵偣鐨勫€硷紝閮界敱鍏舵湰韬拰閭诲煙鍐呯殑鍏朵粬鍍忕礌鍊肩粡杩囧姞鏉冨钩鍧囧悗寰楀埌銆?

??杈圭紭妫€娴嬬殑鐩殑鏄爣璇嗘暟瀛楀浘鍍忎腑浜害鍙樺寲鏄庢樉鐨勭偣銆傞珮鏂竟缂樻娴嬫槸鐢ㄩ珮鏂护娉㈢殑鏂瑰紡杩涜杈圭紭妫€娴嬨€?

??姝ラ濡備笅锛?/p>

1.璇诲彇鍘熷浘

??棣栧厛灞曠ず鍘熷浘锛屼唬鐮佸涓嬶細

import cv2
import cv2 as cv

# 璇诲彇鏈浉瀵硅矾寰勪笅鐨刬nitial.bmp鏂囦欢
image = cv2.imread ("initial.png")
# 鍔犲叆鏂囨湰淇℃伅
cv2.putText(image,鈥榠nitial鈥?(50,50),cv2.FONT_HERSHEY_SIMPLEX,1.5,(255,0, 0),4)
# 灏唅mage瀵瑰簲鍥惧儚鍦ㄥ浘鍍忕獥鍙f樉绀哄嚭鏉?cv2.imshow(鈥榠nitial鈥?image)
cv2.waitKey(0)

鎶€鏈浘鐗? src=

# 鍧囧€兼护娉?image2 = cv2.blur(image,(10,5)) cv2.putText(image2,鈥榓verageFiltering鈥?(50,50),cv2.FONT_HERSHEY_SIMPLEX,1.5,(255,0, 0),4) cv2.imshow(鈥榓verageFiltering鈥?image2)

鎶€鏈浘鐗? src=

image3 = cv2.medianBlur(image, 5) cv2.putText(image3,鈥榤edianFiltering鈥?(50,50),cv2.FONT_HERSHEY_SIMPLEX,1.5,(255,0, 0),4) cv2.imshow(鈥榤edianFiltering鈥?image3)

鎶€鏈浘鐗? src=

4.楂樻柉婊ゆ尝

??涔嬪悗杩涜楂樻柉婊ゆ尝锛屼唬鐮佸涓嬶細

# 楂樻柉婊ゆ尝
image4 = cv2.GaussianBlur(image,(5,5),0)
cv2.putText(image4,鈥榞aussianFilter鈥?(50,50),cv2.FONT_HERSHEY_SIMPLEX,1.5,(255,0, 0),4)
cv2.imshow(鈥榞aussianFilter鈥?image4)

鎶€鏈浘鐗? src=

# 楂樻柉杈圭紭妫€娴?gau_matrix = np.asarray([[-2/28,-5/28,-2/28],[-5/28,28/28,-5/28],[-2/28,-5/28,-2/28]]) img = np.zeros(image.shape) hight,width = image.shape for i in range(1,hight-1): for j in range(1,width-1): img[i-1,j-1] = np.sum(image[i-1:i+2,j-1:j+2]*gau_matrix) image5 = img.astype(np.uint8) cv2.putText(image5,鈥榞aussianEdgeDetection鈥?(50,50),cv2.FONT_HERSHEY_SIMPLEX,1.5,(255,0, 0),4) cv2.imshow(鈥榞aussianEdgeDetection鈥?image5)

鎶€鏈浘鐗? src=

1.鏄剧ず鍘熷浘

??棣栧厛鐢ㄤ笅闈㈢殑浠g爜琛ㄧ幇鍑哄師鍥惧儚锛?/p>

# 璇诲彇鏈浉瀵硅矾寰勪笅鐨刣ip_switch_02.bmp鏂囦欢
src_s = cv2.imread ("dip_switch_02.bmp",0)
cv2.imshow(鈥榮rc_s鈥?src_s)

鎶€鏈浘鐗? src=

src = cv.imread("dip_switch_02.bmp") height, width, channels = src.shape for row in range(height): for list in range(width): for c in range(channels): pv = src[row, list, c] src[row, list, c] = 255 - pv cv.imshow("AfterDeal", src)

鎶€鏈浘鐗? src=

# 鐢╯obel鏂规硶杩涜杈圭紭妫€娴?x = cv2.Sobel(src,cv2.CV_16S,1,0) y = cv2.Sobel(src,cv2.CV_16S,0,1) absX = cv2.convertScaleAbs(x) # 杞洖uint8 # cv2.imshow("absX", absX) # 鎴浘鍚庡弽杞?def inverse_color(image): height, width, channels = image.shape for row in range(height): for list in range(width): for c in range(channels): pv = image[row, list, c] image[row, list, c] = 255 - pv cv.imshow("result", image) one = cv.imread("2.jpg") inverse_color(one)

鎶€鏈浘鐗? src=

# 鐢╮obert鏂规硶杩涜杈圭紭妫€娴?dst = cv2.addWeighted(absX,0.5,absY,0.5,0) # cv2.imshow("dst", dst) # 鎴浘鍚庡弽杞?def inverse_color(image): height, width, channels = image.shape for row in range(height): for list in range(width): for c in range(channels): pv = image[row, list, c] image[row, list, c] = 255 - pv cv.imshow("result", image) three = cv.imread ("3.jpg") inverse_color(three)

鎶€鏈浘鐗? src=

鍥涳細鎶曞奖

??鎶曞奖灏辨槸灏辨槸鎶婁竴涓満鏅姇褰卞埌鎽勫儚鏈虹殑鍍忓钩闈笂銆傜被鍨嬫湁閫忚鎶曞奖锛屼豢灏勬姇褰憋紝寮遍€忚鎶曞奖涓庣被閫忚鎶曞奖绛夈€?

??鍦╫pencv涓紝鎶曞奖涓昏鍒嗕负姘村钩鎶曞奖鍜屽瀭鐩存姇褰便€傛按骞虫姇褰辨槸浜岀淮鍥惧儚鍦▂杞翠笂鐨勬姇褰憋紱鍨傜洿鎶曞奖鏄簩缁村浘鍍忓湪x杞翠笂鐨勬姇褰便€?

??鍦ㄦ按骞冲拰鍨傜洿鐨勬柟鍚戜笂杩涜鎶曞奖锛屽厛灏嗘姇褰辩殑鍥惧儚杞寲涓虹伆搴﹀浘锛岀劧鍚庤繘琛屼簩鍊煎寲闃堝€煎垎鍓诧紝涔嬪悗鍦ㄦ按骞冲拰鍨傜洿鐨勬柟鍚戜笂杩涜鎶曞奖銆傛墍鐢ㄥ簱涓篶v2浠ュ強matplotlib鏁版嵁鍒嗘瀽搴撹繘琛屾搷浣溿€?br> ??姝ラ濡備笅锛?/p>

1.鏄剧ず鍘熷浘

import cv2 
import numpy as np 
from matplotlib import pyplot as plt 

img=cv2.imread(鈥?23.jpg鈥?

鎶€鏈浘鐗? src=

2.鍨傜洿鏂瑰悜鎶曞奖

img=cv2.imread(鈥?23.jpg鈥? #璇诲彇鍥剧墖锛岃鎹负鍙繍绠楃殑鏁扮粍
GrayImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #灏咮GR鍥捐浆涓虹伆搴﹀浘
ret,thresh1=cv2.threshold(GrayImage,130,255,cv2.THRESH_BINARY)#灏嗗浘鐗囪繘琛屼簩鍊煎寲锛?30,255锛変箣闂寸殑鐐瑰潎鍙樹负255锛堣儗鏅級
(h,w)=thresh1.shape #杩斿洖楂樺拰瀹?a = [0 for z in range(0, w)] 
#璁板綍姣忎竴鍒楃殑娉㈠嘲
for j in range(0,w): #閬嶅巻涓€鍒?
	for i in range(0,h): #閬嶅巻涓€琛?		if thresh1[i,j]==0: #濡傛灉鏀圭偣涓洪粦鐐?			a[j]+=1 #璇ュ垪鐨勮鏁板櫒鍔犱竴璁℃暟
			thresh1[i,j]=255 #璁板綍瀹屽悗灏嗗叾鍙樹负鐧借壊 

for j in range(0,w): #閬嶅巻姣忎竴鍒?	for i in range((h-a[j]),h): #浠庤鍒楀簲璇ュ彉榛戠殑鏈€椤堕儴鐨勭偣寮€濮嬪悜鏈€搴曢儴娑傞粦
		thresh1[i,j]=0 #娑傞粦
plt.imshow(thresh1,cmap=plt.gray())
plt.show()
cv2.imshow(鈥榦ne鈥?thresh1) 
cv2.waitKey(0)

鎶€鏈浘鐗? src=

3.姘村钩鏂瑰悜鎶曞奖

for j in range(0,h): 
	for i in range(0,w): 
		if thresh1[j,i]==0: 
			a[j]+=1 
			thresh1[j,i]=255

for j in range(0,h): 
	for i in range(0,a[j]): 
		thresh1[j,i]=0 

plt.imshow(thresh1,cmap=plt.gray())
plt.show()

鎶€鏈浘鐗? src=

浜旓細杞︾墝瀛楃鍒嗗壊

??杞︾墝璇嗗埆绯荤粺(Vehicle License Plate Recognition锛孷LPR) 鏄绠楁満瑙嗛鍥惧儚璇嗗埆鎶€鏈湪杞﹁締鐗岀収璇嗗埆涓殑涓€绉嶅簲鐢ㄣ€?

??杞︾墝璇嗗埆鎶€鏈姹傝兘澶熷皢杩愬姩涓殑姹借溅鐗岀収浠庡鏉傝儗鏅腑鎻愬彇骞惰瘑鍒嚭鏉ワ紝閫氳繃杞︾墝鎻愬彇銆佸浘鍍忛澶勭悊銆佺壒寰佹彁鍙栥€佽溅鐗屽瓧绗﹁瘑鍒瓑鎶€鏈紝璇嗗埆杞﹁締鐗屽彿銆侀鑹茬瓑淇℃伅锛岀洰鍓嶆渶鏂扮殑鎶€鏈按骞充负瀛楁瘝鍜屾暟瀛楃殑璇嗗埆鐜囧彲杈惧埌99.7%锛屾眽瀛楃殑璇嗗埆鐜囧彲杈惧埌99%銆?

??瑕佸皢杞︾墝杩涜瀛楃鍒嗗壊锛屽厛杩涜瀛楃璇嗗埆鎿嶄綔銆傝繖閲岃繘琛岀殑鏄洿鎺ョ敤鏂规璇嗗埆骞舵嫭璧锋潵銆傞渶瑕佺殑姝ラ鏄鍏堣璁╁僵鑹茬殑杞︾墝鍥惧儚杞寲涓虹伆搴﹀浘锛屽啀鍙嶈壊锛岄槇鍊煎垎鍓诧紝鍐嶅埄鐢ㄦ按骞冲拰鍨傜洿鏂瑰悜鐨勬姇褰变緷娆℃埅鍙栭渶瑕佺殑灏忔柟妗嗙殑闀垮拰瀹斤紝杩涜璇嗗埆銆傜敤鍒扮殑鎶€鏈槸python+opencv鍏朵腑鐨刢v2搴撲互鍙婃暟鎹垎鏋恘umpy搴撱€?br> ??姝ラ濡備笅锛?/p>

1.璇诲彇鍘熷浘

import cv2
import cv2 as cv
import numpy as np

image1 = cv.imread(鈥?23456.jpg鈥?1)
cv.imshow(鈥榠mage1鈥? image1)

鎶€鏈浘鐗? src=

2.鐏板害杞崲

??鐒跺悗鎶婂浘鐗囪浆鍖栦负鐏板害鍥撅紝杩欓噷璁剧疆璇诲彇鍥剧墖鐢ㄧ伆搴﹀浘鐗囪鍙栧嵆鍙€?/p>

image2 = cv.imread(鈥?23456.jpg鈥?0)
cv.imshow(鈥榠mage2鈥? image2)

鎶€鏈浘鐗? src=

3.鍙嶈壊

??鎺ョ潃灏嗗浘鍍忚繘琛屽弽鑹诧紝璁板綍瀹介珮鍜屾繁搴︼紝骞剁敤255鍑忓幓鐏板害鍥鹃鑹茬殑瀹介珮銆?/p>

height, width, deep = image1.shape
dst = np.zeros((height,width,1), np.uint8)
for i in range(0, height):
    for j in range(0, width):
        grayPixel = image2[i, j]
        dst[i, j] = 255-grayPixel
cv2.imshow(鈥榠mage3鈥? dst)

鎶€鏈浘鐗? src=

ret, thresh = cv2.threshold(dst, 100, 255, cv2.THRESH_TOZERO) cv2.imshow(鈥榠mage4鈥? thresh)

鎶€鏈浘鐗? src=

5.鎶曞奖

??涔嬪悗鍦ㄦ按骞冲拰鍨傜洿鏂瑰悜涓婅繘琛屾姇褰憋紝鐢诲嚭鎶曞奖鍥撅紝骞惰褰曟姇褰卞皬榛戠偣鐨勫崟浣嶈捣濮嬮暱搴︽暟缁勶紝璁惧畾涓や釜鍑芥暟锛岃繑鍥炰箣鍚庤鐢ㄥ埌鐨勬瘡涓皬鍒嗗壊鐭╁舰鐨勭壒寰佺偣鍧愭爣鍊笺€?/p>

# 姘村钩鏂瑰悜鎶曞奖
def hProject(binary):
    h, w = binary.shape
    # 姘村钩鎶曞奖
    hprojection = np.zeros(binary.shape, dtype=np.uint8)
    # 鍒涘缓h闀垮害閮戒负0鐨勬暟缁?    h_h = [0]*h
    for j in range(h):
        for i in range(w):
            if binary[j,i] == 0:
                h_h[j] += 1
    # 鐢诲嚭鎶曞奖鍥?    for j in range(h):
        for i in range(h_h[j]):
            hprojection[j,i] = 255
    return h_h

# 鍨傜洿鍙嶅悜鎶曞奖
def vProject(binary):
    h, w = binary.shape
    # 鍨傜洿鎶曞奖
    vprojection = np.zeros(binary.shape, dtype=np.uint8)
    # 鍒涘缓 w 闀垮害閮戒负0鐨勬暟缁?    w_w = [0]*w
    for i in range(w):
        for j in range(h):
            if binary[j, i ] == 0:
                w_w[i] += 1
    for i in range(w):
        for j in range(w_w[i]):
            vprojection[j,i] = 255
    return w_w

鎶€鏈浘鐗? src=

鎶€鏈浘鐗? src=

6.瀛楃璇嗗埆鍖归厤鍒嗗壊

??鏍规嵁杩斿洖鐨勬暟缁勶紝纭畾鍒嗗壊浣嶇疆锛岃繘琛屽瓧绗﹁瘑鍒尮閰嶅垎鍓层€?/p>

th = thresh
h,w = th.shape
h_h = hProject(th)
start = 0
h_start, h_end = [], []
position = []
# 鏍规嵁姘村钩鎶曞奖鑾峰彇鍨傜洿鍒嗗壊
for i in range(len(h_h)):
    if h_h[i] > 0 and start == 0:
        h_start.append(i)
        start = 1
    if h_h[i] ==0 and start == 1:
        h_end.append(i)
        start = 0
for i in range(len(h_start)):
    cropImg = th[h_start[i]:h_end[i], 0:w]
    if i ==0:
        pass
    w_w = vProject(cropImg)
    wstart , wend, w_start, w_end = 0, 0, 0, 0
    for j in range(len(w_w)):
        if w_w[j] > 0 and wstart == 0:
            w_start = j
            wstart = 1
            wend = 0
        if w_w[j] ==0 and wstart == 1:
            w_end = j
            wstart = 0
            wend = 1
        # 褰撶‘璁や簡璧风偣鍜岀粓鐐逛箣鍚庝繚瀛樺潗鏍?  
        if wend == 1:
            position.append([w_start, h_start[i], w_end, h_end[i]])
            wend = 0
# 纭畾鍒嗗壊浣嶇疆
for p in position:
    cv2.rectangle(thresh, (p[0], p[1]), (p[2], p[3]), (0, 0, 255), 2)
cv2.imshow(鈥榠mage7鈥? thresh)
cv.waitKey(0)

鎶€鏈浘鐗? src=

鎶€鏈浘鐗? src=

以上是关于python+opencv瀹炵幇鏈哄櫒瑙嗚鍩虹鎶€鏈?杈圭紭鎻愬彇锛屽浘鍍忔护娉紝杈圭紭妫€娴嬬畻瀛愶紝鎶曞奖锛岃溅鐗屽瓧绗﹀垎鍓?的主要内容,如果未能解决你的问题,请参考以下文章

Android | 瓒呯畝鍗曢泦鎴怘MS ML Kit瀹炵幇鏈€澶ц劯寰瑧鎶撴媿

闃舵1 璇█鍩虹+楂樼骇_1-3-Java璇█楂樼骇_02-缁ф壙涓庡鎬乢绗?鑺?鎶借薄绫籣19-鍙戠孩鍖呮渚媉瀹炵幇

F5鍋ュ悍妫€鏌ュ鑷寸殑鏈嶅姟绔繛鎺ュ紓甯窻ST

鏈哄櫒瀛︿範瀹炴垬-Logistics鍥炲綊

寰俊app鏀粯python浠g爜瀹炵幇

鏈哄櫒鍒嗛厤