{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 引入影象處理庫OpenCV\n", "import cv2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 引入numpy庫,以np做別名\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 要被複製的圖片\n", "image = cv2.imread('data/image/logo.png')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 使用numpy.zeros來返回一個新的數組,其中參數則是原圖片的屬性以及資料類型\n", "numpy_image = np.zeros(image.shape, np.uint8)\n", "\n", "# 獲取被複製圖片的rows、cols\n", "rows = image.shape[0]\n", "cols = image.shape[1]\n", "\n", "# 遍尋複製好的數組,將被複製的數組,按照位置塞入\n", "for now_row in range(rows): \n", " for now_col in range(cols):\n", " numpy_image[now_row, now_col, 0] = image[now_row, now_col, 0]\n", " numpy_image[now_row, now_col, 1] = image[now_row, now_col, 1]\n", " numpy_image[now_row, now_col, 2] = image[now_row, now_col, 2]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 直接複製原來的圖片到一張新的圖片上\n", "copy_image = image.copy()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 使用cvtColor獲取原圖片的副本\n", "cvtColor_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 調整大小\n", "numpy_image = cv2.resize(numpy_image, (200, 400))\n", "copy_image = cv2.resize(copy_image, (numpy_image.shape[0], numpy_image.shape[1]))\n", "cvtColor_image = cv2.resize(cvtColor_image, None, fx=0.8, fy=1.2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 顯示圖片\n", "cv2.imshow(\"image\", image)\n", "cv2.imshow(\"numpy_image\", numpy_image)\n", "cv2.imshow(\"copy_image\", copy_image)\n", "cv2.imshow(\"cvtColor_image\", cvtColor_image)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 持續等待按鍵,並刷新\n", "cv2.waitKey(0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 關閉所有OpenCV視窗\n", "cv2.destroyAllWindows()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }