opencv信用卡检测遇到了(-210:Unsupported format or combination of formats) [Start]FindContours supports only

在网上看到了很多解决方法,但都i行不通,试过在cv2.imread()读取图片末端加0,也没用

 想尝试如何将三通道的图片改成单通道的,但都无济于事,可能是我的方法不对,下面附上我的代码

import cv2
import numpy as np


def show(img):
    cv2.imshow('xx', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

def sort_conts(conts):
    boxes = [cv2.boundingRect(c) for c in conts]#绘制矩形边界框
    a= sorted(zip(conts, boxes), key=lambda x: x[1])
    return conts, boxes
#读取模板,并灰度值
img = cv2.imread('E:\opencv2\picture/OIP.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

#二值化阈值
_,ref = cv2.threshold(gray,10,255,cv2.THRESH_BINARY_INV)
show(ref)

#轮廓检测,画出轮廓
refCnts, _ =cv2.findContours(ref.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, refCnts, -1, (0,0,255), 2)
show(img)

#轮廓排序
refCnts, _ = sort_conts(refCnts)

#模板切割
digits= {}#建立一个字典类型,i是轮廓索引,c是轮廓----字典类型:每个索引号对应一个索引值
for i, c in enumerate(refCnts):#i是轮廓索引,c是对应轮廓,则完成了对检测出来的轮廓进行了排序
    x, y, w, h = cv2.boundingRect(c)#得到没一个外接矩形的左上坐标点以及长度、宽度
    roi=ref[y:y+h,x:x+w]#每个数字的外接矩形的尺寸
    #print(w, h)
    roi = cv2.resize(roi, (17, 28))  # 重置外接矩形的尺寸至合适大小
    digits[i] = roi  # 每个数字对应一个模板
    show(roi)

image = cv2.imread('E:\opencv2\picture/AC.png')
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
show(gray)

#初始化卷积核
rectkernel = cv2.getStructuringElement(cv2.MORPH_RECT,(11,9))#卷积核形象着闭合操作的是否完整
sqkernel = cv2.getStructuringElement(cv2.MORPH_RECT,(2,2))

#礼帽操作,过滤,突出明亮部分,以便后面的读取轮廓
result = cv2.morphologyEx(image, cv2.MORPH_TOPHAT, rectkernel)
show(result)
#Sobel算子对x方向进行检测
gradX = cv2.Sobel(result, ddepth=cv2.CV_32F,dx=1, dy=0,
                  ksize=-1)   # -1 相当于3*3
#加绝对值,黑白边界均能看到
gradX = np.absolute(gradX)
(minVAl, maxVal) = (np.min(gradX), np.max(gradX))
gradX = (255*((gradX - minVAl) / (maxVal -minVAl)))
gradX= gradX.astype(np.float32) / 255
gradX= (gradX * 255).astype(np.uint8)


show(gradX)

#闭操作
gradX = cv2.morphologyEx(gradX,cv2.MORPH_CLOSE,rectkernel)

#再次闭操作,使空白补满
thresh=cv2.morphologyEx(gradX.copy(),cv2.MORPH_CLOSE,sqkernel)
show(thresh)

#轮廓检测
cnts, _ =cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

cut_image = image.copy()
Cnts = cnts.copy()
cut_image = cv2.drawContours(cut_image, Cnts, -1, (0,0,255), 2)
show(cut_image)

写到轮廓检测这里就出现问题了,显示error: (-210:Unsupported format or combination of formats) [Start]FindContours supports only CV_8UC1 images when mode != CV_RETR_FLOODFILL otherwise supports CV_32SC1 images only in function    所以没有接着往下写,附上出现的问题的图片

opencv信用卡检测遇到了(-210:Unsupported format or combination of formats) [Start]FindContours supports only_第1张图片

附上运行效果图

opencv信用卡检测遇到了(-210:Unsupported format or combination of formats) [Start]FindContours supports only_第2张图片opencv信用卡检测遇到了(-210:Unsupported format or combination of formats) [Start]FindContours supports only_第3张图片

 opencv信用卡检测遇到了(-210:Unsupported format or combination of formats) [Start]FindContours supports only_第4张图片opencv信用卡检测遇到了(-210:Unsupported format or combination of formats) [Start]FindContours supports only_第5张图片opencv信用卡检测遇到了(-210:Unsupported format or combination of formats) [Start]FindContours supports only_第6张图片opencv信用卡检测遇到了(-210:Unsupported format or combination of formats) [Start]FindContours supports only_第7张图片opencv信用卡检测遇到了(-210:Unsupported format or combination of formats) [Start]FindContours supports only_第8张图片

 下面附上我在imread后面加上0的代码,加0后我就把gray灰度的代码去掉了

import cv2
import numpy as np


def show(img):
    cv2.imshow('xx', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

def sort_conts(conts):
    boxes = [cv2.boundingRect(c) for c in conts]#绘制矩形边界框
    a= sorted(zip(conts, boxes), key=lambda x: x[1])
    return conts, boxes
#读取模板,并灰度值
img = cv2.imread('E:\opencv2\picture/OIP.jpg',0)

#二值化阈值
_,ref = cv2.threshold(img,10,255,cv2.THRESH_BINARY_INV)
show(ref)

#轮廓检测,画出轮廓
refCnts, _ =cv2.findContours(ref.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img, refCnts, -1, (0,0,255), 2)
show(img)

#轮廓排序
refCnts, _ = sort_conts(refCnts)

#模板切割
digits= {}#建立一个字典类型,i是轮廓索引,c是轮廓----字典类型:每个索引号对应一个索引值
for i, c in enumerate(refCnts):#i是轮廓索引,c是对应轮廓,则完成了对检测出来的轮廓进行了排序
    x, y, w, h = cv2.boundingRect(c)#得到没一个外接矩形的左上坐标点以及长度、宽度
    roi=ref[y:y+h,x:x+w]#每个数字的外接矩形的尺寸
    #print(w, h)
    roi = cv2.resize(roi, (17, 28))  # 重置外接矩形的尺寸至合适大小
    digits[i] = roi  # 每个数字对应一个模板
    show(roi)

image = cv2.imread('E:\opencv2\picture/AC.png',0)

show(image)

#初始化卷积核
rectkernel = cv2.getStructuringElement(cv2.MORPH_RECT,(11,9))#卷积核形象着闭合操作的是否完整
sqkernel = cv2.getStructuringElement(cv2.MORPH_RECT,(2,2))

#礼帽操作,过滤,突出明亮部分,以便后面的读取轮廓
result = cv2.morphologyEx(image.copy(), cv2.MORPH_TOPHAT, rectkernel)
show(result)
#Sobel算子对x方向进行检测
gradX = cv2.Sobel(result, ddepth=cv2.CV_32F,dx=1, dy=0,
                  ksize=-1)   # -1 相当于3*3
#加绝对值,黑白边界均能看到
gradX = np.absolute(gradX)
(minVAl, maxVal) = (np.min(gradX), np.max(gradX))
gradX = (255*((gradX - minVAl) / (maxVal -minVAl)))
gradX= gradX.astype(np.float32) / 255
gradX= (gradX * 255).astype(np.uint8)
show(gradX)

#闭操作
gradX = cv2.morphologyEx(gradX,cv2.MORPH_CLOSE,rectkernel)

#再次闭操作,使空白补满
thresh=cv2.morphologyEx(gradX.copy(),cv2.MORPH_CLOSE,sqkernel)
show(thresh)

#轮廓检测
cnts, _ =cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

cut_image = image.copy()
Cnts = cnts.copy()
cut_image = cv2.drawContours(cut_image, Cnts, -1, (0,0,255), 2)
show(cut_image)

 附上运行效果,我有点懵逼,画出来的轮廓可怪了

opencv信用卡检测遇到了(-210:Unsupported format or combination of formats) [Start]FindContours supports only_第9张图片opencv信用卡检测遇到了(-210:Unsupported format or combination of formats) [Start]FindContours supports only_第10张图片opencv信用卡检测遇到了(-210:Unsupported format or combination of formats) [Start]FindContours supports only_第11张图片 opencv信用卡检测遇到了(-210:Unsupported format or combination of formats) [Start]FindContours supports only_第12张图片opencv信用卡检测遇到了(-210:Unsupported format or combination of formats) [Start]FindContours supports only_第13张图片 opencv信用卡检测遇到了(-210:Unsupported format or combination of formats) [Start]FindContours supports only_第14张图片opencv信用卡检测遇到了(-210:Unsupported format or combination of formats) [Start]FindContours supports only_第15张图片

opencv信用卡检测遇到了(-210:Unsupported format or combination of formats) [Start]FindContours supports only_第16张图片

求大佬帮忙解决,小白一个求求了

你可能感兴趣的:(python,opencv)