OpenCV - locations of all non-zero pixels in binary image

前端 未结 3 976
梦如初夏
梦如初夏 2021-01-01 22:52

How can I find the locations of all non-zero pixels in a binary image (cv::Mat)? Do I have to scan through every pixel in the image or is there a high level OpenCV function(

3条回答
  •  误落风尘
    2021-01-01 23:21

    Anyone looking to do this in python. it is also possible to do it with numpy arrays and therefore you don't need to upgrade your opencv version (or use undocumented functions).

    mask = np.zeros(imgray.shape,np.uint8)
    cv2.drawContours(mask,[cnt],0,255,-1)
    pixelpoints = np.transpose(np.nonzero(mask))
    #pixelpoints = cv2.findNonZero(mask)
    

    Commented out is the same function using openCV instead. For more info see:

    https://github.com/abidrahmank/OpenCV2-Python-Tutorials/blob/master/source/py_tutorials/py_imgproc/py_contours/py_contour_properties/py_contour_properties.rst

提交回复
热议问题