Skip to content

Commit

Permalink
updating hw4
Browse files Browse the repository at this point in the history
  • Loading branch information
mariakakis committed Dec 3, 2023
1 parent a1432ff commit 11e49f1
Show file tree
Hide file tree
Showing 4 changed files with 1,294 additions and 3 deletions.
4 changes: 2 additions & 2 deletions _lecturesfall/4_images.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ materials:
- type: "colab"
url: https://colab.research.google.com/github/C4M-UofT/C4M-UofT.github.io/blob/master/lectures/fall/4_images/4g - Outlines.ipynb
assignment:
text: "Materials in progress"
text: "HW 4"
due_date: 2024-01-17 12:00 PM
submission_link: https://q.utoronto.ca/courses/342394/assignments/1175771
files:
- type: "colab"
url: TBD
url: https://colab.research.google.com/github/C4M-UofT/C4M-UofT.github.io/blob/master/homeworks/HW4.ipynb
---
379 changes: 379 additions & 0 deletions homeworks/HW4 Answers.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,379 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"collapsed_sections": [
"B6v86VjQmIrK",
"7XJJtyIRQgSQ",
"ckicG5ai2Mbe"
]
},
"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.2"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "B6v86VjQmIrK"
},
"source": [
"## Important: Run this code cell each time you start a new session!"
]
},
{
"cell_type": "code",
"source": [
"!pip install numpy\n",
"!pip install pandas\n",
"!pip install matplotlib\n",
"!pip install os\n",
"!pip install opencv-python\n",
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"import os\n",
"import cv2"
],
"metadata": {
"id": "jrO0X1ZMxMN5"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"!wget -Ncnp https://physionet.org/files/images/1.0.0/E1154S7I000.png\n",
"!wget -Ncnp https://physionet.org/files/images/1.0.0/E1154S7I024.png"
],
"metadata": {
"id": "mD9SEZljuFVA"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import shutil\n",
"import os\n",
"orig_file = os.path.join('E1154S7I000.png')\n",
"os.rename(orig_file, 'bw_mra_single.png')\n",
"orig_file = os.path.join('E1154S7I024.png')\n",
"os.rename(orig_file, 'bw_mra_multiple.png')"
],
"metadata": {
"id": "o8I8wOuMuPOn"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "IhQvi7z1v6rm"
},
"source": [
"# Instructions"
]
},
{
"cell_type": "markdown",
"source": [
"To get full credit for this assignment, we should be able to run your entire notbook from start to finish without any errors. You can check this yourself by selecting \"Runtime\" > \"Run all\" in the Google Colab menu."
],
"metadata": {
"id": "FmZhTZTraQGQ"
}
},
{
"cell_type": "markdown",
"source": [
"# Exercise 1: Working with Basic Shapes and Colors"
],
"metadata": {
"id": "7XJJtyIRQgSQ"
}
},
{
"cell_type": "markdown",
"source": [
"This exercise will require drawing shapes on a blank image created using the following function:"
],
"metadata": {
"id": "xtwly1OhRpGP"
}
},
{
"cell_type": "code",
"source": [
"def create_black_img():\n",
" return np.zeros((300,300,3), np.uint8)"
],
"metadata": {
"id": "xChw-dSJQk_u"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**(Part 1)** Write code that draws a red circle with the following properties:\n",
"* Center at position `x = 250, y = 100`\n",
"* Radius of `25`\n",
"* Outline thickness of `3`"
],
"metadata": {
"id": "7gEjqGWZR9r4"
}
},
{
"cell_type": "code",
"source": [
"img = create_black_img()\n",
"cv2.circle(img, center=(250, 100), radius=25,\n",
" color=(255, 0, 0), thickness=3)\n",
"plt.figure(figsize=(3, 3))\n",
"plt.imshow(img)\n",
"plt.show()"
],
"metadata": {
"id": "DFNaewqkSvUh"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**(Part 2)** Write code that draws a filled magenta rectangle with the following properties:\n",
"* Top left corner at position `x = 50, y = 100`\n",
"* Height of `50`\n",
"* Width of `100`\n"
],
"metadata": {
"id": "umOgE7tZQ4IZ"
}
},
{
"cell_type": "code",
"source": [
"img = create_black_img()\n",
"top_left = (50, 100)\n",
"h = 50\n",
"w = 100\n",
"bottom_right = (top_left[0]+w, top_left[1]+h)\n",
"cv2.rectangle(img, pt1=top_left, pt2=bottom_right,\n",
" color=(255, 0, 255), thickness=-1)\n",
"plt.figure(figsize=(3, 3))\n",
"plt.imshow(img)\n",
"plt.show()"
],
"metadata": {
"id": "jjQT41bURWLg"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**(Part 3)** Write code that draws a filled white ellipse with the following properties:\n",
"* Center at position `x = 100, y = 200`\n",
"* Major axis of `100`\n",
"* Minor axis of `25`\n",
"* No rotation"
],
"metadata": {
"id": "Z0rRrjwrTUDc"
}
},
{
"cell_type": "code",
"source": [
"img = create_black_img()\n",
"cv2.ellipse(img, center=(100, 200), axes=(100, 25), angle=0,\n",
" startAngle=0, endAngle=360,\n",
" color=(255, 255, 255), thickness=-1)\n",
"plt.figure(figsize=(3, 3))\n",
"plt.imshow(img)\n",
"plt.show()"
],
"metadata": {
"id": "d62CLFCsTvr0"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# Exercise 2: Identifying a Region of Interest in an MRA Image"
],
"metadata": {
"id": "ckicG5ai2Mbe"
}
},
{
"cell_type": "markdown",
"source": [
"This exercise will revolve around a magnetic resonance angiography (MRA) image showing a coronal slice from an anteroposterior position within the torso. We have already taken the liberty of applying a binary threshold to reveal the prominent regions of the MRA image."
],
"metadata": {
"id": "0DWUqG9Z3fJG"
}
},
{
"cell_type": "code",
"source": [
"img = cv2.imread('bw_mra_multiple.png')\n",
"img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)\n",
"_, img_thresh = cv2.threshold(img_gray, 200, 255, cv2.THRESH_BINARY)\n",
"\n",
"plt.figure(figsize=(6, 3))\n",
"plt.subplot(1, 2, 1), plt.title('Original Image')\n",
"plt.imshow(img)\n",
"plt.subplot(1, 2, 2), plt.title('Thresholded Image')\n",
"plt.imshow(img_thresh, cmap='gray')\n",
"plt.show()"
],
"metadata": {
"id": "Xm0Kq1yM3fJH"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Notice that the binary image still contains numerous regions: three prominent ones along the center and right side of the image, and many smaller ones along the left side and the periphery. Our goal is to measure the radius of the round blob in the upper half of the image."
],
"metadata": {
"id": "YX8AR1fqMSXK"
}
},
{
"cell_type": "markdown",
"source": [
"**(Part 1)** Write code that collects a list of the contours corresponding to the three largest regions in the binary image. There are multiple ways of doing this, but here are a couple of suggestions:\n",
"1. Sort all of the contours by their size and then pick the largest three ones\n",
"2. Iterate through the contours and keep the ones whose size exceed a minimum threshold defined by you\n",
"\n",
"To confirm that this is working properly, draw green outlines around these regions using the code provided."
],
"metadata": {
"id": "sH4HFMfs6QBi"
}
},
{
"cell_type": "code",
"source": [
"# Locate the contours\n",
"cnts, hierarchy = cv2.findContours(img_thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)\n",
"\n",
"# Only keep the contours that pass the size check\n",
"large_cnts = []\n",
"for cnt in cnts:\n",
" cnt_area = cv2.contourArea(cnt)\n",
" if 500 < cnt_area:\n",
" large_cnts.append(cnt)\n",
"\n",
"# Draw the outlines on the image\n",
"output_img = img.copy()\n",
"for cnt in large_cnts:\n",
" cv2.drawContours(output_img, [cnt], -1, (0, 255, 0), 3)\n",
"\n",
"# Show the result\n",
"plt.figure(figsize=(6, 3))\n",
"plt.imshow(output_img, cmap='gray')\n",
"plt.show()"
],
"metadata": {
"id": "mBOeY12sJu4L"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**(Part 2)** Write code that identifies the most circular contour out of the three regions that were selected in the previous problem. Draw a green outline around it using the code provided.\n",
"\n",
"*Hint:* Think about the contour properties we covered during the lecture. A circle is a special kind of ellipse where the major and minor axes are equal."
],
"metadata": {
"id": "_06qfRD9Jvq_"
}
},
{
"cell_type": "code",
"source": [
"# Iterate through all of the contours\n",
"circular_cnt = None\n",
"min_axis_ratio = 1e6\n",
"for cnt in large_cnts:\n",
" # Calculate the \"roundness\" using the ratio of the major and minor axes\n",
" cnt_ellipse = cv2.fitEllipse(cnt)\n",
" cnt_axes = cnt_ellipse[1]\n",
" cnt_axis_ratio = cnt_axes[0]/cnt_axes[1]\n",
" # Save the contour if this one is more round than past ones\n",
" if cnt_axis_ratio < min_axis_ratio:\n",
" circular_cnt = cnt\n",
"\n",
"# Draw the outlines on the image\n",
"output_img = img.copy()\n",
"cv2.drawContours(output_img, [circular_cnt], -1, (0, 255, 0), 3)\n",
"\n",
"# Show the result\n",
"plt.figure(figsize=(6, 3))\n",
"plt.imshow(output_img, cmap='gray')\n",
"plt.show()"
],
"metadata": {
"id": "u4i6qrHDJwIJ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**(Part 3)** Write code that calculates the radius of the circular region that you have identified in pixels."
],
"metadata": {
"id": "cnNmSd5RKK-p"
}
},
{
"cell_type": "code",
"source": [
"circle_outline = cv2.minEnclosingCircle(circular_cnt)\n",
"circle_outline[1]"
],
"metadata": {
"id": "0Qmwz3dYKN7s"
},
"execution_count": null,
"outputs": []
}
]
}

0 comments on commit 11e49f1

Please sign in to comment.