
Advanced OpenCV Image Processing Techniques in Python
Explore advanced OpenCV image processing techniques in Python with examples such as importing images, resizing, cropping, adding shapes and text, webcam and IP camera usage, and more. Enhance your computer vision skills with this comprehensive guide.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
2021 OpenCV
cvimage.py OpenCV webcam ipCam
import cv2 print("cv2 imported") img=cv2.imread("data/mouth.jpg") cv2.imshow("Output", img) cv2.waitKey(0) # 1 0.001
import cv2 print("cv2 imported") cap = cv2.VideoCapture("data/cck.mp4") # cap = cv2.VideoCapture(0) while True: success, frame = cap.read() cv2.imshow("video", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break
cvimage2.py OpenCV
import cv2 import numpy as np img=cv2.imread("data/mouth.jpg") img= cv2.resize(img, (320,200)) print(img.shape) imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) imgBlur = cv2.GaussianBlur(imgGray,(7,7),0) imgCanny = cv2.Canny(img, 300,300) print(imgBlur.shape) imgGray3 = cv2.cvtColor(imgGray, cv2.COLOR_GRAY2BGR) imghor3 = np.hstack((imgGray3,img)) imghor = np.hstack((imgGray,imgBlur,imgCanny)) imgver = np.vstack((imghor,imghor)) cv2.imshow("Output all", imgver) cv2.imshow("Output 3", imghor3) cv2.waitKey(0)
cvimage3.py OpenCV imgCropped
import cv2 img = cv2.imread("data/mouth.jpg") img = cv2.resize(img,(800,480)) imgCropped = img[210:460, 160:360] print(img.shape) print(imgCropped.shape) cv2.imshow("Image",img) cv2.imshow("Image Cropped",imgCropped) cv2.waitKey(0)
cvimage4.py OpenCV
import cv2 import numpy as np img = np.zeros((512,512,3),np.uint8) # print(img.shape) img[:] = 255,0,0 img[200:300,100:300]=0,255,0 cv2.line(img,(0,0),(img.shape[1],img.shape[0]),(0,255,0),1) cv2.rectangle(img,(0,0),(255,350),(0,0,255),1) cv2.circle(img,(400,50),30,(255,255,0),5) cv2.putText(img," NTUST ",(300,200),cv2.FONT_HERSHEY_COMPLEX,1,(0,150,0),2) cv2.imshow("Image",img) cv2.waitKey(0)
cvimage5.py OpenCV
import cv2 import numpy as np img1=cv2.imread("data/1.jpg") img1= cv2.resize(img1, (320,200)) img2=cv2.imread("data/2.jpg") img2= cv2.resize(img2, (320,200)) img3 = cv2.add(img1, img2) img4 = cv2.addWeighted(img1,0.7,img2,0.3,0) imghor = np.hstack((img1,img2,img3, img4)) cv2.imshow("Image",imghor) cv2.waitKey(0)
cvimage6.py OpenCV bar
import cv2 def nothing(x): pass img1 = cv2.imread('data/1.jpg') img2 = cv2.imread('data/2.jpg') img1 = cv2.resize(img1, (640, 480)) img2 = cv2.resize(img2, (640, 480)) cv2.namedWindow('CV1') cv2.createTrackbar('%', 'CV1', 0, 100, nothing) while (True): r = cv2.getTrackbarPos('%', 'CV1') r = float(r) / 100.0 img = cv2.addWeighted(img1, r, img2, 1.0 - r, 0) cv2.putText(img, str(r), (3, 40), cv2.FONT_HERSHEY_PLAIN, 3, (255, 255, 255), 3) cv2.imshow('CV1', img) if cv2.waitKey(5) & 0xFF == 27: break cv2.destroyAllWindows()
cvimage7.py OpenCV bar HSV np.hstack
print(h_min, h_max, s_min, s_max, v_min, v_max) import cv2 lower = np.array([h_min, s_min, v_min]) import numpy as np upper = np.array([h_max, s_max, v_max]) # bar lower,upper mask from PIL import ImageFont, ImageDraw, Image mask = cv2.inRange(imgHSV, lower, upper) # cv2.imshow("hsv", imgHSV) def nothing(x): # cv2.imshow("mask",mask) # mask bitwise_and pass font = ImageFont.truetype("simsun.ttc", 20) # imgResult = cv2.bitwise_and(img, img, mask=mask) imgtypes = ["original","HSV","Mask","result"] imgtypesC = [" ","HSV"," "," "] imgmask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR) imghor = np.hstack((img, imgHSV,imgmask, imgResult)) cv2.namedWindow("TrackBars") for i in range(0, len(imgtypesC)): cv2.resizeWindow("TrackBars",640,300) # 6 bar cv2.putText(imghor, imgtypes[i], ((10 + 280 * i), 14), 4, 0.6, (0, 255, 0), 1) cv2.createTrackbar("Hue Min","TrackBars",0,179,nothing) # PIL Draw PIL numpy array img_pil = Image.fromarray(imghor) # numpy array PIL draw = ImageDraw.Draw(img_pil) # cv2.createTrackbar("Hue Max","TrackBars",179,179,nothing) cv2.createTrackbar("Sat Min","TrackBars",0,255,nothing) cv2.createTrackbar("Sat Max","TrackBars",255,255,nothing) for i in range(0, len(imgtypesC)): draw.text(((10 + 280 * i), 20), imgtypesC[i], font=font, fill=(100, 10, 255, 1)) # imghor = np.array(img_pil) # PIL numpy array cv2.createTrackbar("Val Min","TrackBars",0,255,nothing) cv2.createTrackbar("Val Max","TrackBars",255,255,nothing) while (True): img = cv2.imread("data/7.jpg") cv2.imshow("all images", imghor) img = cv2.resize(img, (280, 200)) if cv2.waitKey(5) & 0xFF == 27: imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) break h_min = cv2.getTrackbarPos("Hue Min", "TrackBars") h_max = cv2.getTrackbarPos("Hue Max", "TrackBars") s_min = cv2.getTrackbarPos("Sat Min", "TrackBars") s_max = cv2.getTrackbarPos("Sat Max", "TrackBars") v_min = cv2.getTrackbarPos("Val Min", "TrackBars") v_max = cv2.getTrackbarPos("Val Max", "TrackBars")
takepic.py OpenCV
import cv2 from time import strftime import os labels = ['nomask','mask'] cap = cv2.VideoCapture(0) while cap.isOpened(): success, frame = cap.read() cv2.imshow('get pic', frame) keyb = cv2.waitKey(100) & 0xFF if keyb == 27: break elif keyb == ord('0') or keyb == ord('1'): print(keyb - 48) systime = strftime("%H%M%S") imgname = os.path.join('data/images', labels[keyb - 48] + '.' + systime + '.jpg') cv2.imwrite(imgname, frame) cap.release() cv2.destroyAllWindows()