From 71e4aa85367ef9af880204f13029211f16ff4f80 Mon Sep 17 00:00:00 2001 From: zhang Date: Sat, 28 Mar 2020 20:32:36 +0800 Subject: =?UTF-8?q?=E6=95=B0=E5=AD=97=E5=9B=BE=E5=83=8F=E5=A4=84=E7=90=86?= =?UTF-8?q?=E8=A1=8C=E4=BA=BA=E6=A3=80=E6=B5=8B=E4=B8=8E=E5=89=8D=E6=99=AF?= =?UTF-8?q?=E8=83=8C=E6=99=AF=E5=88=86=E5=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dip/data/imgs/persons/person_010.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_011.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_014.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_029.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_032.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_033.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_044.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_063.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_085.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_086.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_090.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_095.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_104.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_120.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_127.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_133.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_136.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_147.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_156.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_175.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_220.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_222.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_251.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_258.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_265.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_293.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_390.bmp | Bin 0 -> 921654 bytes dip/data/imgs/persons/person_454.bmp | Bin 0 -> 921654 bytes dip/hog_det/pedestrian_det.py | 101 +++++++++++++++++++++++++++++++++++ dip/segmentation/grab_cut.py | 41 ++++++++++++++ dip/segmentation/image_fusion.py | 53 ++++++++++++++++++ dip/segmentation/result.jpg | Bin 0 -> 9900 bytes dip/segmentation/sample_dog.jpg | Bin 0 -> 163759 bytes dip/segmentation/sample_giraffe.jpg | Bin 0 -> 382965 bytes dip/segmentation/sample_person.jpg | Bin 0 -> 113880 bytes dip/segmentation/sample_scream.jpg | Bin 0 -> 174515 bytes 36 files changed, 195 insertions(+) create mode 100644 dip/data/imgs/persons/person_010.bmp create mode 100644 dip/data/imgs/persons/person_011.bmp create mode 100644 dip/data/imgs/persons/person_014.bmp create mode 100644 dip/data/imgs/persons/person_029.bmp create mode 100644 dip/data/imgs/persons/person_032.bmp create mode 100644 dip/data/imgs/persons/person_033.bmp create mode 100644 dip/data/imgs/persons/person_044.bmp create mode 100644 dip/data/imgs/persons/person_063.bmp create mode 100644 dip/data/imgs/persons/person_085.bmp create mode 100644 dip/data/imgs/persons/person_086.bmp create mode 100644 dip/data/imgs/persons/person_090.bmp create mode 100644 dip/data/imgs/persons/person_095.bmp create mode 100644 dip/data/imgs/persons/person_104.bmp create mode 100644 dip/data/imgs/persons/person_120.bmp create mode 100644 dip/data/imgs/persons/person_127.bmp create mode 100644 dip/data/imgs/persons/person_133.bmp create mode 100644 dip/data/imgs/persons/person_136.bmp create mode 100644 dip/data/imgs/persons/person_147.bmp create mode 100644 dip/data/imgs/persons/person_156.bmp create mode 100644 dip/data/imgs/persons/person_175.bmp create mode 100644 dip/data/imgs/persons/person_220.bmp create mode 100644 dip/data/imgs/persons/person_222.bmp create mode 100644 dip/data/imgs/persons/person_251.bmp create mode 100644 dip/data/imgs/persons/person_258.bmp create mode 100644 dip/data/imgs/persons/person_265.bmp create mode 100644 dip/data/imgs/persons/person_293.bmp create mode 100644 dip/data/imgs/persons/person_390.bmp create mode 100644 dip/data/imgs/persons/person_454.bmp create mode 100644 dip/hog_det/pedestrian_det.py create mode 100644 dip/segmentation/grab_cut.py create mode 100644 dip/segmentation/image_fusion.py create mode 100644 dip/segmentation/result.jpg create mode 100644 dip/segmentation/sample_dog.jpg create mode 100644 dip/segmentation/sample_giraffe.jpg create mode 100644 dip/segmentation/sample_person.jpg create mode 100644 dip/segmentation/sample_scream.jpg (limited to 'dip') diff --git a/dip/data/imgs/persons/person_010.bmp b/dip/data/imgs/persons/person_010.bmp new file mode 100644 index 0000000..4597574 Binary files /dev/null and b/dip/data/imgs/persons/person_010.bmp differ diff --git a/dip/data/imgs/persons/person_011.bmp b/dip/data/imgs/persons/person_011.bmp new file mode 100644 index 0000000..65bdc52 Binary files /dev/null and b/dip/data/imgs/persons/person_011.bmp differ diff --git a/dip/data/imgs/persons/person_014.bmp b/dip/data/imgs/persons/person_014.bmp new file mode 100644 index 0000000..f6df899 Binary files /dev/null and b/dip/data/imgs/persons/person_014.bmp differ diff --git a/dip/data/imgs/persons/person_029.bmp b/dip/data/imgs/persons/person_029.bmp new file mode 100644 index 0000000..a9e3cf8 Binary files /dev/null and b/dip/data/imgs/persons/person_029.bmp differ diff --git a/dip/data/imgs/persons/person_032.bmp b/dip/data/imgs/persons/person_032.bmp new file mode 100644 index 0000000..f7be1f5 Binary files /dev/null and b/dip/data/imgs/persons/person_032.bmp differ diff --git a/dip/data/imgs/persons/person_033.bmp b/dip/data/imgs/persons/person_033.bmp new file mode 100644 index 0000000..a1096fa Binary files /dev/null and b/dip/data/imgs/persons/person_033.bmp differ diff --git a/dip/data/imgs/persons/person_044.bmp b/dip/data/imgs/persons/person_044.bmp new file mode 100644 index 0000000..5846743 Binary files /dev/null and b/dip/data/imgs/persons/person_044.bmp differ diff --git a/dip/data/imgs/persons/person_063.bmp b/dip/data/imgs/persons/person_063.bmp new file mode 100644 index 0000000..6262c11 Binary files /dev/null and b/dip/data/imgs/persons/person_063.bmp differ diff --git a/dip/data/imgs/persons/person_085.bmp b/dip/data/imgs/persons/person_085.bmp new file mode 100644 index 0000000..ff5e8b5 Binary files /dev/null and b/dip/data/imgs/persons/person_085.bmp differ diff --git a/dip/data/imgs/persons/person_086.bmp b/dip/data/imgs/persons/person_086.bmp new file mode 100644 index 0000000..3170a6e Binary files /dev/null and b/dip/data/imgs/persons/person_086.bmp differ diff --git a/dip/data/imgs/persons/person_090.bmp b/dip/data/imgs/persons/person_090.bmp new file mode 100644 index 0000000..316190f Binary files /dev/null and b/dip/data/imgs/persons/person_090.bmp differ diff --git a/dip/data/imgs/persons/person_095.bmp b/dip/data/imgs/persons/person_095.bmp new file mode 100644 index 0000000..90676e5 Binary files /dev/null and b/dip/data/imgs/persons/person_095.bmp differ diff --git a/dip/data/imgs/persons/person_104.bmp b/dip/data/imgs/persons/person_104.bmp new file mode 100644 index 0000000..069b83d Binary files /dev/null and b/dip/data/imgs/persons/person_104.bmp differ diff --git a/dip/data/imgs/persons/person_120.bmp b/dip/data/imgs/persons/person_120.bmp new file mode 100644 index 0000000..0eb1272 Binary files /dev/null and b/dip/data/imgs/persons/person_120.bmp differ diff --git a/dip/data/imgs/persons/person_127.bmp b/dip/data/imgs/persons/person_127.bmp new file mode 100644 index 0000000..f9c4854 Binary files /dev/null and b/dip/data/imgs/persons/person_127.bmp differ diff --git a/dip/data/imgs/persons/person_133.bmp b/dip/data/imgs/persons/person_133.bmp new file mode 100644 index 0000000..9cf9086 Binary files /dev/null and b/dip/data/imgs/persons/person_133.bmp differ diff --git a/dip/data/imgs/persons/person_136.bmp b/dip/data/imgs/persons/person_136.bmp new file mode 100644 index 0000000..e3582ad Binary files /dev/null and b/dip/data/imgs/persons/person_136.bmp differ diff --git a/dip/data/imgs/persons/person_147.bmp b/dip/data/imgs/persons/person_147.bmp new file mode 100644 index 0000000..66732b7 Binary files /dev/null and b/dip/data/imgs/persons/person_147.bmp differ diff --git a/dip/data/imgs/persons/person_156.bmp b/dip/data/imgs/persons/person_156.bmp new file mode 100644 index 0000000..dfa88b5 Binary files /dev/null and b/dip/data/imgs/persons/person_156.bmp differ diff --git a/dip/data/imgs/persons/person_175.bmp b/dip/data/imgs/persons/person_175.bmp new file mode 100644 index 0000000..b7b8e8b Binary files /dev/null and b/dip/data/imgs/persons/person_175.bmp differ diff --git a/dip/data/imgs/persons/person_220.bmp b/dip/data/imgs/persons/person_220.bmp new file mode 100644 index 0000000..d716bd2 Binary files /dev/null and b/dip/data/imgs/persons/person_220.bmp differ diff --git a/dip/data/imgs/persons/person_222.bmp b/dip/data/imgs/persons/person_222.bmp new file mode 100644 index 0000000..40d4cef Binary files /dev/null and b/dip/data/imgs/persons/person_222.bmp differ diff --git a/dip/data/imgs/persons/person_251.bmp b/dip/data/imgs/persons/person_251.bmp new file mode 100644 index 0000000..0d46c09 Binary files /dev/null and b/dip/data/imgs/persons/person_251.bmp differ diff --git a/dip/data/imgs/persons/person_258.bmp b/dip/data/imgs/persons/person_258.bmp new file mode 100644 index 0000000..8b23988 Binary files /dev/null and b/dip/data/imgs/persons/person_258.bmp differ diff --git a/dip/data/imgs/persons/person_265.bmp b/dip/data/imgs/persons/person_265.bmp new file mode 100644 index 0000000..f322ae0 Binary files /dev/null and b/dip/data/imgs/persons/person_265.bmp differ diff --git a/dip/data/imgs/persons/person_293.bmp b/dip/data/imgs/persons/person_293.bmp new file mode 100644 index 0000000..4f71d76 Binary files /dev/null and b/dip/data/imgs/persons/person_293.bmp differ diff --git a/dip/data/imgs/persons/person_390.bmp b/dip/data/imgs/persons/person_390.bmp new file mode 100644 index 0000000..f7e139c Binary files /dev/null and b/dip/data/imgs/persons/person_390.bmp differ diff --git a/dip/data/imgs/persons/person_454.bmp b/dip/data/imgs/persons/person_454.bmp new file mode 100644 index 0000000..2396327 Binary files /dev/null and b/dip/data/imgs/persons/person_454.bmp differ diff --git a/dip/hog_det/pedestrian_det.py b/dip/hog_det/pedestrian_det.py new file mode 100644 index 0000000..11f54b0 --- /dev/null +++ b/dip/hog_det/pedestrian_det.py @@ -0,0 +1,101 @@ +import cv2 +import imutils +import numpy as np +from imutils import paths +from imutils.object_detection import non_max_suppression + + +def hog_clf(descriptor_type='default'): + if descriptor_type == 'daimler': + winSize = (48, 96) + blockSize = (16, 16) + blockStride = (8, 8) + cellSize = (8, 8) + nbins = 9 + hog = cv2.HOGDescriptor(winSize, blockSize, blockStride, cellSize, nbins) + hog.setSVMDetector(cv2.HOGDescriptor_getDaimlerPeopleDetector()) + return hog + else: + winSize = (64, 128) + blockSize = (16, 16) + blockStride = (8, 8) + cellSize = (8, 8) + nbins = 9 + hog = cv2.HOGDescriptor(winSize, blockSize, blockStride, cellSize, nbins) + hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) + return hog + + +def detect_image(hog, image): + # image = cv2.imread(image_path) + image = imutils.resize(image, width=min(400, image.shape[1])) + orig = image.copy() + + # detect people in the image + (rects, weights) = hog.detectMultiScale(image, winStride=(4, 4), + padding=(8, 8), scale=1.1) + + # draw the original bounding boxes + for (x, y, w, h) in rects: + cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2) + + # apply non-maxima suppression to the bounding boxes using a + # fairly large overlap threshold to try to maintain overlapping + # boxes that are still people + rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects]) + pick = non_max_suppression(rects, probs=None, overlapThresh=0.65) + + # draw the final bounding boxes + for (xA, yA, xB, yB) in pick: + cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2) + + # show some information on the number of bounding boxes + print("[INFO] {} original boxes, {} after suppression".format( + len(rects), len(pick))) + return image + + +def detect_images(hog, images_path): + # loop over the image paths + for image_path in paths.list_images(images_path): + # load the image and resize it to (1) reduce detection time + # and (2) improve detection accuracy + orig = cv2.imread(image_path) + image = detect_image(hog, orig) + + # show the output images + cv2.imshow("Before NMS", orig) + cv2.imshow("After NMS", image) + if cv2.waitKey(0) & 0xFF == ord('q'): + break + + +def detect_video(hog, video_path): + cap = cv2.VideoCapture(video_path) + while True: + ret, frame = cap.read() + if not ret: + break + detected = detect_image(hog, frame) + cv2.imshow("capture", detected) + if cv2.waitKey(100) & 0xFF == ord('q'): + break + cap.release() + cv2.destroyAllWindows() + + +if __name__ == '__main__': + # ap = argparse.ArgumentParser() + # ap.add_argument("-i", "--images", required=True, help="path to images directory") + # args = vars(ap.parse_args()) + # detect_images(args['images']) + + hog = cv2.HOGDescriptor() + hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) + + hog = hog_clf() + images_path = '../data/imgs/persons' + detect_images(hog, images_path) + + video_path = '../data/video/vtest.avi' + detect_video(hog, video_path) diff --git a/dip/segmentation/grab_cut.py b/dip/segmentation/grab_cut.py new file mode 100644 index 0000000..363496b --- /dev/null +++ b/dip/segmentation/grab_cut.py @@ -0,0 +1,41 @@ +import cv2 +import numpy as np + +src = cv2.imread("sample_person.jpg") +src = cv2.resize(src, (0, 0), fx=0.5, fy=0.5) + +# 交互式,返回 (x_min, y_min, w, h) +r = cv2.selectROI('input', src, True) + +# roi区域 +roi = src[int(r[1]):int(r[1] + r[3]), int(r[0]):int(r[0] + r[2])] + +# 原图mask,与原图等大小 +mask = np.zeros(src.shape[:2], dtype=np.uint8) + +# 矩形roi +rect = (int(r[0]), int(r[1]), int(r[2]), int(r[3])) # 包括前景的矩形,格式为(x,y,w,h) + +# bg模型的临时数组 +bgdmodel = np.zeros((1, 65), np.float64) +# fg模型的临时数组 +fgdmodel = np.zeros((1, 65), np.float64) + +cv2.grabCut(src, mask, rect, bgdmodel, fgdmodel, 11, mode=cv2.GC_INIT_WITH_RECT) + +print(np.unique(mask)) +# 提取前景和可能的前景区域 +mask2 = np.where((mask == 1) | (mask == 3), 255, 0).astype('uint8') + +print(mask2.shape) + +# 按位与 src & src == 0,得到的是二进制 +result = cv2.bitwise_and(src, src, mask=mask2) +# cv2.imwrite('result.jpg', result) +# cv2.imwrite('roi.jpg', roi) + +cv2.imshow('mask', mask2) +cv2.imshow('roi', roi) +cv2.imshow("result", result) +cv2.waitKey(0) +cv2.destroyAllWindows() diff --git a/dip/segmentation/image_fusion.py b/dip/segmentation/image_fusion.py new file mode 100644 index 0000000..4ef0762 --- /dev/null +++ b/dip/segmentation/image_fusion.py @@ -0,0 +1,53 @@ +import cv2 +import numpy as np + +src = cv2.imread("sample_person.jpg") +src = cv2.resize(src, (0, 0), fx=0.5, fy=0.5) +r = cv2.selectROI('input', src, False) # 返回 (x_min, y_min, w, h) + +# roi区域 +roi = src[int(r[1]):int(r[1] + r[3]), int(r[0]):int(r[0] + r[2])] +img = src.copy() +cv2.rectangle(img, (int(r[0]), int(r[1])), (int(r[0]) + int(r[2]), int(r[1]) + int(r[3])), (255, 0, 0), 2) + +# 原图mask +mask = np.zeros(src.shape[:2], dtype=np.uint8) +# 矩形roi +rect = (int(r[0]), int(r[1]), int(r[2]), int(r[3])) # 包括前景的矩形,格式为(x,y,w,h) + +background = cv2.imread("sample_giraffe.jpg") + +h, w, ch = src.shape +background = cv2.resize(background, (w, h)) +# cv.imwrite("background.jpg", background) + +# mask = np.zeros(src.shape[:2], dtype=np.uint8) +bgdmodel = np.zeros((1, 65), np.float64) +fgdmodel = np.zeros((1, 65), np.float64) + +cv2.grabCut(src, mask, rect, bgdmodel, fgdmodel, 5, mode=cv2.GC_INIT_WITH_RECT) +mask2 = np.where((mask == 1) | (mask == 3), 255, 0).astype('uint8') + +# 高斯模糊,边缘变得光滑 +se = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) +cv2.dilate(mask2, se, mask2) +mask2 = cv2.GaussianBlur(mask2, (5, 5), 0) +cv2.imshow('background-mask', mask2) +# cv.imwrite('background-mask.jpg', mask2) + +# 虚化背景 +# background = cv.GaussianBlur(background, (0, 0), 3) +mask2 = mask2 / 255.0 +print('mask2 shape', mask2.shape) +a = mask2[..., None] +print('a shape', a.shape) + +# 融合方法 com = a*fg + (1-a)*bg +result = a * (src.astype(np.float32)) + (1 - a) * (background.astype(np.float32)) +# result = cv2.bitwise_and(background.astype(np.uint8), background.astype(np.uint8), mask=mask2) + +cv2.imshow("result", result.astype(np.uint8)) +cv2.imwrite("result.jpg", result.astype(np.uint8)) + +cv2.waitKey(0) +cv2.destroyAllWindows() diff --git a/dip/segmentation/result.jpg b/dip/segmentation/result.jpg new file mode 100644 index 0000000..07dbf44 Binary files /dev/null and b/dip/segmentation/result.jpg differ diff --git a/dip/segmentation/sample_dog.jpg b/dip/segmentation/sample_dog.jpg new file mode 100644 index 0000000..77b0381 Binary files /dev/null and b/dip/segmentation/sample_dog.jpg differ diff --git a/dip/segmentation/sample_giraffe.jpg b/dip/segmentation/sample_giraffe.jpg new file mode 100644 index 0000000..a93e8b8 Binary files /dev/null and b/dip/segmentation/sample_giraffe.jpg differ diff --git a/dip/segmentation/sample_person.jpg b/dip/segmentation/sample_person.jpg new file mode 100644 index 0000000..61d377f Binary files /dev/null and b/dip/segmentation/sample_person.jpg differ diff --git a/dip/segmentation/sample_scream.jpg b/dip/segmentation/sample_scream.jpg new file mode 100644 index 0000000..43f2c36 Binary files /dev/null and b/dip/segmentation/sample_scream.jpg differ -- cgit v1.2.3