def camShift(): global frame, frame2, inputmode, trackWindow, roi_hist, out try: cap = cv2.VideoCapture(1) cap.set(3, 480) cap.set(4, 320) except: print('카메라 구동 실패') return ret, frame = cap.read() cv2.nameWindow('frame') cv2.setMouseCallback('frame', onMouse, param=(frame, frame2)) termination = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1) while True: ret, frame = cap.read() if not ret: break if trackWindow is not None: hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1) ret, trackWindow = cv2.Camshift(dst, trackWindow, termination) pts = cv2.boxPoints(ret) pts = np.int0(pts) cv2.polylines(frame, [pts], True, (0, 255, 0), 2) cv2.imshow('frame', frame) k = cv2.waitKey(60) & 0xFF if k == 27: break if k == ord('i'): print('추적할 영역을 지정하고 아무키나 누르세요') inputmode = True frame2 = frame.copy() while inputmode: cv2.imshow('frame', frame) cv2.waitKey(0) cap.release() cv2.destroyAllwindows()
import cv2 cam=cv2.VideoCapture(0) s, img=cam.read() winname="movement" cv2.nameWindow(winname, cv2.CV_WINDOW_AUTOSIZE) while s: cv2.imshow( winName,img ) s, img = cam.read() key = cv2.waitKey(10) if key == 27: cv2.destroyWindow(winName) break print "Goodbye"
#intent : #Author :Michael Jack hu #start date : 2018/10/8 #File : 人脸识别2.py #Software : PyCharm #finish date : ''' # 在识别的图片上面添加人脸识别:重点注意 需要添加模型 # 1.导入库 import cv2 # 2.加载图片 img = cv2.imread('D:\04.jpg') # 3.加载人脸模型 face = cv2.Case("D:\timg.jpg") # 4.调整图片灰度 gray = cv2.cvColor(img, cv2.COLOR_RGB2GRAY) # 5.检查人脸 faces = face.detectMultiScale(gray) # 6.标记人脸 for (x, y, w, h) in faces: #里面有四个参数 1,写图片 2,坐标原点 3,识别大小 4,颜色RGB5,线宽 cv2.rectang(img, (x, y), (x + w, y + h), (0, 255, 0), 10) # 7.创建窗口 cv2.nameWindow('james 窗口') # 8.显示图片 cv2.imshow('jiaqi', img) # 9.暂停窗口 cv2.waitKey(0) # 10.关闭窗口 cv2.destroyAllWindows()
global start_point global end_point global canvas global is_drawing if event == cv2.EVENT_LBUTTONDOWN: if is_drawing: start_point(x,y) elif event == cv2.EVENT_MOUSEMOVE: if is_drawing end_point = (x,y) draw_line(canvas,start_point,end_point) start_point = end_point elif event == cv2.EVENT_LBUTTONUP is_drawing = False cv2.nameWindow(wname) cv2.setMouseCallback(wname, shape) while True: cv2.imshow(wname, canvas) key = cv2.waitKey(1) # press any keyto go to next state if key == ord('q'): break elif key == ord('c'): canvas[50:350, 50:350] = 0 elif key == ord('w'): out = canvas[100:500, 100:500] result = net.predict(image) print("PREDICTION :", result)
#intent :摄像头识别人脸 #Author :Michael Jack hu #start date : 2018/10/8 #File : 人脸识别4.py #Software : PyCharm #finish date : ''' # 1.导入库 import cv2 # 2.加载人脸模型 cv2.CascadeClassifiler() # 3.打开摄像头 capture = cv2.VideoCapture(0) # 4.创建窗口 cv2.nameWindow() # 5.获取摄像头实时画面 cv2.nameWindow('she xiang tou') while True: #获取摄像头的帧画面 ret, frame = capture.read() # 图片灰度调整 gary = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) #检查人脸 faces = face.detecMultiScale(gary, 1.1, 3, 0 (100, 100)) #标记人脸 for (x, y, w, h) in faces: # 里面有四个参数 1,写图片 2,坐标原点 3,识别大小 4,颜色RGB5,线宽 cv2.rectang(frame, (x, y), (x + w, y + h), (0, 255, 0), 10) #显示图片 cv2.imshow('shexiangtou', frame)
def original(): cv2.nameWindow("Original Image") cv2.imshow("Orignal Image", pic)
from PIL import Image import pytesseract import cv2 img = cv2.imread('plate1.png') tex = pytesseract.image_to_string(Image.open('plate1.png'), lang='ben') print(pytesseract.image_to_string(Image.open('plate1.png'), lang='ben')) cv2.nameWindow('Input image') cv2.imshow("Input image", img) cv2.waitkey(0) cv2.destroyWindow("Test") cv2.destroyWindow("Main")
import cv2 import numpy as np cv2.nameWindow("gray") img = np.zeros((512, 512), np.uint8) #生成一张空的灰度图像 cv2.line(img, (0, 0), (511, 511), 255, 5) #绘制一条白色直线 cv2.imshow("gray", img) #显示图像 #循环等待,按q键退出 while True: key = cv2.waitKey(1) if key == ord("q"): break cv2.destoryWindow("gray")
def __init__(self): self.bridge = cv.bridge.CvBridge() cv2.nameWindow("window",1) self.image_sub = rospy.Subscriber("camera/rgb/image_raw",Image,self.image_callback)
import numpy as np img = cv2.imread( "C:\Users\Xiaoxi Chen\Downloads\OpenCV_Preperation\OpenCV_homework\Test_image\baboon.jpg" ) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.iwrite( "C:\Users\Xiaoxi Chen\Downloads\OpenCV_Preperation\OpenCV_homework\Test_image\baboon.jpg", gray) #gray threshold_type = 2 threshold_value = 128 ret, dst = cv2.threshold(gray, threshold_value, 255, cv2.TRESH_TRUNC) cv2.imshow("threshold", dst) cv2.nameWindow("threshold image") #binary threshold current_threshold = 128 max_threshold = 255 ret, thre = cv2.threshold(gray, current_threshold, max_threshold, cv2.THRESH_BINARY) cv2.imshow("binary threshold", thre) cv2.nameWindow("binary threshold") #band thresholding threshold1 = 27 threshold2 = 125 ret, bin1 = cv2.thresold(gray, threshold1, 255, cv2.THRESH_BINARY) ret, bin2 = cv2.thresold(gray, threshold2, 255, cv2.THRESH_BINARY_INV) band_thre_img = np.bitwise_and(bin1, bin2)
# -*- coding: utf-8 -*- """ Created on Sat Jul 11 23:18:29 2020 @author: ZYD """ """ 【学习内容】 学习使用cv.getTrackbarPos(), cv.createTrackbar。 了解将滑动条固定到openCV窗口。 【代码内容】 在GUI中使用滑动条自定义的RGB颜色。 """ import cv2 as cv import numpy as np def nothing(x): pass img = np.zeros((300,512,3), np.uint8) #创建黑色图像 cv.nameWindow('image') #创建窗口 #创建颜色变化的滑动条 cv.createTrackbar('R', 'image', 0, 255, nothing) cv.createTrackbar('G', 'image', 0, 255, nothing) cv.createTrackbar('B', 'image', 0, 255, nothing)
import cv2 import numpy as np x,y,z=200,200,-1 cap= cv2.VideoCapture(0) def take_inp(event, x1,y1,flag,param): gloabal x,y,k if event == cv2.EVENT_LBUTTONDOWN: x=x1 y=y1 k=1 cv2.nameWindow("enter_point") cv2
# -- coding: utf-8 -- import numpy as np import cv2 from matplotlib import pyplot as plt # python绘图库 # 读入图像 # Load an color image in grayscale img = cv2.imread('../test0.png', 0) # 事先创建一个window,可调整大小 cv2.nameWindow('image', cv2.WINDOW_NORMAL) # 显示图像 cv2.imshow('image', img) # cv2.waiKey(0) k = cv2.waitKey(0) if k == 27: # wait for ESC key to exit cv2.destroyAllWindows() # 删除所有窗口,删除自定窗口使用cv2.destroyWindow(windowname) elif k == ord('s') # wait for 's' key to save and exit cv2.imwrite('test0Gray.png', img) # 保存图像 cv2.destroyAllWindows() # 使用matplotlib img = cv2.imread('test0.png', 0) plt.imshow(img, cmap = 'gray', interpolation = 'bicubic') plt.xticks([]), plt.yticks([]), # to hide tick values on X and Y axis plt.show()
if not depth_frame or not color_frame: continue depth_image = np.asanyarray(depth_frame.get_data()) color_image = np.asanyarray(color_frame.get_data()) depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image,alph=0.03),cv2.COLORMAP_JET) face_cascade = cv2.CascadeClassifier('/home/isp/Desktop/opencv-master/data/haarcascades/haarcascade_frontalface_default.xml') gray = cv2.cvtColor(color_image,cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray,scaleFactor=1.2,minNeighbor=5,minsize=(50,50)) for (x,y,w,h) in faces: cv2.rectangle(color_image,(x,y-5),(x+w,y+h),(255,0,0),2) text_depth = "depth is "+str(np.round(depth_frame.get_distance(int (x+(1/2)*w),int (y+(1/2)*h)),3)+"m") color_image=cv2.putText(color_image,text_depth,(x,y-5),cv2.FONT_HERSHEY_PLAIN,1,(0.0.255),1,cv2.LINE_AA) images = np.hstack((color_image,depthcolormap)) cv2.nameWindow('Realsense',cv2.WINDOW_AUTOSIZE) cv2.imshow('Realsense',images) key = cv2.waitKey(1) if Key & 0xFF == ord('q') or Key == 27: cv2.destroyAllWindows() break finally: pipeline.stop()
import cv2 import numpy as np events = [i for i in dir(cv2) if 'EVENT' in i] print events # mouse callback function def draw_circle(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDBLCLK: cv2.circle(img, (x,y), 100, (255, 0, 0), -1) #window with a black pic img = np.zeros((512, 512, 3), np.unit8) cv2.nameWindow('image') cv2.setMouseCallback
# -*- coding: utf-8 -*- ''' #intent :人脸识别 #Author :Michael Jack hu #start date : 2018/10/8 #File : 人脸识别.py #Software : PyCharm #finish date : ''' # #1. 导入库 import cv2 font = cv2.FONT_HERSHEY_SIMPLEX #2. 加载图片 img = cv2.imread('C\huzhi\Desktop\04.jpg') #3. 创建窗口 cv2.nameWindow('james') #4. 显示图片 cv2.imshow('jiaqi', img) #5. 暂停窗口 cv2.waitKey(0) #6. 关闭窗口 cv2.destroyAllWindows()
import cv2 import numpy as np img = cv2.imread("C:\Users\Xiaoxi Chen\Downloads\OpenCV_Preperation\OpenCV_homework\Test_images\Lenna") cv2.nameWindow("Lenna") cv2.imshow("Lenna", img) #show RGB r,g,b = cv2.split(img) cv2.imshow('Original Image', img) cv2.imshow('Red', r) cv2.imwrite('Red.png', r) cv2.imshow('Green', g) cv2.imwrite('Green.png', g) cv2.imshow('Blue', b) cv2.imwrite('Blue.png', b) rgbPixel = img[20,25] print rgbPixel #YCC imgYCC = cv2.cvtColor(img, cv2.COLOR_RGB2YCrCb) Y,Cb,Cr = cv2.split(ycrcb_image) cv2.imshow('Y', y) cv2.imwrite('Y.png', y) cv2.imshow('Cr', cr) cv2.imwrite('Cr.png', cr) cv2.imshow('Cb', cb) cv2.imwrite('Cb.png', cb) ycrcbPixel = imgYCC[20,25] print ycrcbPixel
import cv2 import numpy as np from math import tan,pi pipe=rs.pipeline() cfg=rs.config() cfg.enable_stream(rs.stream.pose) pipe.start(cfg) try: WINDOW_TITLE='Realsense' cv2.nameWindow(WINDOW_TITLE,cv2.WINDOW_NORMAL) window_size=5 min_disp=0 num_disp=112-min_disp max_disp=min_disp+num_disp stereo = cv2.StereoSGBM_create(minDisparity = min_disp, numDisparities = num_disp, blockSize = 16, P1 = 8*3*window_size**2, P2 = 32*3*window_size**2, disp12MaxDiff = 1, uniquenessRatio = 10, speckleWindowSize = 100,
import numpy as np import cv2 from matplotlib import pyplot as plt img = cv2.imread('messi5.jpg', 0) plt.imshow(img, cmap='gray', interpolation='bicubic') plt.xticks([]), plt.yticks([]) plt.show() """ cv2.nameWindow('image',cv2.WINDOW_NORMAL) cv2.imshow('inamge',img) cv2.waitKey(0) cv2.destroyAllWindows() """