diff options
36 files changed, 195 insertions, 0 deletions
diff --git a/dip/data/imgs/persons/person_010.bmp b/dip/data/imgs/persons/person_010.bmp Binary files differnew file mode 100644 index 0000000..4597574 --- /dev/null +++ b/dip/data/imgs/persons/person_010.bmp diff --git a/dip/data/imgs/persons/person_011.bmp b/dip/data/imgs/persons/person_011.bmp Binary files differnew file mode 100644 index 0000000..65bdc52 --- /dev/null +++ b/dip/data/imgs/persons/person_011.bmp diff --git a/dip/data/imgs/persons/person_014.bmp b/dip/data/imgs/persons/person_014.bmp Binary files differnew file mode 100644 index 0000000..f6df899 --- /dev/null +++ b/dip/data/imgs/persons/person_014.bmp diff --git a/dip/data/imgs/persons/person_029.bmp b/dip/data/imgs/persons/person_029.bmp Binary files differnew file mode 100644 index 0000000..a9e3cf8 --- /dev/null +++ b/dip/data/imgs/persons/person_029.bmp diff --git a/dip/data/imgs/persons/person_032.bmp b/dip/data/imgs/persons/person_032.bmp Binary files differnew file mode 100644 index 0000000..f7be1f5 --- /dev/null +++ b/dip/data/imgs/persons/person_032.bmp diff --git a/dip/data/imgs/persons/person_033.bmp b/dip/data/imgs/persons/person_033.bmp Binary files differnew file mode 100644 index 0000000..a1096fa --- /dev/null +++ b/dip/data/imgs/persons/person_033.bmp diff --git a/dip/data/imgs/persons/person_044.bmp b/dip/data/imgs/persons/person_044.bmp Binary files differnew file mode 100644 index 0000000..5846743 --- /dev/null +++ b/dip/data/imgs/persons/person_044.bmp diff --git a/dip/data/imgs/persons/person_063.bmp b/dip/data/imgs/persons/person_063.bmp Binary files differnew file mode 100644 index 0000000..6262c11 --- /dev/null +++ b/dip/data/imgs/persons/person_063.bmp diff --git a/dip/data/imgs/persons/person_085.bmp b/dip/data/imgs/persons/person_085.bmp Binary files differnew file mode 100644 index 0000000..ff5e8b5 --- /dev/null +++ b/dip/data/imgs/persons/person_085.bmp diff --git a/dip/data/imgs/persons/person_086.bmp b/dip/data/imgs/persons/person_086.bmp Binary files differnew file mode 100644 index 0000000..3170a6e --- /dev/null +++ b/dip/data/imgs/persons/person_086.bmp diff --git a/dip/data/imgs/persons/person_090.bmp b/dip/data/imgs/persons/person_090.bmp Binary files differnew file mode 100644 index 0000000..316190f --- /dev/null +++ b/dip/data/imgs/persons/person_090.bmp diff --git a/dip/data/imgs/persons/person_095.bmp b/dip/data/imgs/persons/person_095.bmp Binary files differnew file mode 100644 index 0000000..90676e5 --- /dev/null +++ b/dip/data/imgs/persons/person_095.bmp diff --git a/dip/data/imgs/persons/person_104.bmp b/dip/data/imgs/persons/person_104.bmp Binary files differnew file mode 100644 index 0000000..069b83d --- /dev/null +++ b/dip/data/imgs/persons/person_104.bmp diff --git a/dip/data/imgs/persons/person_120.bmp b/dip/data/imgs/persons/person_120.bmp Binary files differnew file mode 100644 index 0000000..0eb1272 --- /dev/null +++ b/dip/data/imgs/persons/person_120.bmp diff --git a/dip/data/imgs/persons/person_127.bmp b/dip/data/imgs/persons/person_127.bmp Binary files differnew file mode 100644 index 0000000..f9c4854 --- /dev/null +++ b/dip/data/imgs/persons/person_127.bmp diff --git a/dip/data/imgs/persons/person_133.bmp b/dip/data/imgs/persons/person_133.bmp Binary files differnew file mode 100644 index 0000000..9cf9086 --- /dev/null +++ b/dip/data/imgs/persons/person_133.bmp diff --git a/dip/data/imgs/persons/person_136.bmp b/dip/data/imgs/persons/person_136.bmp Binary files differnew file mode 100644 index 0000000..e3582ad --- /dev/null +++ b/dip/data/imgs/persons/person_136.bmp diff --git a/dip/data/imgs/persons/person_147.bmp b/dip/data/imgs/persons/person_147.bmp Binary files differnew file mode 100644 index 0000000..66732b7 --- /dev/null +++ b/dip/data/imgs/persons/person_147.bmp diff --git a/dip/data/imgs/persons/person_156.bmp b/dip/data/imgs/persons/person_156.bmp Binary files differnew file mode 100644 index 0000000..dfa88b5 --- /dev/null +++ b/dip/data/imgs/persons/person_156.bmp diff --git a/dip/data/imgs/persons/person_175.bmp b/dip/data/imgs/persons/person_175.bmp Binary files differnew file mode 100644 index 0000000..b7b8e8b --- /dev/null +++ b/dip/data/imgs/persons/person_175.bmp diff --git a/dip/data/imgs/persons/person_220.bmp b/dip/data/imgs/persons/person_220.bmp Binary files differnew file mode 100644 index 0000000..d716bd2 --- /dev/null +++ b/dip/data/imgs/persons/person_220.bmp diff --git a/dip/data/imgs/persons/person_222.bmp b/dip/data/imgs/persons/person_222.bmp Binary files differnew file mode 100644 index 0000000..40d4cef --- /dev/null +++ b/dip/data/imgs/persons/person_222.bmp diff --git a/dip/data/imgs/persons/person_251.bmp b/dip/data/imgs/persons/person_251.bmp Binary files differnew file mode 100644 index 0000000..0d46c09 --- /dev/null +++ b/dip/data/imgs/persons/person_251.bmp diff --git a/dip/data/imgs/persons/person_258.bmp b/dip/data/imgs/persons/person_258.bmp Binary files differnew file mode 100644 index 0000000..8b23988 --- /dev/null +++ b/dip/data/imgs/persons/person_258.bmp diff --git a/dip/data/imgs/persons/person_265.bmp b/dip/data/imgs/persons/person_265.bmp Binary files differnew file mode 100644 index 0000000..f322ae0 --- /dev/null +++ b/dip/data/imgs/persons/person_265.bmp diff --git a/dip/data/imgs/persons/person_293.bmp b/dip/data/imgs/persons/person_293.bmp Binary files differnew file mode 100644 index 0000000..4f71d76 --- /dev/null +++ b/dip/data/imgs/persons/person_293.bmp diff --git a/dip/data/imgs/persons/person_390.bmp b/dip/data/imgs/persons/person_390.bmp Binary files differnew file mode 100644 index 0000000..f7e139c --- /dev/null +++ b/dip/data/imgs/persons/person_390.bmp diff --git a/dip/data/imgs/persons/person_454.bmp b/dip/data/imgs/persons/person_454.bmp Binary files differnew file mode 100644 index 0000000..2396327 --- /dev/null +++ b/dip/data/imgs/persons/person_454.bmp 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 Binary files differnew file mode 100644 index 0000000..07dbf44 --- /dev/null +++ b/dip/segmentation/result.jpg diff --git a/dip/segmentation/sample_dog.jpg b/dip/segmentation/sample_dog.jpg Binary files differnew file mode 100644 index 0000000..77b0381 --- /dev/null +++ b/dip/segmentation/sample_dog.jpg diff --git a/dip/segmentation/sample_giraffe.jpg b/dip/segmentation/sample_giraffe.jpg Binary files differnew file mode 100644 index 0000000..a93e8b8 --- /dev/null +++ b/dip/segmentation/sample_giraffe.jpg diff --git a/dip/segmentation/sample_person.jpg b/dip/segmentation/sample_person.jpg Binary files differnew file mode 100644 index 0000000..61d377f --- /dev/null +++ b/dip/segmentation/sample_person.jpg diff --git a/dip/segmentation/sample_scream.jpg b/dip/segmentation/sample_scream.jpg Binary files differnew file mode 100644 index 0000000..43f2c36 --- /dev/null +++ b/dip/segmentation/sample_scream.jpg |
