| OpenCV 5.0.0alpha Open Source Computer Vision

Loading...
Searching...
No Matches
samples/python/snippets/laplace.py

An example using Laplace filter for edge detection in python

1#!/usr/bin/env python
2
3'''
4 This program demonstrates Laplace point/edge detection using
5 OpenCV function Laplacian()
6 It captures from the camera of your choice: 0, 1, ... default 0
7 Usage:
8 python laplace.py <ddepth> <smoothType> <sigma>
9 If no arguments given default arguments will be used.
10
11 Keyboard Shortcuts:
12 Press space bar to exit the program.
13 '''
14
15# Python 2/3 compatibility
16 from __future__ import print_function
17
18 import numpy as np
19 import cv2 as cv
20 import sys
21
22 def main():
23 # Declare the variables we are going to use
24 ddepth = cv.CV_16S
25 smoothType = "MedianBlur"
26 sigma = 3
27 if len(sys.argv)==4:
28 ddepth = sys.argv[1]
29 smoothType = sys.argv[2]
30 sigma = sys.argv[3]
31 # Taking input from the camera
32 cap=cv.VideoCapture(0)
33 # Create Window and Trackbar
34 cv.namedWindow("Laplace of Image", cv.WINDOW_AUTOSIZE)
35 cv.createTrackbar("Kernel Size Bar", "Laplace of Image", sigma, 15, lambda x:x)
36 # Printing frame width, height and FPS
37 print("=="*40)
38 print("Frame Width: ", cap.get(cv.CAP_PROP_FRAME_WIDTH), "Frame Height: ", cap.get(cv.CAP_PROP_FRAME_HEIGHT), "FPS: ", cap.get(cv.CAP_PROP_FPS))
39 while True:
40 # Reading input from the camera
41 ret, frame = cap.read()
42 if ret == False:
43 print("Can't open camera/video stream")
44 break
45 # Taking input/position from the trackbar
46 sigma = cv.getTrackbarPos("Kernel Size Bar", "Laplace of Image")
47 # Setting kernel size
48 ksize = (sigma*5)|1
49 # Removing noise by blurring with a filter
50 if smoothType == "GAUSSIAN":
51 smoothed = cv.GaussianBlur(frame, (ksize, ksize), sigma, sigma)
52 if smoothType == "BLUR":
53 smoothed = cv.blur(frame, (ksize, ksize))
54 if smoothType == "MedianBlur":
55 smoothed = cv.medianBlur(frame, ksize)
56
57 # Apply Laplace function
58 laplace = cv.Laplacian(smoothed, ddepth, 5)
59 # Converting back to uint8
60 result = cv.convertScaleAbs(laplace, (sigma+1)*0.25)
61 # Display Output
62 cv.imshow("Laplace of Image", result)
63 k = cv.waitKey(30)
64 if k == 27:
65 return
66 if __name__ == "__main__":
67 print(__doc__)
68 main()
69 cv.destroyAllWindows()
cv::VideoCapture
Class for video capturing from video files, image sequences or cameras.
Definition videoio.hpp:727
cv::convertScaleAbs
void convertScaleAbs(InputArray src, OutputArray dst, double alpha=1, double beta=0)
Scales, calculates absolute values, and converts the result to 8-bit.
cv::getTrackbarPos
int getTrackbarPos(const String &trackbarname, const String &winname)
Returns the trackbar position.
cv::imshow
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
cv::waitKey
int waitKey(int delay=0)
Waits for a pressed key.
cv::namedWindow
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
cv::destroyAllWindows
void destroyAllWindows()
Destroys all of the HighGUI windows.
cv::createTrackbar
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.
cv::medianBlur
void medianBlur(InputArray src, OutputArray dst, int ksize)
Blurs an image using the median filter.
cv::blur
void blur(InputArray src, OutputArray dst, Size ksize, Point anchor=Point(-1,-1), int borderType=BORDER_DEFAULT)
Blurs an image using the normalized box filter.
cv::Laplacian
void Laplacian(InputArray src, OutputArray dst, int ddepth, int ksize=1, double scale=1, double delta=0, int borderType=BORDER_DEFAULT)
Calculates the Laplacian of an image.
cv::GaussianBlur
void GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX, double sigmaY=0, int borderType=BORDER_DEFAULT, AlgorithmHint hint=cv::ALGO_HINT_DEFAULT)
Blurs an image using a Gaussian filter.
main
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3