{ "cells": [ { "cell_type": "code", "execution_count": 3, "id": "670cf316-1aee-4e4b-8a87-d5ef568d2eed", "metadata": {}, "outputs": [], "source": [ "import cv2\n", "import numpy as np" ] }, { "cell_type": "code", "execution_count": 7, "id": "0d5211e7-dd25-49c4-9e1b-d19f580be4fe", "metadata": {}, "outputs": [], "source": [ "img = cv2.imread(r\"C:\\Users\\phane\\Downloads\\OIP.jpeg\")" ] }, { "cell_type": "code", "execution_count": 9, "id": "41f4aadc-8db1-47f8-ab7c-cbcb0e0a7543", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(469, 474, 3)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "img.shape" ] }, { "cell_type": "markdown", "id": "4b0aa245-3543-400f-9f03-24678a273e07", "metadata": {}, "source": [ "## Translation Matrix" ] }, { "cell_type": "code", "execution_count": 11, "id": "73728266-f7ec-4343-9071-e0515025367d", "metadata": {}, "outputs": [], "source": [ "tx = 50\n", "ty = 50\n", "t_m = np.array([[1,0,tx],[0,1,ty]],dtype = np.float32)\n", "\n", "t_img = cv2.warpAffine(img,t_m,(474,469))" ] }, { "cell_type": "code", "execution_count": 14, "id": "1da4ae8c-7dcb-4c3b-a334-05ae3184a8fb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1., 0., 50.],\n", " [ 0., 1., 50.]], dtype=float32)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "t_m" ] }, { "cell_type": "code", "execution_count": 10, "id": "3e7b9305-f250-4b61-9223-8bbe3c8a5f60", "metadata": {}, "outputs": [], "source": [ "cv2.imshow(\"o_i\",img)\n", "cv2.imshow(\"t_i\",t_img)\n", "cv2.waitKey()\n", "cv2.destroyAllWindows()" ] }, { "cell_type": "code", "execution_count": 28, "id": "e96e24c2-3224-49b0-9aac-5979aff7c5a5", "metadata": {}, "outputs": [], "source": [ "tx = 50\n", "ty = 50\n", "t_m = np.array([[1,0,tx],[0,1,ty]],dtype = np.float32)\n", "\n", "t_img = cv2.warpAffine(img,t_m,(474,469),borderMode=cv2.BORDER_CONSTANT,borderValue = (100,100,255))\n", "t_img1 = cv2.warpAffine(img,t_m,(474,469),borderMode=cv2.BORDER_REFLECT)" ] }, { "cell_type": "code", "execution_count": 30, "id": "9444d2f3-e3cf-46f6-bf3a-a5ba1980fdf8", "metadata": {}, "outputs": [], "source": [ "cv2.imshow(\"o_i\",img)\n", "cv2.imshow(\"t_i\",t_img)\n", "cv2.imshow(\"t_i1\",t_img1)\n", "cv2.waitKey()\n", "cv2.destroyAllWindows()" ] }, { "cell_type": "markdown", "id": "4992b214-417f-4d0c-befd-917c8b526f89", "metadata": {}, "source": [ "## Rotational Matrix" ] }, { "cell_type": "code", "execution_count": 89, "id": "5aa99d8c-82ef-440e-9609-3c4362973ed3", "metadata": {}, "outputs": [], "source": [ "r_m = cv2.getRotationMatrix2D((237,234),110,1) ## (237,234) it indicates pixal ,110 degrees to rotate, 1 it indicates original img" ] }, { "cell_type": "code", "execution_count": 91, "id": "d580676e-5b8e-4f73-8d32-b2e535de887e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-3.42020143e-01, 9.39692621e-01, 9.81707007e+01],\n", " [-9.39692621e-01, -3.42020143e-01, 5.36739865e+02]])" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r_m" ] }, { "cell_type": "code", "execution_count": 105, "id": "ebcddf1f-5ab1-4e90-b312-e7cf59ed1e27", "metadata": {}, "outputs": [], "source": [ "r_img = cv2.warpAffine(img,r_m,(474,469))" ] }, { "cell_type": "code", "execution_count": 107, "id": "75c78fb5-543b-487b-90c0-b0a310a49165", "metadata": {}, "outputs": [], "source": [ "cv2.imshow(\"o_i\",img)\n", "cv2.imshow(\"r_i\",r_img)\n", "cv2.waitKey()\n", "cv2.destroyAllWindows()" ] }, { "cell_type": "code", "execution_count": 11, "id": "3bbd1344-93f2-44a9-9ebc-0d9d8949bb22", "metadata": {}, "outputs": [], "source": [ "img1=cv2.rotate(img,cv2.ROTATE_180)\n", "img2=cv2.rotate(img,cv2.ROTATE_90_CLOCKWISE)\n", "img3 = cv2.rotate(img,cv2.ROTATE_90_COUNTERCLOCKWISE)\n", "\n", "cv2.imshow(\"o_i\",img)\n", "cv2.imshow(\"r_i1\",img1)\n", "cv2.imshow(\"r_i2\",img2)\n", "cv2.imshow(\"r_i3\",img3)\n", "cv2.waitKey()\n", "cv2.destroyAllWindows()" ] }, { "cell_type": "markdown", "id": "91209549-3a0e-4c48-8d2a-80f6a31a0cc8", "metadata": {}, "source": [ "## Scaling (Zoom in and Zoom out)" ] }, { "cell_type": "code", "execution_count": 38, "id": "50284545-c4f6-4b80-bd57-b15205088a0d", "metadata": {}, "outputs": [], "source": [ "## Scaling have Resize and Pyrdown() and Pyrup()\n", "\n", "\n", "sx= 2\n", "sy = 2\n", "tx=100\n", "ty = 100\n", "\n", "sc_m = np.array([[sx,0,tx],[0,sy,ty]],dtype = np.float32)" ] }, { "cell_type": "code", "execution_count": 40, "id": "1e7e470c-219a-4293-94df-8dfcc64f4b47", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 2., 0., 100.],\n", " [ 0., 2., 100.]], dtype=float32)" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sc_m" ] }, { "cell_type": "code", "execution_count": 44, "id": "f6ac5de8-9606-49dc-ae36-02c022bf548f", "metadata": {}, "outputs": [], "source": [ "scale_img = cv2.warpAffine(img,sc_m,(2*474,2*469))\n", "\n", "cv2.imshow(\"o_i\",img)\n", "cv2.imshow(\"S-im\",scale_img)\n", "cv2.waitKey()\n", "cv2.destroyAllWindows()" ] }, { "cell_type": "code", "execution_count": 46, "id": "4ce575a0-e1de-4520-8bb8-37af3c48f871", "metadata": {}, "outputs": [], "source": [ "zo_o = cv2.pyrDown(img)\n", "zo_i = cv2.pyrUp(img)" ] }, { "cell_type": "code", "execution_count": 48, "id": "ee0ed2e7-b1b5-4d24-a8c9-0507a74f8106", "metadata": {}, "outputs": [], "source": [ "cv2.imshow(\"o_i\",zo_i)\n", "cv2.imshow(\"S-im\",zo_o)\n", "cv2.waitKey()\n", "cv2.destroyAllWindows()" ] }, { "cell_type": "markdown", "id": "08a15a39-86bf-4a04-9f93-731c199446dd", "metadata": {}, "source": [ "## Sheering " ] }, { "cell_type": "code", "execution_count": 70, "id": "65543283-b5fc-472b-9f7e-eeaebd70bd78", "metadata": {}, "outputs": [], "source": [ "shx = 1\n", "shy = 1\n", "tx = 0\n", "ty = 0\n", "\n", "sh_m = np.array([[1,shx,tx],[shy,1,ty]],dtype = np.float32)" ] }, { "cell_type": "code", "execution_count": 72, "id": "af8dba52-2d25-42d7-aef3-fe5ac2f34322", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 1., 0.],\n", " [1., 1., 0.]], dtype=float32)" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sh_m" ] }, { "cell_type": "code", "execution_count": 74, "id": "59ac5f58-0937-4f4f-9fdf-4de0f52e414d", "metadata": {}, "outputs": [], "source": [ "sheering_img = cv2.warpAffine(img,sh_m,(474,469))\n", "\n", "cv2.imshow(\"o_i\",img)\n", "cv2.imshow(\"She-im\",sheering_img)\n", "cv2.waitKey()\n", "cv2.destroyAllWindows()" ] }, { "cell_type": "markdown", "id": "e61d5da0-6c1a-452b-80dd-5b801f768745", "metadata": {}, "source": [ "## Cropping" ] }, { "cell_type": "code", "execution_count": 77, "id": "c9b7df21-dd3c-4be6-934b-10b4529bef2a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[ 70, 82, 152],\n", " [ 69, 81, 151],\n", " [ 71, 82, 150],\n", " ...,\n", " [147, 125, 167],\n", " [153, 131, 173],\n", " [153, 131, 173]],\n", "\n", " [[ 68, 80, 150],\n", " [ 68, 80, 150],\n", " [ 69, 80, 148],\n", " ...,\n", " [145, 123, 165],\n", " [153, 131, 173],\n", " [153, 131, 173]],\n", "\n", " [[ 66, 78, 148],\n", " [ 66, 78, 148],\n", " [ 65, 77, 147],\n", " ...,\n", " [144, 123, 162],\n", " [151, 130, 169],\n", " [151, 130, 169]],\n", "\n", " ...,\n", "\n", " [[159, 150, 160],\n", " [155, 146, 156],\n", " [152, 140, 152],\n", " ...,\n", " [156, 154, 176],\n", " [154, 152, 174],\n", " [154, 152, 174]],\n", "\n", " [[164, 155, 165],\n", " [159, 150, 160],\n", " [157, 145, 157],\n", " ...,\n", " [142, 140, 162],\n", " [166, 164, 186],\n", " [166, 164, 186]],\n", "\n", " [[166, 157, 167],\n", " [162, 153, 163],\n", " [159, 147, 159],\n", " ...,\n", " [158, 156, 178],\n", " [173, 171, 193],\n", " [173, 171, 193]]], dtype=uint8)" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "img = cv2.imread(r\"C:\\Users\\phane\\Downloads\\OIP.jpeg\")\n", "img" ] }, { "cell_type": "code", "execution_count": 81, "id": "5c02ac13-8aee-48af-b5cd-bb6fac208999", "metadata": {}, "outputs": [], "source": [ "crop_img = img[64:318,137:412]" ] }, { "cell_type": "code", "execution_count": 87, "id": "7c2a8714-3f01-489b-b746-3e650de3645c", "metadata": {}, "outputs": [], "source": [ "cv2.imshow(\"o_i\",img)\n", "cv2.imshow(\"S-im\",crop_img)\n", "cv2.waitKey()\n", "cv2.destroyAllWindows()" ] }, { "cell_type": "code", "execution_count": 85, "id": "17168e79-54a0-4765-83c4-4509c7a5855b", "metadata": {}, "outputs": [], "source": [ "crop_img = img[137:412,64:318]" ] }, { "cell_type": "code", "execution_count": null, "id": "4c0d4a73-a1e5-42f0-95c1-a91b65829d9e", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.7" } }, "nbformat": 4, "nbformat_minor": 5 }