本文介绍了如何在我的python程序中计算canny图像的非零像素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找佳能图像的非零像素数量,您能帮忙吗?

I'm trying to find the number of non zero pixels of the canny image, could you help?

这是我的代码:

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的函数 countNonZero 用于计算图像中非零像素的数量.

You can use opencv's function countNonZero for counting the number of non-zero pixels in the image.

img3 = doCanny(img2, 10, 100, 3)
nzCount = cv.CountNonZero(img3);

更新:

在较新版本的OpenCV中,非零计数功能已更新如下:

Update:

In newer versions of OpenCV, the function to count non zeros has been updated as follows:

nzCount = cv2.countNonZero(img3)

这篇关于如何在我的python程序中计算canny图像的非零像素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 17:57