Is it by design that you must pass cv2.connectedComponentsWithStats
a white-on-black image as opposed to a black-on-white image? I get different results doing one versus the other.
Example Code:
import os
import cv2
root = r'pth/to/img'
fl = r'img.png'
src = os.path.join( root, fl )
img = cv2.imread( src, 0 )
img_inv = cv2.bitwise_not( img )
cv2.imshow( 'Black-on-White', img )
cv2.waitKey(0)
cv2.imshow( 'White-on-Black', img_inv )
cv2.waitKey(0)
bw_nlbls, bw_lbls, bw_stats, _ = cv2.connectedComponentsWithStats( img )
wb_nlbls, wb_lbsl, wb_stats, _ = cv2.connectedComponentsWithStats( img_inv )
bw = 'Black-On-White'
wb = 'White-On-Black'
print( bw )
print( '-'*len(bw) )
print()
print('Number of Components: ', bw_nlbls)
print()
print( wb )
print( '-'*len(wb) )
print()
print('Number of Components: ', wb_nlbls)
Output:
Black-on-White
White-on-Black
Black-On-White
--------------
Number of Components: 3
White-On-Black
--------------
Number of Components: 6
I'm assuming by the output that:
For the Black-on-White image, black is taken as the background, white as foreground, and the 3 components are the background, the white around the numbers, and the white inside the number 4.
For the White-on-Black image, black is still background and white foreground, but now there are 6 components (the background and five numbers).
Seems logical, but could we add this to the documentation for future users? Could we add the functionality to allow the user to choose what "color" they want represented as background (white or black)?
white on black
. For your image, 5 number,1background, total 6 components – HeliostatconnectedComponentsWithStats()
function does, ty for posting. – Unveiling