如何在python程序中计算canny图像的非零像素数

如何在python程序中计算canny图像的非零像素数,python,opencv,Python,Opencv,我想找出canny图像中非零像素的数量,你能帮我吗 这是我的密码: import cv def doCanny(input, lowThresh, highThresh, aperture): if input.nChannels != 1: return(0) out = cv.CreateImage((input.width, input.height), input.depth, 1) cv.Canny(input, out, lowThre

我想找出canny图像中非零像素的数量,你能帮我吗

这是我的密码:

import cv
def doCanny(input, lowThresh, highThresh, aperture):
    if input.nChannels != 1:
            return(0)
    out = cv.CreateImage((input.width, input.height), input.depth, 1)
    cv.Canny(input, out, lowThresh, highThresh, aperture)
    return out
def doPyrDown(input):
    assert(input.width !=0 and input.height !=0)
    out = cv.CreateImage((input.width/2, input.height/2), input.depth, input.nChannels)
    cv.PyrDown(input, out)
    return out

img = cv.LoadImage('mypic.jpg')
img2 = cv.CreateImage((img.width, img.height), img.depth, 1)
cv.CvtColor(img, img2, cv.CV_BGR2GRAY)
cv.NamedWindow("Example GRAY", cv.CV_WINDOW_AUTOSIZE)
cv.ShowImage("Example GRAY", img2)
img3 = doCanny(img2, 10, 100, 3)
img2 = doPyrDown(img3)
cv.ShowImage("Example 2", img2)
cv.WaitKey(0)

您可以使用opencv的函数来计算图像中非零像素的数量

img3 = doCanny(img2, 10, 100, 3)
nzCount = cv.CountNonZero(img3);
更新: 在较新版本的OpenCV中,计数非零的函数已更新如下:

nzCount = cv2.countNonZero(img3)

试试
cv.countNonZero()
countNonZero()
对我不起作用。

如果我给“print nzCount”,值会被打印出来吗?@VickyKaran。。。如果这个答案解决了你的问题,请考虑接受: