python - openCV python 根据边问题查找轮廓

标签 python opencv detection

我想检测车号!

看这张照片


使用这些代码:

import numpy as np
import imutils
import cv2

# Read the image file
image = cv2.imread('Car_Image_1.jpg')

# Resize the image - change width to 500
image = imutils.resize(image, width=500)

# Display the original image
cv2.imshow("Original Image", image)

# RGB to Gray scale conversion
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("1 - Grayscale Conversion", gray)

# Noise removal with iterative bilateral filter(removes noise while preserving edges)
gray = cv2.bilateralFilter(gray, 11, 17, 17)
cv2.imshow("2 - Bilateral Filter", gray)

# Find Edges of the grayscale image
edged = cv2.Canny(gray, 170, 200)
cv2.imshow("4 - Canny Edges", edged)

# Find contours based on Edges
(new, cnts , _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# sort contours based on their area keeping minimum required area as '30' (anything smaller than this will not be considered)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:30]
# we currently have no Number plate contour
NumberPlateCnt = None

# loop over our contours to find the best possible approximate contour of number plate
count = 0
for c in cnts:
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)
        if len(approx) == 4:  # Select the contour with 4 corners
            NumberPlateCnt = approx #This is our approx Number Plate Contour
            break

# Drawing the selected contour on the original image
cv2.drawContours(image, [NumberPlateCnt], -1, (0,255,0), 3)
cv2.imshow("Final Image With Number Plate Detected", image)

cv2.waitKey(0) #Wait for user input before closing the images displayed

但是当我运行我的代码时,出现了这个错误:

C:\Python27\python.exe C:/Users/Crypt/PycharmProjects/MyDetector/CarPlateDetection.py
Traceback (most recent call last):
  File "C:/Users/Crypt/PycharmProjects/MyDetector/CarPlateDetection.py", line 27, in <module>
    (new, cnts , _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
ValueError: need more than 2 values to unpack

Process finished with exit code 1

我修改这行代码

(new, cnts , _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

(new, cnts) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

然后运行,错误是:

C:\Python27\python.exe C:/Users/Crypt/PycharmProjects/MyDetector/CarPlateDetection.py
Traceback (most recent call last):
  File "C:/Users/Crypt/PycharmProjects/MyDetector/CarPlateDetection.py", line 29, in <module>
    cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:30]
cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\imgproc\src\shapedescr.cpp:272: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'cv::contourArea'

进程结束,退出代码为 1

这是github上的代码:

https://github.com/Aqsa-K/Car-Number-Plate-Detection-OpenCV-Python

最佳答案

从 OpenCV4 开始,findContours 返回 2 个值 contourshierarchy,所以现在,contours 在new在你的代码中。应该是

cnts, hier = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

关于python - openCV python 根据边问题查找轮廓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54937062/

相关文章:

python - 如何拆分和连接 Pandas 数据框

python - Opencv人脸检测网络摄像头不保存检测到的人脸

python-2.7 - 使用python将一系列ppm图像转换为视频

从点的数组列表中进行java圆识别

neural-network - 试图在图像中找到对象坐标 (x,y),我的神经网络似乎在没有学习的情况下优化了错误

python - 如何在 Django 后台定期重复一个函数直到成功?

python - 如何使用正则表达式提取多个字符串?

javascript - 使用 JavaScript 检测用户存在

python - 元素不可见异常 : Selenium Python

opencv - 使用 OpenCv 多跟踪器实时跟踪对象