Example #1
0
def main():
    # 読み込み&初期画像表示
    img = cv2.imread("./assets/imori.jpg")
    showImg(img, QUESTION_ID)

    # no openCV
    ans1 = x_y_sharing(img, dx=30)
    showImg(ans1, QUESTION_ID + "_noOpenCV_" + QUESTION_NAME)
    ans1 = x_y_sharing(img, dy=30)
    showImg(ans1, QUESTION_ID + "_noOpenCV_" + QUESTION_NAME)
    ans1 = x_y_sharing(img, dx=30, dy=30)
    showImg(ans1, QUESTION_ID + "_noOpenCV_" + QUESTION_NAME)

    # openCV
    h, w, d = img.shape
    after_height, after_width = getAfterHeightAndWidth(h, w, theta=30)
    img_r = np.insert(img, [0] * (after_width - w // 2 + 1), 0, axis=1)
    img_r = np.insert(img_r, [0] * (after_height - h // 2 + 1), 0, axis=0)
    img_r = np.insert(img_r, [img_r.shape[1]] * (after_width - w // 2 + 1),
                      0,
                      axis=1)
    img_r = np.insert(img_r, [img_r.shape[0]] * (after_height - h // 2 + 1),
                      0,
                      axis=0)

    M = cv2.getRotationMatrix2D((img_r.shape[1] // 2, img_r.shape[0] // 2), 30,
                                1)
    ans2 = cv2.warpAffine(img_r, M, (img_r.shape[1], img_r.shape[0]))
    showImg(ans2, QUESTION_ID + "_OpenCV_" + QUESTION_NAME)
Example #2
0
def main():
    # 読み込み&初期画像表示
    img = cv2.imread("./assets/imori.jpg")
    showImg(img, "Q6")

    # 平均プーリング no openCV
    tileSize = 8
    ans = mean_pooling(img, tileSize)

    showImg(ans, "Q7_no_openCV")
Example #3
0
def main():
    # 読み込み&初期画像表示
    img = cv2.imread("./assets/imori_dark.jpg")
    showImg(img, QUESTION_ID)
    
    grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    # ヒストグラム平坦化 no openCV
    ans = histgramFlatten(img, m = 128, sigma = 52)

    showImg(ans, QUESTION_ID + "_" + QUESTION_NAME)
Example #4
0
def main():
    # 読み込み&初期画像表示
    img = cv2.imread("./assets/imori_dark.jpg")
    showImg(img, QUESTION_ID)

    grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # ヒストグラム作成 no openCV
    ans = histgramTransform(grayImg, 0, 255, grayImg.min(), grayImg.max())
    # ans = histgramTransform(grayImg, 0, 255, 30, 220)
    showImg(ans, QUESTION_ID + "_" + QUESTION_NAME)
Example #5
0
def main():
    # 読み込み&初期画像表示
    img = cv2.imread("./assets/imori_dark.jpg")
    showImg(img, QUESTION_ID)

    grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # ヒストグラム作成 no openCV
    bins = np.arange(256)
    plt.hist(grayImg.flatten(), bins=bins)
    plt.title(QUESTION_ID + " " + QUESTION_NAME)
    plt.xlim(0, 255)
    plt.show()
Example #6
0
def main():
    # 読み込み&初期画像表示
    img = cv2.imread("./assets/imori.jpg")
    showImg(img, "Q12")

    grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # 平滑化フィルタ処理 no openCV
    ans = sobelFilter(grayImg, "V")
    showImg(ans, "Q15_no_openCV_vertical")

    ans = sobelFilter(grayImg, "H")
    showImg(ans, "Q15_no_openCV_horizontal")

    # openCVでの平滑化処理 padding処理は zero padding
    ans = cv2.Sobel(grayImg, -1, 0, 1, ksize=3)  #vertical
    showImg(np.clip(ans, 0, 255), "Q15_openCV_vertical")

    ans = cv2.Sobel(grayImg, -1, 1, 0, ksize=3)  #horizontal
    showImg(np.clip(ans, 0, 255), "Q15_openCV_horizontal")
Example #7
0
def main():
    # 読み込み&初期画像表示
    img = cv2.imread("./assets/imori.jpg")
    showImg(img, QUESTION_ID)

    grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # 平滑化フィルタ処理 no openCV
    ans = prewittFilter(grayImg, "V")
    showImg(ans, QUESTION_ID + "no_openCV" + "vertical")

    ans = prewittFilter(grayImg, "H")
    showImg(ans, QUESTION_ID + "no_openCV" + "horizontal")

    # openCVでの平滑化処理

    ans = cv2.filter2D(grayImg, -1, kernel=getprewittFilter("V"))  #vertical
    showImg(np.clip(ans, 0, 255), QUESTION_ID + "openCV_vertical")

    ans = cv2.filter2D(grayImg, -1, kernel=getprewittFilter("H"))  #horizontal
    showImg(np.clip(ans, 0, 255), QUESTION_ID + "openCV_horizontal")
Example #8
0
def main():
    # 読み込み&初期画像表示
    img = cv2.imread("./assets/imori.jpg")
    showImg(img, "Q14")

    grayImg = bgr2gray(img)
    # 微分フィルタ処理 no openCV
    ans = diffFilter(grayImg, "V")
    showImg(ans, "Q14_no_openCV vertical")

    ans = diffFilter(grayImg, "H")
    showImg(ans, "Q14_no_openCV horizontal")

    # 微分フィルタ処理 openCV
    kernel = {
        "V": np.array([[0, -1, 0], [0, 1, 0], [0, 0, 0]]),
        "H": np.array([[0, 0, 0], [-1, 1, 0], [0, 0, 0]]),
    }
    
    ans = cv2.filter2D(grayImg, -1, kernel["V"])
    showImg(np.clip(ans, 0, 255), "Q14_opencv_vertical")
    ans = cv2.filter2D(grayImg, -1, kernel["H"])
    showImg(np.clip(ans, 0, 255), "Q14_opencv_vertical")
Example #9
0
def main():
    # 読み込み&初期画像表示
    img = cv2.imread("./assets/imori_noise.jpg")
    showImg(img, QUESTION_ID)

    grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    showImg(grayImg, QUESTION_ID)
    s = 3
    kernelSize = 3

    # 平滑化フィルタ処理 no openCV
    ans = logFilter(grayImg, s=s, kernelSize=kernelSize)
    showImg(ans, QUESTION_ID + "no_openCV" + QUESTION_NAME)

    # openCVでの平滑化処理
    ans = cv2.filter2D(grayImg, -1, kernel=getLoGFilter(s, kernelSize))
    showImg(np.clip(ans, 0, 255), QUESTION_ID + "openCV" + QUESTION_NAME)
Example #10
0
def main():
    # 読み込み&初期画像表示
    img = cv2.imread("./assets/imori.jpg")
    showImg(img, "Q11")
    tileSize = 3

    # 平滑化フィルタ処理 no openCV
    ans = smoothingBlur(img, tileSize)
    showImg(ans, "Q11_no_openCV")

    # openCVでの平滑化処理 padding処理は zero padding
    ans = cv2.blur(img, (tileSize, tileSize), borderType=cv2.BORDER_CONSTANT)

    showImg(ans, "Q11_openCV")
Example #11
0
def main():
    # 読み込み&初期画像表示
    img = cv2.imread("./assets/imori_gamma.jpg")
    showImg(img, QUESTION_ID)

    c = 1
    gamma = 2.2
    # ガンマ補正 no openCV
    ans = gammaCorrection(img, c, gamma)
    showImg(ans, QUESTION_ID + "_noOpenCV_" + QUESTION_NAME)

    # ガンマ補正 openCV
    LUT = np.array([255 * (1 / c * x / 255)**(1 / gamma) for x in range(256)])
    ans = cv2.LUT(img, LUT)
    showImg(ans, QUESTION_ID + "_OpenCV_" + QUESTION_NAME)
Example #12
0
def main():
    # 読み込み&初期画像表示
    img = cv2.imread("./assets/imori.jpg")
    showImg(img, "Q6")

    # 減色処理 no openCV
    ans = ((img - 1) // 64) * 64 + 32
    showImg(ans, "Q6_numpy")

    # 減色処理 no openCV
    # kmeans法による減色ができる。
    # 問題文と異なる減色方法にはなるが、サンプルとして処理をためす
    k = 12
    ans = genSubImg(img, k)
    showImg(ans, "Q6_OpenCV, k = " + str(k))
Example #13
0
def main():
    # 読み込み&初期画像表示
    img = cv2.imread("./assets/imori.jpg")
    showImg(img, QUESTION_ID)
    
    grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    kernelSize = 3

    # 平滑化フィルタ処理 no openCV
    ans = lapracianFilter(grayImg)
    showImg(ans, QUESTION_ID + "no_openCV" + QUESTION_NAME)

    # openCVでの平滑化処理    
    ans =  cv2.filter2D(grayImg, -1, kernel = getLapracianFilter())
    showImg(np.clip(ans, 0, 255), QUESTION_ID + "openCV" + QUESTION_NAME)
Example #14
0
def main():
    # 読み込み&初期画像表示
    img = cv2.imread("./assets/imori.jpg")
    showImg(img, QUESTION_ID)

    # バイリニア no openCV
    ans = bi_linear(img, ratio=1.5)
    showImg(ans, QUESTION_ID + "_noOpenCV_" + QUESTION_NAME)

    # バイリニア openCV
    ans = cv2.resize(
        img,
        dsize=(int(img.shape[0] * 1.5), int(img.shape[1] * 1.5)),
        interpolation=cv2.INTER_LINEAR,
    )
    showImg(ans, QUESTION_ID + "_OpenCV_" + QUESTION_NAME)
Example #15
0
def main():
    # 読み込み&初期画像表示
    img = cv2.imread("./assets/imori.jpg")
    showImg(img, QUESTION_ID)

    # no openCV
    ans1 = bi_cubic(img, ratio = 1.5)
    showImg(ans1, QUESTION_ID + "_noOpenCV_" + QUESTION_NAME)

    # openCV
    ans2 = cv2.resize(
                img,
                dsize = (int(img.shape[1] * 1.5), int(img.shape[0] * 1.5)),
                interpolation = cv2.INTER_CUBIC,
                )
    showImg(ans2, QUESTION_ID + "_OpenCV_" + QUESTION_NAME)
Example #16
0
def main():
    # 読み込み&初期画像表示
    img = cv2.imread("./assets/imori.jpg")
    showImg(img, QUESTION_ID)

    grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # ヒストグラム平坦化 no openCV
    ans = histgramFlatten_2(img)
    showImg(ans, QUESTION_ID + "_noOpenCV_" + QUESTION_NAME)

    # ヒストグラム平坦化
    ans = img.copy()
    for i in range(3):
        ans[:, :, i] = cv2.equalizeHist(img[:, :, i])
    showImg(ans, QUESTION_ID + "_OpenCV_" + QUESTION_NAME)
Example #17
0
def main():
    # 読み込み&初期画像表示
    img = cv2.imread("./assets/imori.jpg")
    showImg(img, QUESTION_ID)

    # no openCV
    ans1 = translation(img, dx = 30, dy = -30)
    showImg(ans1, QUESTION_ID + "_noOpenCV_" + QUESTION_NAME)

    # openCV
    M = np.array([[1,0,30],[0,1,-30]], dtype = np.float)
    ans2 = cv2.warpAffine(
                img,
                M,
                (img.shape[1], img.shape[0])
                )
    showImg(ans2, QUESTION_ID + "_OpenCV_" + QUESTION_NAME)
Example #18
0
def main():
    # 読み込み&初期画像表示
    img = cv2.imread("./assets/imori_noise.jpg")
    showImg(img, "Q6")
    tileSize = 3
    sigma = 1.3

    # ガウシアンフィルタ処理 no openCV
    ans = medianBlur(img, tileSize)
    showImg(ans, "Q7_no_openCV")

    # openCVでのガウシアンフィルタ処理 padding処理はdefault(reflect)
    ans = cv2.medianBlur(
        img,
        ksize=tileSize,
        #⃣borderType = cv2.BORDER_CONSTANT,
    )
    showImg(ans, "Q7_openCV")
Example #19
0
def main():
    # 読み込み&初期画像表示
    img = cv2.imread("./assets/imori_noise.jpg")
    showImg(img, "Q6")
    tileSize = 3
    sigma = 1.3

    # ガウシアンフィルタ処理 no openCV
    ans = gaussianBlur(img, tileSize, sigma)
    showImg(ans, "Q7_no_openCV")

    # openCVでのガウシアンフィルタ処理
    ans = cv2.GaussianBlur(
        img,
        ksize=(tileSize, tileSize),
        sigmaX=sigma,
        sigmaY=sigma,
        borderType=cv2.BORDER_CONSTANT,
    )
    showImg(ans, "Q7_openCV")
Example #20
0
def main():
    # 読み込み&初期画像表示
    img = cv2.imread("./assets/imori.jpg")
    showImg(img, QUESTION_ID)

    # no openCV
    ans1 = bi_cubic(img, 1.3, 0.8, -1)
    ans1 = translation(ans1, dx = 30, dy = -30)
    showImg(ans1, QUESTION_ID + "_noOpenCV_" + QUESTION_NAME)

    # openCV
    ans2 = cv2.resize(img,
                dsize = (int(img.shape[0] * 1.3), int(img.shape[1] * 0.8)),
                interpolation = cv2.INTER_CUBIC,
                )
    M = np.array([[1,0,30],[0,1,-30]], dtype = np.float)
    ans2 = cv2.warpAffine(
                ans2,
                M,
                (ans2.shape[1], ans2.shape[0])
                )
    showImg(ans2, QUESTION_ID + "_OpenCV_" + QUESTION_NAME)
Example #21
0
def main():
    # 読み込み&初期画像表示
    img = cv2.imread("./assets/imori.jpg")
    grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    showImg(grayImg, QUESTION_ID)
    h, w = grayImg.shape
    r1 = w / 2 * 0.1
    r2 = w / 2 * 0.5
    filt = np.array([[
        1 if r2 > sqrt(min(x, w - x)**2 + min(y, h - y)**2) > r1 else 0
        for x in range(w)
    ] for y in range(h)])
    # filt[0,0] = 1 # 直流成分(バイアス)だけ通す

    # # no openCV
    spectrum = fourier_trans(grayImg)
    spectrum *= filt

    ps = np.abs(spectrum[1:, 1:]) / np.abs(spectrum[1:, 1:]).max() * 255
    ps = ps.astype(np.uint8)

    showImg(ps, QUESTION_ID + " no opencv " + "power spectrum")

    ans1 = inv_fourier_trans(spectrum)
    showImg(
        ans1.clip(0, 255).astype(np.uint8),
        QUESTION_ID + " no opencv " + "img")

    # numpy
    spectrum = np.fft.fft2(grayImg)
    spectrum *= filt

    ps = np.abs(spectrum[1:, 1:]) / np.abs(spectrum[1:, 1:]).max() * 255
    ps = ps.astype(np.uint8)
    showImg(ps, QUESTION_ID + " numpy " + "power spectrum")

    ans2 = np.fft.ifft2(spectrum)
    ans2 = np.abs(ans2)

    showImg(
        ans2.clip(0, 255).astype(np.uint8), QUESTION_ID + " numpy " + "img")

    # openCV
    spectrum = cv2.dft(grayImg.astype(np.float), flags=cv2.DFT_COMPLEX_OUTPUT)
    spectrum[:, :, 0] *= filt
    spectrum[:, :, 1] *= filt

    ps = np.sqrt(spectrum[1:, 1:, 0]**2 + spectrum[1:, 1:, 1]**2)
    ps = ps / ps.max() * 255
    showImg(ps, QUESTION_ID + " opencv " + "power spectrum")

    ans3 = cv2.idft(spectrum)
    ans3 = cv2.magnitude(ans3[:, :, 0], ans3[:, :, 1]) / grayImg.size
    showImg(
        ans3.clip(0, 255).astype(np.uint8), QUESTION_ID + " opencv " + "img")
Example #22
0
def main():
    # 読み込み&初期画像表示
    img = cv2.imread("./assets/imori.jpg")
    grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    showImg(grayImg, QUESTION_ID)
    h, w = grayImg.shape
    filt = np.array([[1 for x in range(w)] for y in range(h)])

    # no openCV
    spectrum = fourier_trans(grayImg)
    spectrum *= filt

    ps = np.abs(spectrum) / np.abs(spectrum).max() * 255
    ps = ps.astype(np.uint8)

    showImg(ps, QUESTION_ID + " no opencv " + "power spectrum")

    ans1 = inv_fourier_trans(spectrum)
    showImg(ans1, QUESTION_ID + " no opencv " + "img")

    # numpy
    spectrum = np.fft.fft2(grayImg)
    spectrum *= filt

    ps = np.abs(spectrum) / np.abs(spectrum).max() * 255
    ps = ps.astype(np.uint8)
    showImg(ps, QUESTION_ID + " numpy " + "power spectrum")

    ans2 = np.fft.ifft2(spectrum)
    ans2 = np.abs(ans2)

    showImg(ans2, QUESTION_ID + " numpy " + "img")

    # openCV
    spectrum = cv2.dft(grayImg.astype(np.float), flags=cv2.DFT_COMPLEX_OUTPUT)
    spectrum[:, :, 0] *= filt
    spectrum[:, :, 1] *= filt

    ps = np.sqrt(spectrum[:, :, 0]**2 + spectrum[:, :, 1]**2)
    ps = ps / ps.max() * 255
    showImg(ps, QUESTION_ID + " opencv " + "power spectrum")

    ans3 = cv2.idft(spectrum)
    ans3 = cv2.magnitude(ans3[:, :, 0], ans3[:, :, 1]) / grayImg.size
    showImg(ans3, QUESTION_ID + " opencv " + "img")