def HOG_draw(gr, img): gray = lib0110.BGR2GRAY(img) add = np.zeros_like(gray) C, height, width = gr.shape for y in range(height): for x in range(width): mat = np.zeros((8,8)) for c in range(C): score = gr[c,y,x] * 10 angle = np.pi * (20*c+10) /180 print(angle) for xx in range(-4,4): yy = xx * np.tan(angle) yy = int(np.round(yy)) if yy>=-4 and yy<4: mat[yy+4, xx+4] = max(score, mat[yy+4, xx+4]) for yy in range(-4,4): a = np.tan(angle) if a==0: a = 1e-5 xx = yy / a xx = int(np.round(xx)) if xx>=-4 and xx<4: mat[yy+4, xx+4] = max(score, mat[yy+4, xx+4]) add[y*8:(y+1)*8, x*8:(x+1)*8] = mat return add
def Harris_corner_detection2(img, ix2g, iy2g, ixyg, k, threshold): gray = lib0110.BGR2GRAY(img) height, width = gray.shape out = gray.copy().ravel() out = out.repeat(3).reshape(height, width, 3) R = (ix2g * iy2g - ixyg * ixyg) - k * ((ix2g + iy2g)**2) out[R >= np.max(R) * threshold] = (0, 0, 255) return out
def Gabor_filtering(img, K, s, g, l, p, A): gray = lib0110.BGR2GRAY(img) filter = make_Gabor_filter(K,s,g,l,p,A) hK = K//2 out = np.zeros_like(gray) height, width = gray.shape pad = np.pad(gray, [(hK,hK), (K-hK,K-hK)], 'edge') for y in range(height): for x in range(width): out[y,x] = np.sum(pad[y:y+K, x:x+K] * filter) return out
def Saliency_map(img): gray = lib0110.BGR2GRAY(img) pyramids = [] pyramids.append(gray.clip(0,255).astype(np.uint8)) for i in range(1,6): out2 = lib2130.Bi_linear_interpolation(gray, 1./(2**i), 1./(2**i)) out3 = lib2130.Bi_linear_interpolation(out2, (2**i), (2**i)) pyramids.append(out3) dif = np.zeros_like(gray) for i in range(5): for j in range(i,6): dif += np.abs(pyramids[j] - pyramids[i]) return lib2130.Histogram_normalization(dif, 0, 255)
def Canny_edge_strength(img, k, sigma): img = img.astype(np.float64) img1 = lib0110.BGR2GRAY(img) img2 = lib0110.Gaussian_filter(img1, k=k, sigma=sigma, padding_type='edge') img3v, img3h = lib1120.Sobel_filter(img2, padding_type='edge') edge = np.sqrt(img3h * img3h + img3v * img3v) img3h[img3h == 0] = 1e-5 tan = np.arctan(img3v / img3h) angle = np.zeros_like(tan) angle[np.where((-0.4142 < tan) & (tan <= 0.4142))] = 0 angle[np.where((0.4142 < tan) & (tan < 2.4142))] = 45 angle[np.where((tan <= -2.4142) | (2.4142 <= tan))] = 90 angle[np.where((-2.4142 < tan) & (tan <= -0.4142))] = 135 return edge, angle
def Harris_corner_detection1(img, Gk, Gsigma): gray = lib0110.BGR2GRAY(img) height, width = gray.shape iy, ix = lib1120.Sobel_filter(gray, padding_type='edge') ix2g = lib0110.Gaussian_filter(ix * ix, k=Gk, sigma=Gsigma, padding_type='edge') iy2g = lib0110.Gaussian_filter(iy * iy, k=Gk, sigma=Gsigma, padding_type='edge') ixyg = lib0110.Gaussian_filter(ix * iy, k=Gk, sigma=Gsigma, padding_type='edge') return ix2g, iy2g, ixyg
def Hessian_corner_detection(img, threshold): gray = lib0110.BGR2GRAY(img) height, width = gray.shape iy, ix = lib1120.Sobel_filter(gray, padding_type='edge') iyy, ixy = lib1120.Sobel_filter(iy, padding_type='edge') _, ixx = lib1120.Sobel_filter(ix, padding_type='edge') detH = iyy * ixx - ixy * ixy maxdH = np.max(detH) out = gray.copy().ravel() out = out.repeat(3).reshape(height, width, 3) for y in range(height): for x in range(width): neighbor = detH[max(0, y - 1):min(y + 2, height), max(0, x - 1):min(x + 2, width)] if np.max(neighbor) == detH[y, x] and detH[y, x] >= threshold * maxdH: out[y, x] = (0, 0, 255) return out
def HOG1_gradient(img): gray = lib0110.BGR2GRAY(img).astype(np.float32) height, width = gray.shape pad = np.pad(gray, [(1,1),(1,1)], 'edge') gx = pad[1:-1, 2:] - pad[1:-1, :-2] gy = pad[2:, 1:-1] - pad[:-2, 1:-1] mag = np.sqrt(gx*gx + gy*gy) gx[gx==0] = 1e-6 ang = np.arctan(gy / gx) ang[ang < 0] = np.pi + ang[ang < 0] a2 = np.zeros_like(ang) for i in range(9): index = np.where((ang > (np.pi/9)*i) & (ang <=(np.pi/9)*(i+1))) a2[index] = i return mag, a2
import cv2 import numpy as np import lib5160 import matplotlib.pyplot as plt import sys,os sys.path.append(os.getcwd()) from q01_10 import lib0110 from q41_50 import lib4150 if __name__ == '__main__': img = cv2.imread("Gasyori100knock/Question_51_60/imori.jpg") img2 = lib0110.OTSU_binarization(lib0110.BGR2GRAY(img)) out = lib5160.Morphology_gradient(img2).clip(0,255).astype(np.uint8) cv2.imshow("imori", out) cv2.waitKey(0) cv2.imwrite("q51_60/051.jpg", out) cv2.destroyAllWindows()
import cv2 import numpy as np import lib1120 import sys,os sys.path.append(os.getcwd()) from q01_10 import lib0110 if __name__ == '__main__': img = cv2.imread("Gasyori100knock/Question_11_20/imori.jpg") gray_img = lib0110.BGR2GRAY(img) ansv, ansh = lib1120.Sobel_filter(gray_img) ansv = ansv.clip(0,255).astype(np.uint8) ansh = ansh.clip(0,255).astype(np.uint8) cv2.imshow("imori", ansv) cv2.waitKey(0) cv2.imwrite("q11_20/015v.jpg", ansv) cv2.imshow("imori", ansh) cv2.waitKey(0) cv2.imwrite("q11_20/015h.jpg", ansh) cv2.destroyAllWindows()
import cv2 import numpy as np import lib7180 import matplotlib.pyplot as plt import sys,os sys.path.append(os.getcwd()) from q21_30 import lib2130 from q01_10 import lib0110 from q61_70 import lib6170 if __name__ == '__main__': img = cv2.imread("Gasyori100knock/Question_71_80/imori.jpg") gray = lib0110.BGR2GRAY(img) pyramids = [] pyramids.append(gray.clip(0,255).astype(np.uint8)) for i in range(1,6): out2 = lib2130.Bi_linear_interpolation(gray, 1./(2**i), 1./(2**i)).clip(0,255).astype(np.uint8) pyramids.append(out2) for i in range(6): cv2.imshow("imori", pyramids[i]) cv2.waitKey(0) cv2.imwrite("q71_80/075_"+str(i)+".jpg", pyramids[i]) cv2.destroyAllWindows()
import cv2 import numpy as np import lib6170 import matplotlib.pyplot as plt import sys, os sys.path.append(os.getcwd()) from q01_10 import lib0110 from q51_60 import lib5160 if __name__ == '__main__': img = cv2.imread("Gasyori100knock/Question_61_70/imori.jpg") out = lib6170.HOG(img).clip(0, 255).astype(np.uint8) out = lib5160.Alpha_blend(lib0110.BGR2GRAY(img), out, 0.2) cv2.imshow("imori", out) cv2.waitKey(0) cv2.imwrite("q61_70/069.jpg", out) cv2.destroyAllWindows()
def Pyramid_difference(img, a): gray = lib0110.BGR2GRAY(img) zoi = zoomout_in(img, a) out = np.abs(zoi - gray) return lib2130.Histogram_normalization(out, 0, 255)
def zoomout_in(img, a): gray = lib0110.BGR2GRAY(img) zoomout = lib2130.Bi_linear_interpolation(gray, 1./a, 1./a) zoomin = lib2130.Bi_linear_interpolation(zoomout, a, a) return zoomin