python opencv 图片前景与背景的分割,拜大神求如何改错

在网上找到了一个用Kmeans算法对图片前景与背景的分割的例子,很适合现在的学习,可一直有一个错误不会修改,跪求大神了。


```# -*- coding: utf-8 -*-
import cv2
import numpy as np
import math
def panelAbstract(srcImage):
    #   read pic shape
    imgHeight,imgWidth = srcImage.shape[:2]
    imgHeight = int(imgHeight);imgWidth = int(imgWidth)
    # 均值聚类提取前景:二维转一维
    imgVec = np.float32(srcImage.reshape((-1,3)))
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER,10,1.0)
    flags = cv2.KMEANS_RANDOM_CENTERS 
    label,clusCenter = cv2.kmeans(imgVec,2,None,criteria,10,flags)
    clusCenter = np.uint8(clusCenter)
    clusResult = clusCenter[label.flatten()]
    imgres = clusResult.reshape((srcImage.shape))
    imgres = cv2.cvtColor(imgres,cv2.COLOR_BGR2GRAY)
    bwThresh = int((np.max(imgres)+np.min(imgres))/2)
    _,thresh = cv2.threshold(imgres,bwThresh,255,cv2.THRESH_BINARY_INV)
    threshRotate = cv2.merge([thresh,thresh,thresh])
# 确定前景外接矩形
    #find contours
    contours = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    minvalx = np.max([imgHeight,imgWidth]);maxvalx = 0
    minvaly = np.max([imgHeight,imgWidth]);maxvaly = 0
    maxconArea = 0;maxAreaPos = -1
    for i in range(len(contours)):
        if maxconArea < cv2.contourArea(contours[i]):
            maxconArea = cv2.contourArea(contours[i])
            maxAreaPos = i
    objCont = contours[maxAreaPos]
    # 旋转校正前景
    rect = cv2.minAreaRect(objCont)
    for j in range(len(objCont)):
        minvaly = np.min([minvaly,objCont[j][0][0]])
        maxvaly = np.max([maxvaly,objCont[j][0][0]])
        minvalx = np.min([minvalx,objCont[j][0][1]])
        maxvalx = np.max([maxvalx,objCont[j][0][1]])
    if rect[2] <=-45:
        rotAgl = 90 +rect[2]
    else:
        rotAgl = rect[2]
    if rotAgl == 0:
        panelImg = srcImage[minvalx:maxvalx,minvaly:maxvaly,:]
    else:
        rotCtr = rect[0]
        rotCtr = (int(rotCtr[0]),int(rotCtr[1]))
        rotMdl = cv2.getRotationMatrix2D(rotCtr,rotAgl,1)
        imgHeight,imgWidth = srcImage.shape[:2]
        #图像的旋转
        dstHeight = math.sqrt(imgWidth *imgWidth + imgHeight*imgHeight)
        dstRotimg = cv2.warpAffine(threshRotate,rotMdl,(int(dstHeight),int(dstHeight)))
        dstImage = cv2.warpAffine(srcImage,rotMdl,(int(dstHeight),int(dstHeight)))
        dstRotimg = cv2.cvtColor(dstRotimg,cv2.COLOR_BGR2GRAY)
        _,dstRotBW = cv2.threshold(dstRotimg,127,255,0)
        contours = cv2.findContours(dstRotBW,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
        maxcntArea = 0;maxAreaPos = -1
        for i in range(len(contours)):
            if maxcntArea < cv2.contourArea(contours[i]):
                maxcntArea = cv2.contourArea(contours[i])
                maxAreaPos = i
        x,y,w,h = cv2.boundingRect(contours[maxAreaPos])
        #提取前景:panel
        panelImg = dstImage[int(y):int(y+h),int(x):int(x+w),:]

    return panelImg
if __name__=="__main__":
    srcImage = cv2.imread('11.jpg')
    a=panelAbstract(srcImage)
    cv2.imshow('figa',a)
    cv2.waitKey(0)
    cv2.destroyAllWindows()  


这是原地址https://blog.csdn.net/Dawn__Z/article/details/82115160
报错如下(知道错是什么意思就是不会改):Traceback (most recent call last):
  File "D:\Workspaces\MyEclipse 2015\pythonTest\src\cc.py", line 70, in 
    a=panelAbstract(srcImage)
  File "D:\Workspaces\MyEclipse 2015\pythonTest\src\cc.py", line 7, in panelAbstract
    imgHeight,imgWidth = srcImage.shape[:2]
AttributeError: 'NoneType' object has no attribute 'shape'

2个回答

我试了一下,如果你用window系统,在绝对路经中用双“\", 列如: srcImage = cv2.imread('C:\\Users\\fw\\Anaconda3\\Lib\\site-packages\\skimage\\data\\rocket.jpg')。 这因该没问题。

li450813298
燢皌 蟹蟹大佬
12 个月之前 回复

srcImage = cv2.imread('11.jpg') 读取图片文件失败,srcImage是空的,建议你查看一下有当前路径下有没有11.jpg这个图片文件

li450813298
燢皌 依旧是错的,还是老问题
12 个月之前 回复
qq_39412061
吃鸡王者 换张图片或用绝对路径试试
12 个月之前 回复
li450813298
燢皌 11.jpg是在这个文件的根目录下,也确实有这个图片。可一直都是这个错误
12 个月之前 回复
li450813298
燢皌 11.jpg是在这个文件的根目录下,也确实有这个图片。可一直都是这个错误
12 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐