去除不需要的框架及其宽高比
我现在正在学习Python的OpenCV,特别是从http://opencvpython.blogspot.com这个网站上。我遇到了一个问题,就是在使用找轮廓和边界矩形的功能后,想要找到车牌。举个例子,我得到了3到5个框,就像在前窗户上,背景有树,还有很多其他东西。有没有人能帮我解决这个问题?我真的很烦恼找不到答案。而且我已经尝试了很多预处理的方法,比如:
- 灰度化
- 阈值处理
- 形态学处理
我的代码:
import cv2
import numpy as np
#import image
im = cv2.imread('sample_1.jpg')
#convert to grayscale
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
#apply bilateral filter
gray = cv2.bilateralFilter(gray,11,17,17)
#preparing a kernel matrix 5x5
kernel = np.ones((5,5),np.uint8)
#tophat ops
cv2.morphologyEx(gray,cv2.MORPH_TOPHAT, kernel)
edged = cv2.Canny(tophat,30,200)
dilated = cv2.dilate(edged,kernel,iterations = 3)
#finding contour
(cnts,_) = cv2.findContours(edged,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)
screenCnt = None
for c in cnts:
peri = cv2.arcLength(c,True)
approx = cv2.approxPolyDP(c,0.02*peri,True)
x,y,w,h = cv2.boundingRect(c)
print cv2.contourArea(c)
x,y,w,h = cv2.boundingrect(c)
cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),3)
roi = im[y:y+h, x:x+w]
cv2.imwrite(roi.png',roi)
if len (approx)==4
screenCnt = approx
break
cv2.imshow("test",im)
cv2.waitKey(0)
这段代码主要是通过浏览和阅读博客得到的。关键是用这段代码我得到了多个车牌框。我想要的只是得到车牌,裁剪掉其他部分,然后进行字符分割。
1 个回答
0
在编程中,有时候我们会遇到一些问题,尤其是在使用某些工具或库的时候。比如,有人可能在使用某个库时,发现它的某个功能没有按照预期工作。这种情况可能会让人感到困惑,因为我们不知道问题出在哪里。
通常,解决这类问题的第一步是仔细检查代码,看看有没有写错的地方。接着,可以查阅相关的文档,看看这个功能的使用方法是否正确。如果文档中没有找到答案,可能需要在网上搜索一下,看看其他人是否遇到过类似的问题。
如果还是找不到解决办法,可以考虑在一些技术论坛上提问,比如StackOverflow。在提问时,最好把自己的代码和遇到的问题描述清楚,这样其他人才能更好地帮助你。
总之,遇到技术问题时,不要着急,慢慢分析,查阅资料,寻求帮助,通常都能找到解决方案。
import cv2
import numpy as np
#import image
im = cv2.imread('sample_1.jpg')
#convert to grayscale
gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
#apply bilateral filter
gray = cv2.bilateralFilter(gray,11,17,17)
#preparing a kernel matrix 5x5
kernel = np.ones((5,5),np.uint8)
#tophat ops
cv2.morphologyEx(gray,cv2.MORPH_TOPHAT, kernel)
edged = cv2.Canny(tophat,30,200)
dilated = cv2.dilate(edged,kernel,iterations = 3)
#finding contour
(cnts,_) = cv2.findContours(edged,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)
screenCnt = None
for c in cnts:
peri = cv2.arcLength(c,True)
approx = cv2.approxPolyDP(c,0.02*peri,True)
x,y,w,h = cv2.boundingRect(c)
print cv2.contourArea(c)
x,y,w,h = cv2.boundingrect(c)
cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),3)
roi = im[y:y+h, x:x+w]
cv2.imwrite(roi.png',roi)
if len (approx)==4
screenCnt = approx
break
cv2.imshow("test",im)
cv2.waitKey(0)