Exemple #1
0
# gray
gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)
# binary threshold
ret, sep_tresh = cv2.threshold(gray, 160, 255, cv2.THRESH_BINARY_INV)
# noise removal
kernel = np.ones((3, 3), np.uint8)
opening = cv2.morphologyEx(sep_tresh, cv2.MORPH_OPEN, kernel, iterations=2)
bg = cv2.dilate(opening, kernel, iterations=3)
# distance transform
dist_trans = cv2.distanceTransform(opening, cv2.DIST_L2, 5)
ret, fg = cv2.threshold(dist_trans, 0.7 * dist_trans.max(), 255, 0)

fg = np.uint8(fg)
missing = cv2.subtract(bg, fg)

ret, markers = cv2.connectedComponents(fg)

markers = markers + 1

markers[missing == 255] = 0

markers = cv2.watershed(img, markers)

image, cntrs, hierarchy = cv2.findContours(markers.copy(), cv2.RETR_CCOMP,
                                           cv2.CHAIN_APPROX_SIMPLE)

for i in range(len(cntrs)):
    if hierarchy[0][i][3] == -1: cv2.drawContours(img, cntrs, i, 255, 10)

helpers.implot(img)
#   if match1.distance < 0.75*match2.distance: good.append([match1])

# sf_matches = cv2.drawMatchesKnn(reeses, kp1, cereals, kp2, good, None, flags=2)

# helpers.implot(sf_matches)

# FLANN based, not best but good looking matches
sift = cv2.xfeatures2d.SIFT_create()
kp1, des1 = sift.detectAndCompute(reeses, None)
kp2, des2 = sift.detectAndCompute(cereals, None)

FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
search_params = dict(checks=50)

flann = cv2.FlannBasedMatcher(index_params, search_params)
matches = flann.knnMatch(des1, des2, 2)
mask = [[0, 0] for i in range(len(matches))]

for i, (match1, match2) in enumerate(matches):
    if match1.distance < 0.7 * match2.distance: mask[i] = [1, 0]

draw_params = dict(matchColor=(0, 255, 0),
                   singlePointColor=(255, 0, 0),
                   matchesMask=mask,
                   flags=0)

fl_matches = cv2.drawMatchesKnn(reeses, kp1, cereals, kp2, matches, None,
                                **draw_params)
helpers.implot(fl_matches)
Exemple #3
0
import numpy as np
import cv2
import matplotlib.pyplot as plt
import helpers

img = cv2.imread('resources/internal_external.png', 0)

helpers.implot(img)

image, cntrs, hierarchy = cv2.findContours(img, cv2.RETR_CCOMP,
                                           cv2.CHAIN_APPROX_SIMPLE)

ext_cons = np.zeros(image.shape)

for i in range(len(cntrs)):
    if (hierarchy[0][i][3]) == -1:
        cv2.drawContours(ext_cons, cntrs, i, 255, -1)

helpers.implot(ext_cons)

int_cons = np.zeros(image.shape)

for i in range(len(cntrs)):
    if (hierarchy[0][i][3]) != -1:
        cv2.drawContours(int_cons, cntrs, i, 255, -1)

helpers.implot(int_cons)
Exemple #4
0
    for (x, y, w, h) in face_recs:
        cv2.rectangle(face, (x, y), (x + w, y + h), (255), 1)

    return face


def detect_eyes(img):
    face = img.copy()

    eye_recs = eyes_cas.detectMultiScale(face)

    for (x, y, w, h) in eye_recs:
        cv2.rectangle(face, (x, y), (x + w, y + h), (255), 1)

    return face


cap = cv2.VideoCapture('data/video_capture.mp4')

while 1:
    ret, frame = cap.read(0)
    frame = adj_detect_face(frame)
    helpers.implot(frame)

    k = cv2.waitKey(1)
    if k == 27: break
cap.release()
cv2.destroyAllWindows()

# helpers.implot(detect_eqqqqqqqqqyes(nadia))
Exemple #5
0
import numpy as np
import cv2
import matplotlib.pyplot as plt
import helpers as hlp

img = hlp.imread('data/sammy_face.jpg')

med_val = np.median(img)

blur = cv2.blur(img, (4, 3))

edges = cv2.Canny(blur, 127, 255)

hlp.implot(edges)
Exemple #6
0
import numpy as np
import cv2
import matplotlib.pyplot as plt
import helpers as hlp

chess = hlp.imread('resources/flat_chessboard.png')

found, corners = cv2.findChessboardCorners(chess, (7, 7))

cv2.drawChessboardCorners(chess, (7, 7), corners, found)

hlp.implot(chess)
Exemple #7
0
import cv2
import matplotlib.pyplot as plt
from matplotlib import cm
import helpers

img = helpers.imread('data/car_plate.jpg')

plate_cas = cv2.CascadeClassifier(
    'data/haarcascades/haarcascade_russian_plate_number.xml')


def detect_plate(img):
    img = img.copy()

    plate_recs = plate_cas.detectMultiScale(img,
                                            scaleFactor=1.2,
                                            minNeighbors=5)
    for (x, y, w, h) in plate_recs:
        cv2.rectangle(img, (x, y), (x + w, y + h), (255), 1)
        # separate plate to blur
        plate_rec = img[y:y + h, x:x + w]
        # blur plate
        plate_rec = cv2.medianBlur(plate_rec, 9)
        # replace plate with blurred plate
        img[y:y + h, x:x + w] = plate_rec

    return img


helpers.implot(detect_plate(img))
flat_chess = hlp.imread('data/flat_chessboard.png')
flat_gray = cv2.cvtColor(flat_chess, cv2.COLOR_BGR2GRAY)

# hlp.implot(flat_gray, cmap='gray')

real_chess = hlp.imread('data/real_chessboard.jpg')
real_gray = cv2.cvtColor(real_chess, cv2.COLOR_BGR2GRAY)

# hlp.implot(real_gray, cmap='gray')

gray = np.float32(flat_gray)
dst = cv2.cornerHarris(gray, 2, 3, 0.04)
dst = cv2.dilate(dst, None)

flat_chess[dst > 0.01 * dst.max()] = [255, 0, 0]
hlp.implot(flat_chess)

gray = np.float32(real_gray)
dst = cv2.cornerHarris(gray, 2, 3, 0.04)
dst = cv2.dilate(dst, None)

real_chess[dst > 0.01 * dst.max()] = [255, 0, 0]
hlp.implot(real_chess)

# shi-tomasi
corners = cv2.goodFeaturesToTrack(image=flat_gray,
                                  maxCorners=5,
                                  qualityLevel=0.01,
                                  minDistance=10)
corners = np.int0(corners)