解决error: (-210:Unsupported format or combination of formats) [Start]FindContours supports only CV_8U

原程序:

# -*- coding:utf-8 -*-
# 创建时间:2019年7月29日
# 使用轮廓匹配识别出目标物

import cv2
import numpy as np


# 不同特征的形状匹配
# 输入参数:model_img:目标图像矩阵;train_frame:待检测的图像矩阵
# 输出参数:matching_value:匹配值,越小表示匹配度越高
def contours_matching(model_img,train_frame):


    ret, thresh = cv2.threshold(model_img, 127, 255,0)
    ret, thresh2 = cv2.threshold(train_frame, 127, 255,0)

    img1, contours, hierarchy = cv2.findContours(thresh,2,1)
    cnt1 = contours[0]
    img2, contours, hierarchy = cv2.findContours(thresh2,2,1)
    cnt2 = contours[0]

    matching_value = cv2.matchShapes(cnt1,cnt2,1,0.0)   # 计算匹配度
    print('匹配度:',matching_value)

    return matching_value


model_img = cv2.imread('../images/contours/circle.png')
train_frame = cv2.imread('../images/contours/circle.png')

matching_value = contours_matching(model_img,train_frame)

报错:

error: (-210:Unsupported format or combination of formats) [Start]FindContours supports only CV_8U

cv2.imread处有问题,改动后:

# -*- coding:utf-8 -*-
# 创建时间:2019年7月29日
# 使用轮廓匹配识别出目标物

import cv2
import numpy as np


# 不同特征的形状匹配
# 输入参数:model_img:目标图像矩阵;train_frame:待检测的图像矩阵
# 输出参数:matching_value:匹配值,越小表示匹配度越高
def contours_matching(model_img,train_frame):


    ret, thresh = cv2.threshold(model_img, 127, 255,0)
    ret, thresh2 = cv2.threshold(train_frame, 127, 255,0)

    img1, contours, hierarchy = cv2.findContours(thresh,2,1)
    cnt1 = contours[0]
    img2, contours, hierarchy = cv2.findContours(thresh2,2,1)
    cnt2 = contours[0]

    matching_value = cv2.matchShapes(cnt1,cnt2,1,0.0)   # 计算匹配度
    print('匹配度:',matching_value)

    return matching_value


model_img = cv2.imread('../images/contours/circle.png',0)
train_frame = cv2.imread('../images/contours/circle.png',0)

matching_value = contours_matching(model_img,train_frame)

完美解决!


原因剖析: 查看官网API手册:

解决error: (-210:Unsupported format or combination of formats) [Start]FindContours supports only CV_8U_第1张图片

第二个参数等于是读入灰度图,而后面的轮廓处理函数只能处理灰度图,所以这样写也是可以的,即在函数中转换成灰度图

# -*- coding:utf-8 -*-
# 创建时间:2019年7月29日
# 使用轮廓匹配识别出目标物

import cv2
import numpy as np


# 不同特征的形状匹配
# 输入参数:model_img:目标图像矩阵;train_frame:待检测的图像矩阵
# 输出参数:matching_value:匹配值,越小表示匹配度越高
def contours_matching(model_img,train_frame):

    model_img = cv2.cvtColor(model_img,cv2.COLOR_BGR2GRAY)
    train_frame = cv2.cvtColor(train_frame,cv2.COLOR_BGR2GRAY)

    ret, thresh = cv2.threshold(model_img, 127, 255,0)
    ret, thresh2 = cv2.threshold(train_frame, 127, 255,0)

    img1, contours, hierarchy = cv2.findContours(thresh,2,1)
    cnt1 = contours[0]
    img2, contours, hierarchy = cv2.findContours(thresh2,2,1)
    cnt2 = contours[0]

    matching_value = cv2.matchShapes(cnt1,cnt2,1,0.0)   # 计算匹配度
    print('匹配度:',matching_value)

    return matching_value


model_img = cv2.imread('../images/contours/circle.png')
train_frame = cv2.imread('../images/contours/circle.png')

matching_value = contours_matching(model_img,train_frame)

 

你可能感兴趣的:(计算机视觉,乱七八糟的bug)