import cv2 as cv import numpy as np import math from matplotlib import pyplot as plt def do_nothing(value): print(value) def binary_demo(): src = cv.imread("D:/javaopencv/lena.png", cv.IMREAD_GRAYSCALE) cv.namedWindow("input", cv.WINDOW_AUTOSIZE) cv.createTrackbar("Threshold:", "input", 0, 255, do_nothing) cv.imshow("input", src) t = 127 while(True): ret, dst = cv.threshold(src, t, 255, cv.THRESH_BINARY) cv.imshow("binary", dst) print("threshold value : ", ret) t = cv.getTrackbarPos("Threshold:", "input") c = cv.waitKey(10) if c == 27: break; def threshold_segmentation_demo(): src = cv.imread("D:/javaopencv/lena.png", cv.IMREAD_GRAYSCALE) cv.imshow("input", src) t = 127 ret, dst = cv.threshold(src, t, 255, cv.THRESH_BINARY | cv.THRESH_TRIANGLE) cv.imshow("binary", dst) print("threshold value : ", ret) def threshold_method_demo(): src = cv.imread("D:/javaopencv/text1.png", cv.IMREAD_GRAYSCALE) cv.imshow("input", src) dst = cv.adaptiveThreshold(src, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 25, 10) cv.imshow("binary", dst) def threshold_noise_demo(): src = cv.imread("D:/javaopencv/noise_rice.png") src = cv.fastNlMeansDenoisingColored(src, None, 15, 15, 10, 30) cv.imshow("input", src) src = cv.GaussianBlur(src, (5, 5), 0) gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) ret, dst = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) cv.imshow("binary", dst) def connected_components_demo(): src = cv.imread("D:/javaopencv/rice.png") cv.imshow("input", src) src = cv.GaussianBlur(src, (3, 3), 0) gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) cv.imshow("binary", binary) output = cv.connectedComponents(binary, connectivity=8, ltype=cv.CV_32S) num_labels = output[0] labels = output[1] colors = [] for i in range(num_labels): b = np.random.randint(0, 256) g = np.random.randint(0, 256) r = np.random.randint(0, 256) colors.append((b, g, r)) colors[0] = (0, 0, 0) h, w = gray.shape image = np.zeros((h, w, 3), dtype=np.uint8) for row in range(h): for col in range(w): image[row, col] = colors[labels[row, col]] cv.imshow("colored labels", image) print("total rice : ", num_labels - 1) output_more = cv.connectedComponentsWithStats(binary, connectivity=8, ltype=cv.CV_32S) num_labels = output_more[0] labels = output_more[1] stats = output_more[2] centers = output_more[3] image_stats = cv.cvtColor(binary, cv.COLOR_GRAY2BGR) for i in range(num_labels): if i == 0: continue cx, cy = centers[i] x, y, width, height, area = stats[i] print("area : ", area) cv.rectangle(image_stats, (x, y), (x+width, y+height), (0, 0, 255), 2, 8, 0) cv.circle(image_stats, (np.int(cx), np.int(cy)), 2, (255, 0, 0), -1, 8, 0) cv.imshow("statistic", image_stats) def find_contours_demo(): src = cv.imread("D:/vcprojects/images/fish.jpg") cv.imshow("input", src) src = cv.GaussianBlur(src, (3, 3), 0) gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) cv.imshow("binary", binary) image, contours, hierachy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) print("total number: ", len(contours)) for i in range(len(contours)): cv.drawContours(src, contours, i, (0, 0, 255), 2, 8) cv.imshow("contours-demo", src) def measure_contours_demo(): src = cv.imread("D:/javaopencv/rice.png") cv.imshow("input", src) src = cv.GaussianBlur(src, (3, 3), 0) gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) cv.imshow("binary", binary) image, contours, hierachy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) print("total number: ", len(contours)) h, w = gray.shape for i in range(len(contours)): area = cv.contourArea(contours[i]) arclen = cv.arcLength(contours[i], True) x, y, ww, hh = cv.boundingRect(contours[i]) if area < 10 or arclen < 10: continue ratio = np.minimum(ww, hh)/np.maximum(ww, hh) if ratio > 0.8: mm = cv.moments(contours[i]) m00 = mm['m00'] m10 = mm['m10'] m01 = mm['m01'] cx = np.int(m10/m00) cy = np.int(m01/m00) (x, y), (a, b), degree = cv.fitEllipse(contours[i]) print(np.int(a), np.int(b), degree) cv.circle(src, (cx, cy), 2, (255, 0, 0), -1, 8, 0) cv.putText(src, str(np.int(degree)), (cx-40, cy-40), cv.FONT_HERSHEY_PLAIN, 1.0, (255, 0, 255), 1) print("area : %d, arclen : %d" % (area, arclen)) cv.drawContours(src, contours, i, (0, 0, 255), 2, 8) cv.imshow("contours-demo", src) def distance_demo(): src = cv.imread("D:/javaopencv/rice.png") cv.imshow("input", src) src = cv.GaussianBlur(src, (3, 3), 0) gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) cv.imshow("binary", binary) dist = cv.distanceTransform(binary, cv.DIST_L2, 3, dstType=cv.CV_32F) cv.normalize(dist, dist, 0, 1, cv.NORM_MINMAX) ret, binary = cv.threshold(dist, 0.8, 1, cv.THRESH_BINARY) cv.imshow("distance-transform", dist) cv.imshow("distance-binary", binary) def point_polygon_test_demo(): src = cv.imread("D:/javaopencv/ppt.png") cv.imshow("input", src) src = cv.GaussianBlur(src, (3, 3), 0) gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) cv.imshow("binary", binary) image, contours, hierachy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) print("total number: ", len(contours)) h, w = src.shape[:2] for row in range(h): for col in range(w): dist = cv.pointPolygonTest(contours[0], (col, row), True) print(dist) if dist > 0: src[row, col] = (255 - dist*2, 0, 0) if dist < 0: src[row, col] = (0, 0, 180 - np.abs(dist)) cv.imshow("ppt-demo", src) def binary_projection_demo(): src = cv.imread("D:/vcprojects/images/canjian.jpg") cv.imshow("input", src) src = cv.GaussianBlur(src, (3, 3), 0) gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) cv.imshow("binary", binary) h, w = gray.shape print(gray.shape) y_projection = np.zeros((h), dtype=np.int32) x_projection = np.zeros((w), dtype=np.int32) for row in range(h): count = 0 for col in range(w): pv = binary[row, col] if pv == 255: count += 1 y_projection[row] = count for col in range(w): count = 0 for row in range(h): pv = binary[row, col] if pv == 255: count += 1 x_projection[col] = count plt.plot(y_projection, color='b') plt.xlim([0, h]) plt.show() plt.plot(x_projection, color='r') plt.xlim([0, w]) plt.show() def match_shapes_demo(): src1 = cv.imread("D:/vcprojects/images/m1.png", cv.IMREAD_GRAYSCALE) src2 = cv.imread("D:/vcprojects/images/m4.png", cv.IMREAD_GRAYSCALE) ret, binary1 = cv.threshold(src1, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) ret, binary2 = cv.threshold(src2, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) cv.imshow("input-1", binary1) cv.imshow("input-2", binary2) image, contours1, hierachy = cv.findContours(binary1, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) image, contours2, hierachy = cv.findContours(binary2, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) mm1 = cv.moments(contours1[0]) mm2 = cv.moments(contours2[0]) hu1 = cv.HuMoments(mm1) hu2 = cv.HuMoments(mm2) dist = cv.matchShapes(hu1, hu2, cv.CONTOURS_MATCH_I3, 0) print("match distance : ", dist) def hough_line_demo(): src = cv.imread("D:/vcprojects/images/roadlines.png") cv.imshow("input", src) image = np.copy(src) src = cv.GaussianBlur(src, (3, 3), 0) edges = cv.Canny(src, 150, 300, apertureSize=3) cv.imshow("edges", edges) lines = cv.HoughLines(edges, 1, np.pi/180, 150, None, 0, 0) if lines is not None: for i in range(0, len(lines)): rho = lines[i][0][0] theta = lines[i][0][1] a = math.cos(theta) b = math.sin(theta) x0 = a*rho y0 = b*rho pt1 = (int(x0+1000*(-b)), int(y0+a*1000)) pt2 = (int(x0-1000*(-b)), int(y0-a*1000)) cv.line(src, pt1, pt2, (0, 0, 255), 2, 8, 0) cv.imshow("hough-lines", src) linesP = cv.HoughLinesP(edges, 1, np.pi/180, 100, None, 50, 10) if lines is not None: for i in range(0, len(linesP)): l = linesP[i][0] cv.line(image, (l[0], l[1]), (l[2], l[3]), (0, 0, 255), 2, 8, 0) cv.imshow("hough-linesP", image) def hough_circle_demo(): src = cv.imread("D:/javaopencv/coins.jpg") cv.imshow("input", src) gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) gray = cv.fastNlMeansDenoising(gray, None, 15, 10, 30) rows, cols = gray.shape print(rows/8) circles = cv.HoughCircles(gray, cv.HOUGH_GRADIENT, 1, rows/8, None, param1=100, param2=30, minRadius=10, maxRadius=50) if circles is not None: for i in circles[0, :]: center = (i[0], i[1]) raidus = i[2] cv.circle(src, center, 2, (0, 0, 0), -1, 8, 0) cv.circle(src, center, raidus, (0, 255, 255), 2, 8, 0) cv.imshow("circles", src) def video_io_demo(): capture = cv.VideoCapture("D:/vcprojects/images/768x576.avi") # capture = cv.VideoCapture(0) height = capture.get(cv.CAP_PROP_FRAME_HEIGHT) width = capture.get(cv.CAP_PROP_FRAME_WIDTH) count = capture.get(cv.CAP_PROP_FRAME_COUNT) fps = capture.get(cv.CAP_PROP_FPS) print(height, width, count, fps) out = cv.VideoWriter("D:/test.mp4", cv.VideoWriter_fourcc('D', 'I', 'V', 'X'), 15, (np.int(width), np.int(height)), True) type = 0 bgfg = cv.createBackgroundSubtractorMOG2() k = cv.getStructuringElement(cv.MORPH_RECT, (3, 3)) while(True): ret, frame = capture.read() if ret is True: cv.imshow("video-input", frame) # result = process_frame(frame, type) mask = bgfg.apply(frame) bg_image = bgfg.getBackgroundImage() mask = cv.morphologyEx(mask, cv.MORPH_OPEN, k) cv.imshow("video-result", mask) cv.imshow("background", bg_image) c = cv.waitKey(20) if c > 0: type = np.abs(c) % 5 out.write(frame) if c == 27: #ESC break else: break def process_frame(frame, type): if type == 0: gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) # ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) binary = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY, 25, 10) return binary if type == 1: dst = cv.GaussianBlur(frame, (0, 0), 15) return dst if type == 2: kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) im = cv.filter2D(frame, -1, kernel) aw = cv.addWeighted(im, 2, cv.GaussianBlur(frame, (0, 0), 15), -2, 128) return aw if type == 3: dst = cv.bilateralFilter(frame, 0, 50, 5) return dst if type == 4: return detect_face(frame) # face_detector = cv.CascadeClassifier("D:/opencv-3.4/opencv/build/etc/haarcascades/haarcascade_frontalface_alt_tree.xml") face_detector = cv.CascadeClassifier("D:/opencv-3.4/opencv/build/etc/lbpcascades/lbpcascade_frontalface_improved.xml") def detect_face(frame): gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) cv.equalizeHist(gray, gray) faces = face_detector.detectMultiScale(gray, 1.2, 1, minSize=(40, 40), maxSize=(300, 300)) for x, y, w, h in faces: cv.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2, 8, 0) return frame def color_object_trace(): capture = cv.VideoCapture(0) height = capture.get(cv.CAP_PROP_FRAME_HEIGHT) width = capture.get(cv.CAP_PROP_FRAME_WIDTH) count = capture.get(cv.CAP_PROP_FRAME_COUNT) fps = capture.get(cv.CAP_PROP_FPS) print(height, width, count, fps) k = cv.getStructuringElement(cv.MORPH_RECT, (5, 5)) while (True): ret, frame = capture.read() if ret is True: cv.imshow("video-input", frame) # stage one hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV) # 黑色 mask = cv.inRange(hsv, (0, 0, 0), (180,255,46)) mask = cv.inRange(hsv, (35, 43, 46), (77, 255, 255)) mask = cv.morphologyEx(mask, cv.MORPH_OPEN, k) cv.imshow("mask", mask) image, contours, hierachy = cv.findContours(mask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) max = 0 temp = 0 index = -1 for i in range(len(contours)): x, y, w, h = cv.boundingRect(contours[i]) temp = w*h if temp > max: max = temp index = i if index >= 0: rrt = cv.fitEllipse(contours[index]) cv.ellipse(frame, rrt, (0, 0, 255), 2, cv.LINE_AA) cv.imshow("trac-object-demo", frame) c = cv.waitKey(50) if c == 27: # ESC break else: break if __name__ == "__main__": video_io_demo() cv.waitKey(0) cv.destroyAllWindows()
image_video_analysis
猜你喜欢
转载自blog.csdn.net/xsjzdrxsjzdr/article/details/93402116
今日推荐
周排行