opencv/samples/python/snippets/laplace.py
Gursimar Singh 3dcc8c38b4
Merge pull request #25268 from gursimarsingh:samples_cleanup_python
Removed obsolete python samples #25268

Clean Samples #25006 
This PR removes 36 obsolete python samples from the project, as part of an effort to keep the codebase clean and focused on current best practices. Some of these samples will be updated with latest algorithms or will be combined with other existing samples. 

Removed Samples:

> browse.py
camshift.py
coherence.py
color_histogram.py
contours.py
deconvolution.py
dft.py
dis_opt_flow.py
distrans.py
edge.py
feature_homography.py
find_obj.py
fitline.py
gabor_threads.py
hist.py
houghcircles.py
houghlines.py
inpaint.py
kalman.py
kmeans.py
laplace.py
lk_homography.py
lk_track.py
logpolar.py
mosse.py
mser.py
opt_flow.py
plane_ar.py
squares.py
stitching.py
text_skewness_correction.py
texture_flow.py
turing.py
video_threaded.py
video_v4l2.py
watershed.py

These changes aim to improve the repository's clarity and usability by removing examples that are no longer relevant or have been superseded by more up-to-date techniques.
2024-07-31 16:11:00 +03:00

70 lines
2.3 KiB
Python

#!/usr/bin/env python
'''
This program demonstrates Laplace point/edge detection using
OpenCV function Laplacian()
It captures from the camera of your choice: 0, 1, ... default 0
Usage:
python laplace.py <ddepth> <smoothType> <sigma>
If no arguments given default arguments will be used.
Keyboard Shortcuts:
Press space bar to exit the program.
'''
# Python 2/3 compatibility
from __future__ import print_function
import numpy as np
import cv2 as cv
import sys
def main():
# Declare the variables we are going to use
ddepth = cv.CV_16S
smoothType = "MedianBlur"
sigma = 3
if len(sys.argv)==4:
ddepth = sys.argv[1]
smoothType = sys.argv[2]
sigma = sys.argv[3]
# Taking input from the camera
cap=cv.VideoCapture(0)
# Create Window and Trackbar
cv.namedWindow("Laplace of Image", cv.WINDOW_AUTOSIZE)
cv.createTrackbar("Kernel Size Bar", "Laplace of Image", sigma, 15, lambda x:x)
# Printing frame width, height and FPS
print("=="*40)
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))
while True:
# Reading input from the camera
ret, frame = cap.read()
if ret == False:
print("Can't open camera/video stream")
break
# Taking input/position from the trackbar
sigma = cv.getTrackbarPos("Kernel Size Bar", "Laplace of Image")
# Setting kernel size
ksize = (sigma*5)|1
# Removing noise by blurring with a filter
if smoothType == "GAUSSIAN":
smoothed = cv.GaussianBlur(frame, (ksize, ksize), sigma, sigma)
if smoothType == "BLUR":
smoothed = cv.blur(frame, (ksize, ksize))
if smoothType == "MedianBlur":
smoothed = cv.medianBlur(frame, ksize)
# Apply Laplace function
laplace = cv.Laplacian(smoothed, ddepth, 5)
# Converting back to uint8
result = cv.convertScaleAbs(laplace, (sigma+1)*0.25)
# Display Output
cv.imshow("Laplace of Image", result)
k = cv.waitKey(30)
if k == 27:
return
if __name__ == "__main__":
print(__doc__)
main()
cv.destroyAllWindows()