'''
Distance transform sample.
Usage:
distrans.py [<image>]
Keys:
ESC - exit
v - toggle voronoi mode
'''
from __future__ import print_function
import numpy as np
import cv2 as cv
from common import make_cmap
import sys
try:
fn = sys.argv[1]
except:
fn = 'fruits.jpg'
if img is None:
print('Failed to load fn:', fn)
sys.exit(1)
cm = make_cmap('jet')
need_update = True
voronoi = False
def update(dummy=None):
global need_update
need_update = False
dist, labels = cv.distanceTransformWithLabels(~mark, cv.DIST_L2, 5)
if voronoi:
vis = cm[np.uint8(labels)]
else:
vis = cm[np.uint8(dist*2)]
vis[mark != 0] = 255
def invalidate(dummy=None):
global need_update
need_update = True
update()
while True:
if ch == 27:
break
if ch == ord('v'):
voronoi = not voronoi
print('showing', ['distance', 'voronoi'][voronoi])
update()
if need_update:
update()
print('Done')
if __name__ == '__main__':
print(__doc__)
cv::String findFile(const cv::String &relative_path, bool required=true, bool silentMode=false)
Try to find requested data file.
int getTrackbarPos(const String &trackbarname, const String &winname)
Returns the trackbar position.
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
void destroyAllWindows()
Destroys all of the HighGUI windows.
int createTrackbar(const String &trackbarname, const String &winname, int *value, int count, TrackbarCallback onChange=0, void *userdata=0)
Creates a trackbar and attaches it to the specified window.
Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
Loads an image from a file.
void Canny(InputArray image, OutputArray edges, double threshold1, double threshold2, int apertureSize=3, bool L2gradient=false)
Finds edges in an image using the Canny algorithm Canny86 .
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3