Skip to content

Instantly share code, notes, and snippets.

@galaxygamerman
Last active April 22, 2025 08:36
Show Gist options
  • Save galaxygamerman/d8ec2bf1f4ae4bf900da42b6639d2c0e to your computer and use it in GitHub Desktop.
Save galaxygamerman/d8ec2bf1f4ae4bf900da42b6639d2c0e to your computer and use it in GitHub Desktop.
CG Lab python scripts for image processing
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "e9ea178c",
"metadata": {},
"source": [
"# Exp 5"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "da0a9bae",
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"id": "7666f5fe",
"metadata": {},
"source": [
"callback to trackbars"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "7161aea7",
"metadata": {},
"outputs": [],
"source": [
"def on_change(value):\n",
" img = cv2.rectangle(image, (0,0), (550,550), (0,0,0), -1)\n",
" pass"
]
},
{
"cell_type": "markdown",
"id": "78dc4dfe",
"metadata": {},
"source": [
"create a window"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "43374279",
"metadata": {},
"outputs": [],
"source": [
"cv2.namedWindow(\"Shape Properties\")\n",
"image = np.zeros((550,550,3), np.uint8)"
]
},
{
"cell_type": "markdown",
"id": "a913b776",
"metadata": {},
"source": [
"initial values"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "8b70b6a0",
"metadata": {},
"outputs": [],
"source": [
"initial_width = 200\n",
"initial_height = 100\n",
"initial_color = (0,255,0) #given"
]
},
{
"cell_type": "markdown",
"id": "19ab15b8",
"metadata": {},
"source": [
"create trackbars"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "aadad5fb",
"metadata": {},
"outputs": [],
"source": [
"cv2.createTrackbar(\"Width\", \"Shape Properties\", initial_width, 500, on_change)\n",
"cv2.createTrackbar(\"Height\", \"Shape Properties\", initial_height, 500, on_change)\n",
"cv2.createTrackbar(\"Red\", \"Shape Properties\", initial_color[0], 255, on_change)\n",
"cv2.createTrackbar(\"Green\", \"Shape Properties\", initial_color[1], 255, on_change)\n",
"cv2.createTrackbar(\"Blue\", \"Shape Properties\", initial_color[2], 255, on_change)"
]
},
{
"cell_type": "markdown",
"id": "d9a6e844",
"metadata": {},
"source": [
"get current values"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "e6393858",
"metadata": {},
"outputs": [],
"source": [
"while True:\n",
" width = cv2.getTrackbarPos(\"Width\",\"Shape Properties\")\n",
" height = cv2.getTrackbarPos(\"Height\",\"Shape Properties\")\n",
" red = cv2.getTrackbarPos(\"Red\",\"Shape Properties\")\n",
" green = cv2.getTrackbarPos(\"Green\",\"Shape Properties\")\n",
" blue = cv2.getTrackbarPos(\"Blue\",\"Shape Properties\")\n",
" \n",
" # draw rectangle with dynamically adjusted properties\n",
" shape = (width, height)\n",
" color = (blue,green,red) #openCV uses BGR format\n",
" \n",
" #display the image\n",
" img = cv2.rectangle(image,(10,10),shape,color,-1)\n",
" cv2.imshow(\"Show\", img)\n",
" \n",
" #check for key press\n",
" key = cv2.waitKey(1) & 0xFF\n",
" if key == ord('q'):\n",
" break\n",
"cv2.destroyAllWindows()"
]
},
{
"cell_type": "markdown",
"id": "cc1dd562",
"metadata": {},
"source": [
"# Exp 6"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "95e16bda",
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"img =cv2.imread('/home/sahyadri/Desktop/images.jpeg')\n",
"\n",
"# cv2.imread() takes an image as the input\n",
"h, w, channels = img.shape\n",
"\n",
"half1, half2 = w//2, h//2\n",
"\n",
"up_left = img[:half2, :half1]\n",
"up_right = img[:half2, half1:]\n",
"down_left = img[half2:, :half1]\n",
"down_right = img[half2:, half1:]\n",
"\n",
"cv2.imshow('Original image', img)\n",
"cv2.imshow('Top-left part', up_left)\n",
"cv2.imshow('Top-right part', up_right)\n",
"cv2.imshow('Bottom-left part', down_left)\n",
"cv2.imshow('Bottom-right part', down_right)\n",
"\n",
"# saving all images using cv2.imwrite()\n",
"cv2.imwrite('up_left.jpg', up_left)\n",
"cv2.imwrite('up_right.jpg', up_right)\n",
"cv2.imwrite('down_left.jpg', down_left)\n",
"cv2.imwrite('down_right.jpg', down_right)\n",
"\n",
"cv2.waitKey(0)\n",
"cv2.destroyAllWindows()"
]
},
{
"cell_type": "markdown",
"id": "8772ac20",
"metadata": {},
"source": [
"# Exp 7"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e441b21d",
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"\n",
"# load the image\n",
"image_path = \"/home/sahyadri/Desktop/images.jpeg\"\n",
"img = cv2.imread(image_path)\n",
"\n",
"# convert the image to grayscale\n",
"gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n",
"\n",
"# edge detection\n",
"edges = cv2.Canny(gray, 100,200)\n",
"\n",
"# Texture extraction\n",
"kernel = np.ones((5,5), np.float32)/25 # define a 5x5 averaging kernel\n",
"texture = cv2.filter2D(gray, -1, kernel) # apply the averaging filter\n",
"\n",
"# display the orginal image, edges, and texture\n",
"cv2.imshow(\"Original Image\", img)\n",
"cv2.imshow(\"Edges\", edges)\n",
"cv2.imshow(\"Texture\", texture)\n",
"\n",
"cv2.waitKey(0)\n",
"cv2.destroyAllWindows()"
]
},
{
"cell_type": "markdown",
"id": "96abcde5",
"metadata": {},
"source": [
"# Exp 8"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "c75bff8e",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"import cv2\n",
"\n",
"# load the image\n",
"image_path = \"/home/sahyadri/Desktop/images.jpeg\"\n",
"image = cv2.imread(image_path)\n",
"\n",
"# average blur\n",
"average_blur = cv2.blur(image, (5,5))\n",
"\n",
"# gaussian blur\n",
"gaussian_blur = cv2.GaussianBlur(image,(5,5),0)\n",
"\n",
"# median blur\n",
"median_blur = cv2.medianBlur(image, 5)\n",
"\n",
"# bilateral filter\n",
"bilateral_filter = cv2.bilateralFilter(image, 9, 75, 75)\n",
"\n",
"# display the original and processed images\n",
"cv2.imshow(\"Original Image\", image)\n",
"cv2.imshow(\"Average Blur\", average_blur)\n",
"cv2.imshow(\"Gaussian Blur\", gaussian_blur)\n",
"cv2.imshow(\"Median Blur\", median_blur)\n",
"cv2.imshow(\"Bilateral Filter\", bilateral_filter)\n",
"\n",
"cv2.waitKey(0)\n",
"cv2.destroyAllWindows()"
]
},
{
"cell_type": "markdown",
"id": "f4da64a3",
"metadata": {},
"source": [
"# Exp 9"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "297eb248",
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"\n",
"# read the image\n",
"image = cv2.imread(\"/home/sahyadri/Desktop/images.jpeg\")\n",
"\n",
"#convert the image to grayscale\n",
"gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
"\n",
"#apply Canny edge detection\n",
"edges = cv2.Canny(gray,100,200)\n",
"\n",
"# find contours in the edged image\n",
"contours,_ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n",
"\n",
"# create a mask with the same dimensions as the image\n",
"mask = np.zeros_like(gray)\n",
"\n",
"# draw contours on the mask\n",
"cv2.drawContours(mask,contours, -1, (255), thickness=cv2.FILLED)\n",
"\n",
"# apply the mask to the original image\n",
"segmented_image = cv2.bitwise_and(image,image,mask=mask)\n",
"\n",
"# display the segemented image\n",
"cv2.imshow(\"Original Image\", image)\n",
"cv2.imshow(\"Segmented Image\", segmented_image)\n",
"\n",
"cv2.waitKey(0)\n",
"cv2.destroyAllWindows()"
]
},
{
"cell_type": "markdown",
"id": "b729fba2",
"metadata": {},
"source": [
"# Playing Around: Finding edges in a blurred image"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "724206b3",
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"\n",
"# load the image\n",
"image_path = \"/home/sahyadri/Desktop/images.jpeg\"\n",
"image = cv2.imread(image_path)\n",
"\n",
"cv2.imshow(\"Original Image\", image)\n",
"\n",
"# blur the image\n",
"blurs = [(\"Average Blur\", cv2.blur(image, (7,7))), # Only one to blur edges\n",
" (\"Gaussian Blur\", cv2.GaussianBlur(image,(5,5),0)), # Preserves edges well\n",
" (\"Median Blur\", cv2.medianBlur(image, 5)), # Only removes noise\n",
" (\"Bilateral Filter\", cv2.bilateralFilter(image, 9, 75, 75))]# Preserves edged and smooths only selective parts\n",
"\n",
"for title, blurred_image in blurs:\n",
" # convert the blur to grayscale\n",
" gray = cv2.cvtColor(blurred_image, cv2.COLOR_BGR2GRAY)\n",
" \n",
" # edge detection\n",
" edges = cv2.Canny(gray, 100,200)\n",
" \n",
" # Texture extraction\n",
" kernel = np.ones((5,5), np.float32)/25 # define a 5x5 averaging kernel\n",
" texture = cv2.filter2D(gray, -1, kernel) # apply the averaging filter\n",
" \n",
" # display the edges, and texture\n",
" cv2.imshow(title, blurred_image)\n",
" cv2.imshow(f\"{title}- Gray Image\",gray)\n",
" cv2.imshow(f\"{title}- Edges\", edges)\n",
" cv2.imshow(f\"{title}- Texture\", texture)\n",
"\n",
"cv2.waitKey(0)\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.8.8"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment