{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-03-21T23:46:20.072322Z","iopub.execute_input":"2022-03-21T23:46:20.072855Z","iopub.status.idle":"2022-03-21T23:46:37.231379Z","shell.execute_reply.started":"2022-03-21T23:46:20.072735Z","shell.execute_reply":"2022-03-21T23:46:37.230248Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"![](https://i.ytimg.com/vi/Cjv9Xaqnjx4/maxresdefault.jpg)youtube.com","metadata":{}},{"cell_type":"markdown","source":"<h1><span class=\"label label-default\" style=\"background-color:black;border-radius:100px 100px; font-weight: bold; font-family:Garamond; font-size:20px; color:#03e8fc; padding:10px\">ROI-Based Processing</span></h1><br>\n\nDefine and operate on regions of interest (ROI)\n\n\"A region of interest (ROI) is a portion of an image that you want to filter or operate on in some way. You can represent an ROI as a binary mask image. In the mask image, pixels that belong to the ROI are set to 1 and pixels outside the ROI are set to 0. The toolbox offers several options to specify ROIs and create binary masks.\"\n\n\"The toolbox supports a set of objects that you can use to create ROIs of many shapes, such circles, ellipses, polygons, rectangles, and hand-drawn shapes. After you create the objects, you can modify their shape, position, appearance, and behavior.\" \n\nhttps://www.mathworks.com/help/images/roi-based-processing.html","metadata":{}},{"cell_type":"code","source":"# Importing essential libraries\nimport cv2, glob, os\nfrom matplotlib import pyplot as plt\nfrom PIL import Image","metadata":{"execution":{"iopub.status.busy":"2022-03-21T23:46:44.890732Z","iopub.execute_input":"2022-03-21T23:46:44.891711Z","iopub.status.idle":"2022-03-21T23:46:45.266017Z","shell.execute_reply.started":"2022-03-21T23:46:44.891667Z","shell.execute_reply":"2022-03-21T23:46:45.265105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Importing essential datasets\nBASE = '../input/hotel-id-to-combat-human-trafficking-2022-fgvc9/train_images/'","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:01:27.510747Z","iopub.execute_input":"2022-03-22T00:01:27.511057Z","iopub.status.idle":"2022-03-22T00:01:27.514671Z","shell.execute_reply.started":"2022-03-22T00:01:27.511028Z","shell.execute_reply":"2022-03-22T00:01:27.513744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Reading and understanding a single image","metadata":{}},{"cell_type":"code","source":"# Reading and understanding a single image\nimg = cv2.imread(\"../input/hotel-id-to-combat-human-trafficking-2022-fgvc9/train_images/100297/000003292.jpg\")\nplt.figure(figsize=(10,10))\nplt.imshow(img, cmap = 'winter'), plt.axis('off'), plt.title('Hotel Room',fontsize=20),plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2022-03-21T23:52:10.684381Z","iopub.execute_input":"2022-03-21T23:52:10.684669Z","iopub.status.idle":"2022-03-21T23:52:11.065629Z","shell.execute_reply.started":"2022-03-21T23:52:10.684633Z","shell.execute_reply":"2022-03-21T23:52:11.064940Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Loading a whole bunch of images in train and test datasets\nimage_train = [cv2.imread(file) for file in glob.glob(BASE+'100297/000003292.jpg')]\n#image_test = [cv2.imread(file) for file in glob.glob(BASE+'abc.jpg')]\nimage_01 = cv2.imread(BASE+'10054/000039550.jpg')\nimage_02 = cv2.imread(BASE+'102048/000003820.jpg')","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:02:14.782116Z","iopub.execute_input":"2022-03-22T00:02:14.782907Z","iopub.status.idle":"2022-03-22T00:02:14.853968Z","shell.execute_reply.started":"2022-03-22T00:02:14.782854Z","shell.execute_reply":"2022-03-22T00:02:14.853259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Select some images","metadata":{}},{"cell_type":"code","source":"# Visualizing the selected images\nplt.figure(figsize=(20,10))\nplt.subplot(121),plt.imshow(image_01, cmap = 'hsv'), plt.axis('off'), plt.title('Hotel Room 2',fontsize=30)\nplt.subplot(122),plt.imshow(image_02, cmap = 'cividis'), plt.axis('off'), plt.title('Hotel room 3',fontsize=30)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:08:42.932262Z","iopub.execute_input":"2022-03-22T00:08:42.932699Z","iopub.status.idle":"2022-03-22T00:08:43.538437Z","shell.execute_reply.started":"2022-03-22T00:08:42.932657Z","shell.execute_reply":"2022-03-22T00:08:43.537731Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Histogram 1st image","metadata":{}},{"cell_type":"code","source":"# Visualizing the image histogram for first image\ncounts,bins,_ = plt.hist(image_01.ravel(),density = False, alpha = 0.8, histtype = 'stepfilled', color = '#0303FF', edgecolor = '#44FF80')","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:10:58.700671Z","iopub.execute_input":"2022-03-22T00:10:58.700933Z","iopub.status.idle":"2022-03-22T00:10:58.858513Z","shell.execute_reply.started":"2022-03-22T00:10:58.700904Z","shell.execute_reply":"2022-03-22T00:10:58.857814Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Histogram 2nd image","metadata":{}},{"cell_type":"code","source":"# Visualizing the histogram for second image\ncounts,bins,_ = plt.hist(image_02.ravel(),density = True, alpha = 0.2, histtype = 'stepfilled', color = '#DC143C', edgecolor = '#FF0000')","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:12:31.790186Z","iopub.execute_input":"2022-03-22T00:12:31.790811Z","iopub.status.idle":"2022-03-22T00:12:32.019703Z","shell.execute_reply.started":"2022-03-22T00:12:31.790777Z","shell.execute_reply":"2022-03-22T00:12:32.018745Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Multivariate normal - 1st image","metadata":{}},{"cell_type":"code","source":"# Understanding multivariate normal for the first image\nx, y = np.random.multivariate_normal([0,200],[[1, 0], [0, 200]],10000).T\nplt.hist2d(x,y,bins=30,cmap=\"Blues\")\ncb = plt.colorbar()\ncb.set_label('Counts in Bin')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:12:59.675520Z","iopub.execute_input":"2022-03-22T00:12:59.676098Z","iopub.status.idle":"2022-03-22T00:12:59.927760Z","shell.execute_reply.started":"2022-03-22T00:12:59.676063Z","shell.execute_reply":"2022-03-22T00:12:59.926954Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Multivariate normal - 2nd image","metadata":{}},{"cell_type":"code","source":"# Understanding multivariate normal for the second image\nx, y = np.random.multivariate_normal([0,200],[[1, 0], [0, 200]],10000).T\nplt.hist2d(x,y,bins=30,cmap=\"Greens\")\ncb = plt.colorbar()\ncb.set_label('Counts in Bin')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:13:55.068811Z","iopub.execute_input":"2022-03-22T00:13:55.069529Z","iopub.status.idle":"2022-03-22T00:13:55.308670Z","shell.execute_reply.started":"2022-03-22T00:13:55.069485Z","shell.execute_reply":"2022-03-22T00:13:55.307729Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Grayscale Histogram","metadata":{}},{"cell_type":"code","source":"# Grayscale histogram\nplt.figure(figsize=(15,8))\nplt.subplot(241), plt.plot(cv2.calcHist([cv2.cvtColor(image_01, cv2.COLOR_BGR2GRAY)],[0],None,[256], [0,256]), color = 'k'), plt.title('Hotel Room 2',fontsize=15)\nplt.subplot(242), plt.plot(cv2.calcHist([image_01],[0],None,[256],[0,256]),color = 'b'), plt.xlim([0,256])\nplt.subplot(243), plt.plot(cv2.calcHist([image_01],[0],None,[256],[0,256]),color = 'g'), plt.xlim([0,256])\nplt.subplot(244), plt.plot(cv2.calcHist([image_01],[0],None,[256],[0,256]),color = 'r'), plt.xlim([0,256])\nplt.subplot(245), plt.plot(cv2.calcHist([cv2.cvtColor(image_02, cv2.COLOR_BGR2GRAY)],[0],None,[256], [0,256]), color = 'k'), plt.title('Hotel Room 3',fontsize=15)\nplt.subplot(246), plt.plot(cv2.calcHist([image_02],[0],None,[256],[0,256]),color = 'b'), plt.xlim([0,256])\nplt.subplot(247), plt.plot(cv2.calcHist([image_02],[0],None,[256],[0,256]),color = 'g'), plt.xlim([0,256])\nplt.subplot(248), plt.plot(cv2.calcHist([image_02],[0],None,[256],[0,256]),color = 'r'), plt.xlim([0,256])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:15:18.998534Z","iopub.execute_input":"2022-03-22T00:15:18.998811Z","iopub.status.idle":"2022-03-22T00:15:19.768804Z","shell.execute_reply.started":"2022-03-22T00:15:18.998782Z","shell.execute_reply":"2022-03-22T00:15:19.768214Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Grayscale","metadata":{}},{"cell_type":"code","source":"# Grayscale Histogram Equalization\nplt.figure(figsize=(20,10))\nplt.subplot(121),plt.imshow(cv2.cvtColor(image_01, cv2.COLOR_BGR2GRAY), cmap = 'gray'), plt.axis('off'), plt.title('Hotel room 2',fontsize=20)\nplt.subplot(122),plt.imshow(cv2.equalizeHist(cv2.cvtColor(image_01, cv2.COLOR_BGR2GRAY)), cmap = 'gray'), plt.axis('off'), plt.title('Equalized Histogram',fontsize=20)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:16:19.644980Z","iopub.execute_input":"2022-03-22T00:16:19.645581Z","iopub.status.idle":"2022-03-22T00:16:20.204171Z","shell.execute_reply.started":"2022-03-22T00:16:19.645534Z","shell.execute_reply":"2022-03-22T00:16:20.203237Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Channel Histogram Equalization","metadata":{}},{"cell_type":"code","source":"# 3-channel Histogram Equalization\nchannels = cv2.split(image_01)\neq_channels = []\nfor ch, color in zip(channels, ['B', 'G', 'R']): \n    eq_channels.append(cv2.equalizeHist(ch))\nplt.figure(figsize=(20,10))\nplt.subplot(121),plt.imshow(image_01, cmap = 'gray'), plt.axis('off'), plt.title('Hotel room 2',fontsize=20)\nplt.subplot(122),plt.imshow(cv2.cvtColor(cv2.merge(eq_channels),cv2.COLOR_BGR2RGB), cmap = 'gray'), plt.axis('off'), plt.title('Equalized Histogram',fontsize=20)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:17:14.959058Z","iopub.execute_input":"2022-03-22T00:17:14.959441Z","iopub.status.idle":"2022-03-22T00:17:15.728267Z","shell.execute_reply.started":"2022-03-22T00:17:14.959394Z","shell.execute_reply":"2022-03-22T00:17:15.725956Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Blurring","metadata":{}},{"cell_type":"code","source":"# Averaging the images\nplt.figure(figsize=(20,10))\nplt.subplot(121),plt.imshow(cv2.blur(image_01,(40,40)), cmap = 'hsv'), plt.axis('off'), plt.title('Hotel room 2',fontsize=30)\nplt.subplot(122),plt.imshow(cv2.blur(image_02,(20,20)), cmap = 'cividis'), plt.axis('off'), plt.title('Hotel room 3',fontsize=30)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:18:12.776087Z","iopub.execute_input":"2022-03-22T00:18:12.776373Z","iopub.status.idle":"2022-03-22T00:18:13.414432Z","shell.execute_reply.started":"2022-03-22T00:18:12.776333Z","shell.execute_reply":"2022-03-22T00:18:13.413628Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#ROI (Region of Interest) selection.","metadata":{}},{"cell_type":"code","source":"# ROI selection in image the images\n# image_01[300:600,170:400] where the first is from top to bottom and the second is from left to right\nlugagge_01 = image_01[380:480,180:390]\nlugagge_02 = image_02[380:480,180:390] # Original 420:500,190:310\nplt.figure(figsize=(20,10))\nplt.subplot(121), plt.imshow(lugagge_01, cmap = 'hsv'), plt.axis('off'), plt.title('Hotel room 2 lugagge',fontsize=30)\nplt.subplot(122), plt.imshow(desk_02, cmap = 'Blues'), plt.axis('off'), plt.title('Hotel room 3 lugagge',fontsize=30)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:26:45.665990Z","iopub.execute_input":"2022-03-22T00:26:45.666248Z","iopub.status.idle":"2022-03-22T00:26:45.998695Z","shell.execute_reply.started":"2022-03-22T00:26:45.666221Z","shell.execute_reply":"2022-03-22T00:26:45.997763Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Image information","metadata":{}},{"cell_type":"code","source":"# Randomly getting some image information\nprint(image_01.shape)\nprint(image_01.dtype)\nprint(lugagge_01.shape)\nprint(lugagge_02.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:29:37.012557Z","iopub.execute_input":"2022-03-22T00:29:37.013012Z","iopub.status.idle":"2022-03-22T00:29:37.019945Z","shell.execute_reply.started":"2022-03-22T00:29:37.012971Z","shell.execute_reply":"2022-03-22T00:29:37.018935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Borders","metadata":{}},{"cell_type":"code","source":"# Making borders for the images\nplt.figure(figsize=(20,10))\nplt.subplot(231), plt.imshow(lugagge_01, cmap = 'gray'), plt.axis('off'), plt.title('Grey',fontsize=25)\nplt.subplot(232), plt.imshow(cv2.copyMakeBorder(lugagge_01,10,10,10,10,cv2.BORDER_REPLICATE), cmap = 'Blues'), plt.axis('off'), plt.title('Replicate',fontsize=25)\nplt.subplot(233), plt.imshow(cv2.copyMakeBorder(lugagge_01,10,10,10,10,cv2.BORDER_REFLECT), cmap = 'gray'), plt.axis('off'), plt.title('Reflect',fontsize=25)\nplt.subplot(234), plt.imshow(cv2.copyMakeBorder(lugagge_01,10,10,10,10,cv2.BORDER_REFLECT_101), cmap = 'Blues'), plt.axis('off'), plt.title('Reflect 101',fontsize=25)\nplt.subplot(235), plt.imshow(cv2.copyMakeBorder(lugagge_01,10,10,10,10,cv2.BORDER_WRAP), cmap = 'gray'), plt.axis('off'), plt.title('Wrap',fontsize=25)\nplt.subplot(236), plt.imshow(cv2.copyMakeBorder(lugagge_01,10,10,10,10,cv2.BORDER_CONSTANT,value=(120,80,250)), cmap = 'Blues'), plt.axis('off'), plt.title('Constant',fontsize=25)\nplt.subplots_adjust(wspace=0.05, hspace=-0.3)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:31:21.304173Z","iopub.execute_input":"2022-03-22T00:31:21.304460Z","iopub.status.idle":"2022-03-22T00:31:22.040076Z","shell.execute_reply.started":"2022-03-22T00:31:21.304431Z","shell.execute_reply":"2022-03-22T00:31:22.039016Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Gaussian Masking","metadata":{}},{"cell_type":"code","source":"# Mask operations for the images\nkernel = cv2.getGaussianKernel(15, 2.0)\nkernel_2D = kernel @ kernel.transpose()\nblurred_lugagge = cv2.filter2D(lugagge_01, -1, kernel_2D)\nplt.imshow(blurred_lugagge, cmap = 'Blues'), plt.axis('off'), plt.title('Gaussian masking',fontsize=20), plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:32:42.828061Z","iopub.execute_input":"2022-03-22T00:32:42.828326Z","iopub.status.idle":"2022-03-22T00:32:43.007511Z","shell.execute_reply.started":"2022-03-22T00:32:42.828300Z","shell.execute_reply":"2022-03-22T00:32:43.006524Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Blending images","metadata":{}},{"cell_type":"code","source":"# Blending images\n#dst = cv2.addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]])\n#dst = src1 * alpha + src2 * beta + gamma\nlugagge_02x = cv2.resize(lugagge_02,lugagge_01.shape[1::-1])\nblended_image = cv2.addWeighted(lugagge_01, 0.5, lugagge_02x, 0.5, 0)\nplt.imshow(blended_image, cmap = 'Blues'), plt.axis('off'), plt.title('Merging images using weights',fontsize=20), plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:34:30.075235Z","iopub.execute_input":"2022-03-22T00:34:30.075524Z","iopub.status.idle":"2022-03-22T00:34:30.241440Z","shell.execute_reply.started":"2022-03-22T00:34:30.075491Z","shell.execute_reply":"2022-03-22T00:34:30.240647Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Masking Images - Bitwise Masking","metadata":{}},{"cell_type":"code","source":"# Masking images\nplt.imshow(cv2.bitwise_and(lugagge_01, lugagge_02x), cmap = 'Blues'), plt.axis('off'), plt.title('Bitwise masking',fontsize=20), plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:35:34.772011Z","iopub.execute_input":"2022-03-22T00:35:34.772282Z","iopub.status.idle":"2022-03-22T00:35:34.937831Z","shell.execute_reply.started":"2022-03-22T00:35:34.772254Z","shell.execute_reply":"2022-03-22T00:35:34.937106Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Masking Images","metadata":{}},{"cell_type":"code","source":"# Masking images\nplt.imshow((lugagge_01*0.2+lugagge_02x*0.8).astype(np.uint8), cmap = 'Blues'), plt.axis('off'), plt.title('Masking images',fontsize=20), plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:37:36.801536Z","iopub.execute_input":"2022-03-22T00:37:36.801820Z","iopub.status.idle":"2022-03-22T00:37:36.960748Z","shell.execute_reply.started":"2022-03-22T00:37:36.801790Z","shell.execute_reply":"2022-03-22T00:37:36.959853Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Uniform addition of pixel values to images","metadata":{}},{"cell_type":"code","source":"# Uniform addition of pixel values to images\nlugagge_01x = (lugagge_01 * 0.5 + lugagge_02x * 0.2 + (96, 128, 160)).clip(0,255)\nplt.imshow(lugagge_01x.astype(np.uint8), cmap = 'Blues'), plt.axis('off'), plt.title('Uniform addition',fontsize=20), plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:39:14.208887Z","iopub.execute_input":"2022-03-22T00:39:14.209220Z","iopub.status.idle":"2022-03-22T00:39:14.366541Z","shell.execute_reply.started":"2022-03-22T00:39:14.209188Z","shell.execute_reply":"2022-03-22T00:39:14.365737Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Sample Mask","metadata":{}},{"cell_type":"code","source":"# Mask creation by drawing in image\nmask_01 = np.zeros_like(image_02[0:300,0:400])\ncv2.rectangle(mask_01, (50, 50), (100, 200), (255, 255, 255), thickness=-1)\ncv2.circle(mask_01, (200, 100), 50, (255, 255, 255), thickness=-1)\ncv2.fillConvexPoly(mask_01, np.array([[330, 50], [300, 200], [360, 150]]), (255, 255, 255))\nmask_01x = cv2.resize(mask_01,image_02.shape[1::-1])\nplt.imshow(mask_01), plt.axis('off'), plt.title('Sample Mask',fontsize=20), plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:39:50.833600Z","iopub.execute_input":"2022-03-22T00:39:50.834632Z","iopub.status.idle":"2022-03-22T00:39:50.970148Z","shell.execute_reply.started":"2022-03-22T00:39:50.834576Z","shell.execute_reply":"2022-03-22T00:39:50.969240Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Bitwise and Mask created","metadata":{}},{"cell_type":"code","source":"# Bitwise and with the mask created\nplt.figure(figsize=(10,10))\nplt.imshow(cv2.bitwise_and(image_02,mask_01x)), plt.axis('off'), plt.title('Bitwise masking',fontsize=20), plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:40:22.085667Z","iopub.execute_input":"2022-03-22T00:40:22.086625Z","iopub.status.idle":"2022-03-22T00:40:22.340932Z","shell.execute_reply.started":"2022-03-22T00:40:22.086584Z","shell.execute_reply":"2022-03-22T00:40:22.339894Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reading a new image for working with image channels\nimage_03 = cv2.imread(BASE+'100297/000003292.jpg')\nprint(image_03.shape)\n\n# Splitting the channels\nplt.figure(figsize=(15,15))\nb,g,r = cv2.split(image_03)\nmask_03 = np.zeros(image_03.shape[:2], dtype = \"uint8\")\nimage_03x = cv2.merge((mask_03,g,r))\nplt.subplot(221), plt.imshow(image_03[:,:,0], cmap= 'gray'), plt.axis('off'), plt.title('Red Channel',fontsize=20)\nplt.subplot(222), plt.imshow(image_03[:,:,1], cmap= 'gray'), plt.axis('off'), plt.title('Green Channel',fontsize=20) \nplt.subplot(223), plt.imshow(image_03[:,:,2], cmap= 'gray'), plt.axis('off'), plt.title('Blue Channel',fontsize=20) \nplt.subplot(224), plt.imshow(image_03x), plt.axis('off'), plt.title('Channels Merged',fontsize=20)\nplt.subplots_adjust(wspace=0, hspace=-0.25)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:42:15.055386Z","iopub.execute_input":"2022-03-22T00:42:15.055689Z","iopub.status.idle":"2022-03-22T00:42:15.952710Z","shell.execute_reply.started":"2022-03-22T00:42:15.055657Z","shell.execute_reply":"2022-03-22T00:42:15.951836Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Crop and Resize Images\nheight, width = image_03.shape[:2]\nquarter_height, quarter_width = height / 4, width / 4\nT = np.float32([[1, 0, quarter_width], [0, 1, quarter_height]]) \n\nplt.figure(figsize=(20,15))\nplt.subplot(231), plt.imshow(image_03), plt.axis('off'), plt.title('Hotel room 1',fontsize=20)\nplt.subplot(232), plt.imshow(cv2.resize(image_03,(200,200))), plt.axis('off'), plt.title('Resized Image',fontsize=20)\nplt.subplot(233), plt.imshow(image_03[470:610,420:610]), plt.axis('off'), plt.title('Cropped Image',fontsize=20)\nplt.subplot(234), plt.imshow(cv2.warpAffine(image_03, T, (width,height)) ), plt.axis('off'), plt.title('Translated Image',fontsize=20)\nplt.subplot(235), plt.imshow(cv2.rotate(image_03, cv2.ROTATE_90_CLOCKWISE)), plt.axis('off'), plt.title('Rotated Image',fontsize=20)\nplt.subplot(236), plt.imshow(np.flip(image_03,(0, 1))), plt.axis('off'), plt.title('Flipped Image',fontsize=20);","metadata":{"execution":{"iopub.status.busy":"2022-03-22T00:45:20.549257Z","iopub.execute_input":"2022-03-22T00:45:20.549538Z","iopub.status.idle":"2022-03-22T00:45:21.842677Z","shell.execute_reply.started":"2022-03-22T00:45:20.549497Z","shell.execute_reply":"2022-03-22T00:45:21.841775Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Acknowledgement:\n\nShrushrita Sharma  https://www.kaggle.com/code/shrushritasharma/understanding-basic-image-operations/notebook","metadata":{}}]}