# coding: utf-8

# In[1]:

import cv2
import numpy as np
from save import result
from filtering import rgb2grayscale, filtering

img = cv2.imread("imori.jpg")
b = img[:, :, 0].copy()
g = img[:, :, 1].copy()
r = img[:, :, 2].copy()

gray_img = rgb2grayscale(r, g, b)

# In[2]:

# Prewitt filter
prewitt_v = np.array([[-1, -1, -1], [0, 0, 0], [1, 1, 1]])
prewitt_h = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]])

out_v = filtering(gray_img, prewitt_v, True)
out_h = filtering(gray_img, prewitt_h, True)

# In[3]:

result(out_v, "16_v")
result(out_h, "16_h")
img = cv2.imread("imori_1.jpg").astype(np.float32)
H, W, C = img.shape

# cropping
np.random.seed(1)
crop_size = 60
crop_num = 200


gt = np.array((47, 41, 129, 103), dtype=np.float32)

# label1:red, label0:blue, GT:green
img = cv2.rectangle(img, (gt[0], gt[1]), (gt[2],gt[3]), (0,255,0))
colors = [[255,0,0], [0,0,255]]
for i in range(crop_num):
    x1 = np.random.randint(W-60)
    y1 = np.random.randint(H-60)
    cropping = np.array((x1, y1, x1+crop_size, y1+crop_size), dtype=np.float32)
    label = 1 if IoU(cropping, gt) >= 0.5 else 0
    img = cv2.rectangle(img, (x1, y1), (x1+crop_size, y1+crop_size), colors[label])
    
out = img.astype(np.uint8)


# In[3]:


result(out, "94")

    dy = orig_y - iy

    out = (1 -
           dx) * (1 - dy) * img[iy, ix] + dx * (1 - dy) * img[iy, ix + 1] + (
               1 - dx) * dy * img[iy + 1, ix] + dx * dy * img[iy + 1, ix + 1]
    out[out > 255] = 255

    return out


pyramid = [gray_img.astype(np.uint8)]
for i in range(1, 6):
    a = 2**i
    img = bilinear(bilinear(gray_img, 1. / a), a)
    pyramid.append(img)

# In[8]:

diff = np.zeros_like(gray_img)
for i, j in ((0, 1), (0, 3), (0, 5), (1, 4), (2, 3), (3, 5)):
    diff += np.abs(pyramid[i] - pyramid[j])

# In[9]:

out = diff / np.max(diff) * 255
out = out.astype(np.uint8)

# In[10]:

result(out, "76")
# Closing processing
n_iter = 3
kernel = np.array([ [0, 1, 0], [1, 0, 1], [0, 1, 0] ], dtype=np.int)
out = binary_img.copy()

# morphology (Dilation)
for _ in range(n_iter):
    tmp = np.pad(out, (1,1), 'edge')
    for y in range(1, H+1):
        for x in range(1, W+1):            
            if (np.sum(kernel * tmp[y-1:y+2, x-1:x+2]) >= 255):
                out[y-1, x-1] = 255
                
# morphology (Erosion)
for _ in range(n_iter):
    tmp = np.pad(out, (1,1), 'edge')
    for y in range(1, H+1):
        for x in range(1, W+1):            
            if (np.sum(kernel * tmp[y-1:y+2, x-1:x+2]) < 255*4):
                out[y-1, x-1] = 0

out = (out - binary_img).astype(np.uint8)


# In[3]:


result(out, "53")

a = 1.5
new_H = int(a * H)
new_W = int(a * W)

y = np.arange(new_H).repeat(new_W).reshape(new_H, -1)
x = np.tile(np.arange(new_W), (new_H, 1))
y = y / a
x = x / a

ix = np.floor(x).astype(np.int)
iy = np.floor(y).astype(np.int)
# 画像の右端、下端の処理はこれで対応しているっぽい?
ix = np.minimum(ix, W - 2)
iy = np.minimum(iy, H - 2)

dx = x - ix
dy = y - iy
# 3次元チャンネル分に拡張
dx = np.repeat(np.expand_dims(dx, axis=-1), 3, axis=-1)
dy = np.repeat(np.expand_dims(dy, axis=-1), 3, axis=-1)

out = (1 - dx) * (1 - dy) * img[iy, ix] + dx * (1 - dy) * img[iy, ix + 1] + (
    1 - dx) * dy * img[iy + 1, ix] + dx * dy * img[iy + 1, ix + 1]

out[out > 255] = 255
out = out.astype(np.uint8)

# In[32]:

result(out, "26")
    if (Class != newClass).any():
        Class = newClass
        updated = True
    else:
        break

# In[33]:

out = np.zeros((H, W, C), dtype=np.float32)
out = Class[index].reshape(out.shape)
out = out.astype(np.uint8)

# In[34]:

result(out, "92")

# ## k-means (k=10)

# In[36]:

img = cv2.imread("imori.jpg").astype(np.float32)
H, W, C = img.shape
img = img.reshape(-1, C)

# k-means
K = 10
np.random.seed(0)
i = np.random.choice(np.arange(H * W), K, replace=False)
Class = img[i].copy()
index = np.zeros((H * W), dtype=np.int)
# coding: utf-8

# In[1]:

import cv2
import numpy as np
from save import result

# In[2]:

img = cv2.imread("imori_gamma.jpg").astype(np.float)

# Gamma correction
c = 1
g = 2.2

out = img.copy()
out /= 255
out = (1 / c * out)**(1 / g)
out *= 255
out = out.astype(np.uint8)

# In[3]:

result(out, "24")
# magnitude and angle of gradients
mag = np.sqrt(np.power(gx, 2) + np.power(gy, 2))
ang = np.arctan(gy / gx) + np.pi / 2

# In[11]:

mag = (mag / np.max(mag) * 255).astype(np.uint8)
# quantize the angle of gradients
q_ang = np.zeros_like(ang, dtype=np.int)
d = np.pi / 9
for i in range(9):
    q_ang[np.where((ang >= d * i) & (ang <= d * (i + 1)))] = i

# In[4]:

result(mag, "66_mag")

# In[12]:

out = np.zeros((H, W, C), dtype=np.uint8)
colors = [[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 0], [255, 0, 255],
          [0, 255, 255], [127, 127, 0], [127, 0, 127], [0, 127, 127]]

for i in range(9):
    out[q_ang == i] = colors[i]

# In[13]:

result(out, "66_ang")
B = detected.copy()
# Non-maximum suppression
R = []
th = 0.25
B.sort(key=lambda x: x[-1])
while len(B) > 0:
    new_B = []
    b0 = B.pop()
    R.append(b0)
    for i in range(len(B)):
        if IoU(b0[:4], B[i][:4]) > th:
            pass
        else:
            new_B.append(B[i])
    B = new_B.copy()

# In[56]:

for detection in R:
    img = cv2.rectangle(img, (detection[0], detection[1]),
                        (detection[2], detection[3]), (0, 0, 255))
    img = cv2.putText(img, "{:.2f}".format(detection[-1]),
                      (detection[0], detection[1] + 10),
                      cv2.FONT_HERSHEY_PLAIN, 0.8, (255, 0, 255))

# In[57]:

out = img.astype(np.uint8)
result(out, "99")
                edge_[y, x] = 0

# In[2]:

# Opening processing
n_iter = 1
kernel = np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]], dtype=np.int)
out = edge_.copy()

# morphology (Dilation)
for _ in range(n_iter):
    tmp = np.pad(out, (1, 1), 'edge')
    for y in range(1, H + 1):
        for x in range(1, W + 1):
            if (np.sum(kernel * tmp[y - 1:y + 2, x - 1:x + 2]) >= 255):
                out[y - 1, x - 1] = 255

# morphology (Erosion)
for _ in range(n_iter):
    tmp = np.pad(out, (1, 1), 'edge')
    for y in range(1, H + 1):
        for x in range(1, W + 1):
            if (np.sum(kernel * tmp[y - 1:y + 2, x - 1:x + 2]) < 255 * 4):
                out[y - 1, x - 1] = 0

out = out.astype(np.uint8)

# In[3]:

result(out, "50")
for i, detection in enumerate(R):
    for ans in GT:
        if IoU(ans, detection[:4]) > th:
            judge[i] = 1.

mAP = 0
count = 0
for i in range(len(judge)):
    if judge[i]:
        mAP += (np.sum(judge[ : i+1]) / (i+1))
        count += 1

mAP /= count


# In[23]:


print("Recall >>", recall, "({} / {})".format(recall_num, len(GT)))
print("Precision >>", precision, "({} / {})".format(precision_num, len(R)))
print("F-score >>", f_score)
print("mAP >>", mAP)


# In[24]:


out = img.astype(np.uint8)
result(out, "100")

K /= (sigma * np.sqrt(2 * np.pi))
K /= K.sum()

for y in range(H):
    for x in range(W):
        Ix2[y,x] = np.sum(Ix2_t[y:y+K_size, x:x+K_size] * K)
        Iy2[y,x] = np.sum(Iy2_t[y:y+K_size, x:x+K_size] * K)
        Ixy[y,x] = np.sum(Ixy_t[y:y+K_size, x:x+K_size] * K)
    


# In[4]:


k = 0.04
th = 0.1
R = (Ix2 * Iy2 - Ixy**2) - k * ((Ix2+Iy2)**2)

out = np.array((gray, gray, gray))
out = np.transpose(out, (1,2,0))
out[R >= np.max(R)*th] = [0, 0, 255]

out = out.astype(np.uint8)


# In[5]:


result(out, "83")

Exemple #13
0
# morphology gradient
n_iter = 1
kernel = np.array([ [0, 1, 0], [1, 0, 1], [0, 1, 0] ], dtype=np.int)
dilation = binary_img.copy()
erosion = binary_img.copy()

# dilation
for _ in range(n_iter):
    tmp = np.pad(dilation, (1,1), 'edge')
    for y in range(1, H+1):
        for x in range(1, W+1):            
            if (np.sum(kernel * tmp[y-1:y+2, x-1:x+2]) >= 255):
                dilation[y-1, x-1] = 255
                
# erosion
for _ in range(n_iter):
    tmp = np.pad(erosion, (1,1), 'edge')
    for y in range(1, H+1):
        for x in range(1, W+1):            
            if (np.sum(kernel * tmp[y-1:y+2, x-1:x+2]) < 255*4):
                erosion[y-1, x-1] = 0
                
out = (dilation - erosion).astype(np.uint8)


# In[8]:


result(out, "51")

Exemple #14
0
    orig_x = new_x / a
    orig_y = new_y / a
    
    ix = np.floor(orig_x).astype(np.int)
    iy = np.floor(orig_y).astype(np.int)
    ix = np.minimum(ix, W-2)
    iy = np.minimum(iy, H-2)
    
    dx = orig_x - ix
    dy = orig_y - iy
    
    out = (1-dx)*(1-dy)*img[iy,ix] + dx*(1-dy)*img[iy,ix+1] + (1-dx)*dy*img[iy+1,ix] + dx*dy*img[iy+1,ix+1]
    out[out > 255] = 255
    
    return out


# In[9]:


out = np.abs(bilinear(bilinear(gray_img, 0.5), 2) - gray_img)
out = out / np.max(out) * 255
out = out.astype(np.uint8)


# In[10]:


result(out, "74")

Exemple #15
0
# In[1]:

import cv2
import numpy as np
from save import result
from filtering import filtering, rgb2grayscale

img = cv2.imread("imori.jpg")

b = img[:, :, 0].copy()
g = img[:, :, 1].copy()
r = img[:, :, 2].copy()
gray_img = rgb2grayscale(r, g, b)

# In[5]:

# diferrential filter
## vertical
K_v = np.array([[0, -1, 0], [0, 1, 0], [0, 0, 0]])
## horizontal
K_h = np.array([[0, 0, 0], [-1, 1, 0], [0, 0, 0]])

out_v = filtering(gray_img, K_v, padding=True)
out_h = filtering(gray_img, K_h, padding=True)

# In[6]:

result(out_v, "14_v")
result(out_h, "14_h")
            else:
                edge_[y, x] = 0


# In[2]:


rmax = np.sqrt((H**2) + (W**2)).astype(np.int)
hough = np.zeros((rmax, 180), dtype=np.int)

idx = np.where(edge_ == 255)


# In[8]:


for y, x in zip(idx[0], idx[1]):
    for t in range(180):
        theta = np.pi * t / 180
        r = int(x * np.cos(theta) + y * np.sin(theta))
        hough[r, t] += 1

out = hough.astype(np.uint8)


# In[9]:


result(out, "44")

Exemple #17
0
            for u in range(T):
                for y in range(T):
                    for x in range(T):
                        F[yi + v, xi +
                          u] += gray_img[yi + y, xi + x] * weight(x, y, u, v)

# In[18]:

# IDCT
T = 8
K = 8
out = np.zeros((H, W), dtype=np.float32)

for yi in range(0, H, T):
    for xi in range(0, W, T):
        for y in range(T):
            for x in range(T):
                for v in range(K):
                    for u in range(K):
                        out[yi + y,
                            xi + x] += F[yi + v, xi + u] * weight(x, y, u, v)

# In[19]:

out[255 < out] = 255
out = out.astype(np.uint8)

# In[20]:

result(out, "36")
                    min_label = min(min_label, lookup_table[num])
            for num in label_nums:
                lookup_table[num] = min_label

# update labels based on look up table
new_label = 0
for label_tag in range(1, label_num + 1):
    flag = True
    for i in range(label_num + 1):
        if lookup_table[i] == label_tag:
            if flag:
                new_label += 1
                flag = False
            lookup_table[i] = new_label

# In[3]:

lookup_table

# In[4]:

label_colors = [[0, 0, 255], [0, 255, 0], [255, 0, 0], [0, 255, 255]]
out = np.zeros_like(img, dtype=np.uint8)

for i, lookup in enumerate(lookup_table[1:]):
    out[label == (i + 1)] = label_colors[lookup - 1]

# In[5]:

result(out, "58")
Exemple #19
0
    return w


# In[81]:

wxy_sum = np.zeros((aH, aW, C), dtype=np.float32)
out = np.zeros((aH, aW, C), dtype=np.float32)

for j in range(-1, 3):
    for i in range(-1, 3):
        idx_x = np.minimum(np.maximum((ix + i), 0), W - 1)
        idx_y = np.minimum(np.maximum((iy + j), 0), H - 1)

        wx = weight(dxs[i + 1])
        wy = weight(dys[j + 1])
        # expand to 3-dimension
        wx = np.repeat(np.expand_dims(wx, axis=-1), 3, axis=-1)
        wy = np.repeat(np.expand_dims(wy, axis=-1), 3, axis=-1)

        wxy_sum += wx * wy
        out += img[idx_y, idx_x] * wx * wy

out /= wxy_sum
out[out > 255] = 255
# out[out < 0] = 0
out = out.astype(np.uint8)

# In[82]:

result(out, "27")
Exemple #20
0
                # condition 5-1
                cond5_1 = True
                for neighbor in X:
                    if neighbor == -1:
                        cond5_1 = False
                        break
                # condition 5-2
                cond5_2 = False
                for i, _ in enumerate(X):
                    X_ = X.copy()
                    c = 0
                    X_[i] = 0
                    if connect8(X_) == 1:
                        c += 1
                if c == 8:
                    cond5_2 = True

                cond5 = cond5_1 or cond5_2

                if cond1 and cond2 and cond3 and cond4 and cond5:
                    flag = True
                    out[y, x] = -1

    out[np.where(out == -1)] = 0

out = out.astype(np.uint8) * 255

# In[7]:

result(out, "64")
Exemple #21
0
h, w, c = template.shape

# template matching by ZNCC
# Zero means
out = img.copy()
mi = np.mean(img, axis=-1, keepdims=True)
mt = np.mean(template, axis=-1, keepdims=True)
img = img - mi
template = template - mt

zncc = 0
idx_x = 0
idx_y = 0
for y in range(H - h):
    for x in range(W - w):
        zncc_tmp = np.sum(img[y:y + h, x:x + w] * template) / (np.sqrt(
            np.sum(np.power(img[y:y + h, x:x + w], 2)) *
            np.sqrt(np.sum(np.power(template, 2)))))
        if (zncc_tmp > zncc):
            zncc = zncc_tmp
            idx_x = x
            idx_y = y

out = cv2.rectangle(out, (idx_x, idx_y), (idx_x + w, idx_y + h), (0, 0, 255),
                    1)
out = out.astype(np.uint8)

# In[43]:

result(out, "57")
        x1 = tmp[y, min(x + 1, W - 1)]
        x2 = tmp[max(0, y - 1), min(x + 1, W - 1)]
        x3 = tmp[max(0, y - 1), x]
        x4 = tmp[max(0, y - 1), max(0, x - 1)]
        x5 = tmp[y, max(0, x - 1)]
        x6 = tmp[min(y + 1, H - 1), max(0, x - 1)]
        x7 = tmp[min(y + 1, H - 1), x]
        x8 = tmp[min(y + 1, H - 1), min(x + 1, W - 1)]

        s = 0
        s += (x1 - x1 * x2 * x3) + (x3 - x3 * x4 * x5) + (
            x5 - x5 * x6 * x7) + (x7 - x7 * x8 * x1)

        if s == 0:
            out[y, x] = [0, 0, 255]
        elif s == 1:
            out[y, x] = [0, 255, 0]
        elif s == 2:
            out[y, x] = [255, 0, 0]
        elif s == 3:
            out[y, x] = [0, 255, 255]
        elif s == 4:
            out[y, x] = [255, 0, 255]

out = out.astype(np.uint8)

# In[5]:

result(out, "61")
Exemple #23
0
            edge[y, x] = 0

# In[7]:

# ヒステリシス閾値処理
HT = 100
LT = 30

out = edge.copy()
out[out >= HT] = 255
out[out <= LT] = 0
check = np.array(np.where((LT < out) & (out < HT)))
for i in range(len(check.T)):
    y, x = check[0][i], check[1][i]
    for dy in range(-1, 2):
        for dx in range(-1, 2):
            X = max(min(0, x + dx), W - 1)
            Y = max(min(0, y + dy), H - 1)
            if (out[Y, X] >= HT):
                out[y, x] = 255
            else:
                out[y, x] = 0

# In[8]:

out = out.astype(np.uint8)

# In[9]:

result(out, "43")
Exemple #24
0
for y in range(cell_H):
    for x in range(cell_W):
        cx = x * N + N // 2
        cy = y * N + N // 2
        x1 = cx + N // 2 - 1
        y1 = cy
        x2 = cx - N // 2 + 1
        y2 = cy

        h = hist[y, x] / np.sum(hist[y, x])
        h /= h.max()

        for c in range(9):
            #angle = (20 * c + 10 - 90) / 180. * np.pi
            angle = (20 * c + 10) / 180. * np.pi
            rx = int(
                np.sin(angle) * (x1 - cx) + np.cos(angle) * (y1 - cy) + cx)
            ry = int(
                np.cos(angle) * (x1 - cx) - np.cos(angle) * (y1 - cy) + cy)
            lx = int(
                np.sin(angle) * (x2 - cx) + np.cos(angle) * (y2 - cy) + cx)
            ly = int(
                np.cos(angle) * (x2 - cx) - np.cos(angle) * (y2 - cy) + cy)

            c = int(255. * h[c])
            cv2.line(out, (lx, ly), (rx, ry), (c, c, c), thickness=1)

# In[4]:

result(out, "69")
detected = []

# sliding window
for y in range(0, H, 4):
    for x in range(0, W, 4):
        for rec in recs:
            dh, dw = int(rec[0] // 2), int(rec[1] // 2)
            x1 = max(0, x - dw)
            x2 = min(W, x + dw)
            y1 = max(0, y - dh)
            y2 = min(H, y + dh)
            focus_region = gray_img[y1:y2, x1:x2]
            focus_region = resize_bilinear(focus_region, resize_len,
                                           resize_len)
            focus_hog = hog(focus_region).ravel()

            proba = nn.forward(focus_hog).item()
            if proba >= 0.65:
                img = cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255))
                detected.append([x1, y1, x2, y2, proba])

# In[19]:

detected

# In[20]:

out = img.astype(np.uint8)
result(out, "98")
Exemple #26
0
H, W, C = img.shape

# In[2]:

# Affine transformation
a = 1
b = 0
c = 0
d = 1
tx = 30
ty = -30

x, y = np.meshgrid(np.arange(W), np.arange(H))

# In[3]:

x_new = a * x + b * y + tx
y_new = c * x + d * y + ty
x_new = np.minimum(np.maximum(x_new, 0), W - 1).astype(np.int)
y_new = np.minimum(np.maximum(y_new, 0), H - 1).astype(np.int)

# In[4]:

out = np.zeros_like(img, dtype=np.float32)
out[y_new, x_new] = img[y, x]
out = out.astype(np.uint8)

# In[5]:

result(out, "28")
Exemple #27
0
    BV = w0 * w1 * (m0 - m1) * (m0 - m1)
    if BV > max_BV:
        max_BV = BV
        max_t = t

threshold = max_t
binary_img = gray_img.copy()
binary_img[binary_img < threshold] = 0
binary_img[binary_img >= threshold] = 255

# In[2]:

# morphology (Dilation)
n_iter = 2
kernel = np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]])

out = binary_img.copy()

for _ in range(n_iter):
    tmp = np.pad(out, (1, 1), 'edge')
    for y in range(1, H + 1):
        for x in range(1, W + 1):
            if (np.sum(kernel * tmp[y - 1:y + 2, x - 1:x + 2]) >= 255):
                out[y - 1, x - 1] = 255

out = out.astype(np.uint8)

# In[3]:

result(out, "47")
H, W, C = img.shape

# In[4]:

# smoothing filter
F_size = 3

# zero padding
pad = F_size // 2
out = np.zeros((H + pad * 2, W + pad * 2, C), dtype=np.float)
out[pad:pad + H, pad:pad + W, :] = img.copy()

# In[5]:

# smoothing filter
out_tmp = out.copy()

for h in range(H):
    for w in range(W):
        for c in range(C):
            out[pad + h, pad + w, c] = np.mean(out[h:h + F_size, w:w + F_size,
                                                   c])

out = out[pad:pad + H, pad:pad + W].astype(np.uint8)

# In[6]:

result(out, "11")

# In[8]:
Exemple #29
0
# In[16]:

import cv2
import numpy as np
from save import result
from filtering import rgb2grayscale

img = cv2.imread("imori.jpg").astype(np.float)
H, W, C = img.shape

b = img[:, :, 0].copy()
g = img[:, :, 1].copy()
r = img[:, :, 2].copy()

gray_img = rgb2grayscale(r, g, b).reshape(128, -1)
result(gray_img.astype(np.uint8), "38_grayscale")

# In[10]:

# Discrete cosine transformation
T = 8
K = 8
F = np.zeros_like(gray_img, dtype=np.float32)


def weight(x, y, u, v):
    if u == 0:
        cu = 1 / np.sqrt(2)
    else:
        cu = 1
    if v == 0:
Exemple #30
0
# translate [vmin, vmax] to [0, 255]
a = 0
b = 255


# In[11]:


img[img < vmin] = a
img[vmax < img] = b
img = (b - a) / (vmax - vmin) * (img - vmin) +a
img = img.astype(np.uint8)


# In[12]:


plt.figure(figsize=(8,5))
plt.hist(img.ravel(), bins=255, range=(0, 255), rwidth=0.8)
plt.title("Histogram of transformed imori_dark.jpg", fontsize=14)
plt.savefig("21_hist.png")
plt.show()


# In[13]:


result(img, "21")