Skip to content

Commit

Permalink
Updated tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
andres-patrignani committed Apr 10, 2024
1 parent 7230e70 commit fc53dce
Show file tree
Hide file tree
Showing 10 changed files with 151 additions and 140 deletions.
219 changes: 111 additions & 108 deletions docs/exercises/count_seeds.html

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/search.json
Original file line number Diff line number Diff line change
Expand Up @@ -2188,7 +2188,7 @@
"href": "exercises/count_seeds.html",
"title": "76  Count seeds",
"section": "",
"text": "In this exercise we will learn how to use image analysis to identify and count seeds from an image collected with a mobile device.\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n\nfrom skimage.filters import threshold_otsu\nfrom skimage.morphology import area_opening, disk, binary_closing\nfrom skimage.measure import find_contours, label\nfrom skimage.color import rgb2gray, label2rgb\n\n\n# Read color image\nimage_rgb = mpimg.imread('../datasets/images/rice_seeds.jpg')\n\n\n# Convert image to grayscale\nimage_gray = rgb2gray(image_rgb)\n\n\n# Visualize rgb and grayscale images\nplt.figure(figsize=(8,4))\n\nplt.subplot(1,2,1)\nplt.imshow(image_rgb)\nplt.axis('off')\nplt.title('RGB')\nplt.tight_layout()\n\nplt.subplot(1,2,2)\nplt.imshow(image_gray, cmap='gray')\nplt.axis('off')\nplt.title('Gray scale')\nplt.tight_layout()\n\nplt.show()\n\n\n\n\n\n# Segment seeds using a global automated threshold\nglobal_thresh = threshold_otsu(image_gray)\nimage_binary = image_gray > global_thresh\n\n\n# Display classified seeds and grayscale threshold\nplt.figure(figsize=(12,4))\n\nplt.subplot(1,3,1)\nplt.imshow(image_gray, cmap='gray')\nplt.axis('off')\nplt.title('Original grayscale')\nplt.tight_layout()\n\nplt.subplot(1,3,2)\nplt.hist(image_gray.ravel(), bins=256)\nplt.axvline(global_thresh, color='r', linestyle='--')\nplt.title('Otsu threshold')\nplt.xlabel('Grayscale')\nplt.ylabel('Counts')\n\nplt.subplot(1,3,3)\nplt.imshow(image_binary, cmap='gray')\nplt.axis('off')\nplt.title('Binary')\nplt.tight_layout()\n\nplt.show()\n\n\n\n\n\n# Invert image\nimage_binary = ~image_binary\n\n\n# Remove small areas (remove noise)\nimage_binary = area_opening(image_binary, area_threshold=1000, connectivity=2)\n\n\n# Closing (performs a dilation followed by an erosion. Connect small bright patches)\nimage_binary = binary_closing(image_binary, disk(5))\n\n# Let's inspect the structuring element\nprint(disk(5))\n\n[[0 0 0 0 0 1 0 0 0 0 0]\n [0 0 1 1 1 1 1 1 1 0 0]\n [0 1 1 1 1 1 1 1 1 1 0]\n [0 1 1 1 1 1 1 1 1 1 0]\n [0 1 1 1 1 1 1 1 1 1 0]\n [1 1 1 1 1 1 1 1 1 1 1]\n [0 1 1 1 1 1 1 1 1 1 0]\n [0 1 1 1 1 1 1 1 1 1 0]\n [0 1 1 1 1 1 1 1 1 1 0]\n [0 0 1 1 1 1 1 1 1 0 0]\n [0 0 0 0 0 1 0 0 0 0 0]]\n\n\n\n# Display inverted and denoised binary image\nplt.figure(figsize=(4,4))\n\nplt.imshow(image_binary, cmap='gray')\nplt.axis('off')\nplt.title('Binary')\nplt.tight_layout()\n\nplt.show()\n\n\n\n\n\n# Identify seed boundaries\ncontours = find_contours(image_binary, 0)\n\n# Print number of seeds in image\nprint('Image contains',len(contours),'seeds')\n\nImage contains 41 seeds\n\n\n\n# Plot seed contours\nplt.figure(figsize=(4,4))\nplt.imshow(image_binary, cmap='gray')\nplt.axis('off')\nplt.tight_layout()\n\nfor contour in contours:\n plt.plot(contour[:, 1], contour[:, 0], '-r', linewidth=1.5)\n \n\n\n\n\n\n# Label image regions\nlabel_image = label(image_binary)\nimage_label_overlay = label2rgb(label_image, image=image_binary)\n\n\n# Display image regions on top of original image\nplt.figure(figsize=(4, 4))\nplt.imshow(image_label_overlay)\nplt.tight_layout()\nplt.axis('off')\nplt.show()\n\n\n\n\n\n# Display contour for a single seed\nplt.figure(figsize=(12, 8))\n\nfor seed in range(36):\n plt.subplot(6,6,seed+1)\n plt.plot(contours[seed][:, 1], contours[seed][:, 0], '-r', linewidth=2)\n plt.tight_layout()\nplt.show()"
"text": "In this exercise we will learn how to use image analysis to identify and count seeds from an image collected with a mobile device.\n\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n\nfrom skimage.filters import threshold_otsu\nfrom skimage.morphology import area_opening, disk, binary_closing\nfrom skimage.measure import find_contours, label\nfrom skimage.color import rgb2gray, label2rgb\n\n\n# Read color image\nimage_rgb = mpimg.imread('../datasets/images/rice_seeds.jpg')\n\n\n# Inspect image size\nprint(image_rgb.shape)\n\n(1622, 1563, 3)\n\n\n\n# Convert image to grayscale\nimage_gray = rgb2gray(image_rgb)\n\n\n# Visualize rgb and grayscale images\nplt.figure(figsize=(8,4))\n\nplt.subplot(1,2,1)\nplt.imshow(image_rgb)\nplt.axis('off')\nplt.title('RGB')\nplt.tight_layout()\n\nplt.subplot(1,2,2)\nplt.imshow(image_gray, cmap='gray')\nplt.axis('off')\nplt.title('Gray scale')\nplt.tight_layout()\n\nplt.show()\n\n\n\n\n\n# Segment seeds using a global automated threshold\nglobal_thresh = threshold_otsu(image_gray)\nimage_binary = image_gray < global_thresh\n\n\n# Display classified seeds and grayscale threshold\nplt.figure(figsize=(12,4))\n\nplt.subplot(1,3,1)\nplt.imshow(image_gray, cmap='gray')\nplt.axis('off')\nplt.title('Original grayscale')\nplt.tight_layout()\n\nplt.subplot(1,3,2)\nplt.hist(image_gray.ravel(), bins=256)\nplt.axvline(global_thresh, color='r', linestyle='--')\nplt.title('Otsu threshold')\nplt.xlabel('Grayscale')\nplt.ylabel('Counts')\n\nplt.subplot(1,3,3)\nplt.imshow(image_binary, cmap='gray')\nplt.axis('off')\nplt.title('Binary')\nplt.tight_layout()\n\nplt.show()\n\n\n\n\n\n# Remove small areas (remove noise)\nimage_binary = area_opening(image_binary, area_threshold=1000, connectivity=2)\n\n\n# Closing (performs a dilation followed by an erosion. Connect small bright patches)\nimage_binary = binary_closing(image_binary, disk(5))\n\n# Let's inspect the structuring element\nprint(disk(5))\n\n[[0 0 0 0 0 1 0 0 0 0 0]\n [0 0 1 1 1 1 1 1 1 0 0]\n [0 1 1 1 1 1 1 1 1 1 0]\n [0 1 1 1 1 1 1 1 1 1 0]\n [0 1 1 1 1 1 1 1 1 1 0]\n [1 1 1 1 1 1 1 1 1 1 1]\n [0 1 1 1 1 1 1 1 1 1 0]\n [0 1 1 1 1 1 1 1 1 1 0]\n [0 1 1 1 1 1 1 1 1 1 0]\n [0 0 1 1 1 1 1 1 1 0 0]\n [0 0 0 0 0 1 0 0 0 0 0]]\n\n\n\n# Display inverted and denoised binary image\nplt.figure(figsize=(4,4))\n\nplt.imshow(image_binary, cmap='gray')\nplt.axis('off')\nplt.title('Binary')\nplt.tight_layout()\n\nplt.show()\n\n\n\n\n\n# Identify seed boundaries\ncontours = find_contours(image_binary, 0)\n\n# Print number of seeds in image\nprint('Image contains',len(contours),'seeds')\n\nImage contains 41 seeds\n\n\n\n# Plot seed contours\nplt.figure(figsize=(4,4))\nplt.imshow(image_binary, cmap='gray')\nplt.axis('off')\nplt.tight_layout()\n\nfor contour in contours:\n plt.plot(contour[:, 1], contour[:, 0], '-r', linewidth=1.5)\n \n\n\n\n\n\n# Label image regions\nlabel_image = label(image_binary)\nimage_label_overlay = label2rgb(label_image, image=image_binary)\n\n\n# Display image regions on top of original image\nplt.figure(figsize=(4, 4))\nplt.imshow(image_label_overlay)\nplt.tight_layout()\nplt.axis('off')\nplt.show()\n\n\n\n\n\n# Display contour for a single seed\nplt.figure(figsize=(12, 8))\n\nfor seed in range(36):\n plt.subplot(6,6,seed+1)\n plt.plot(contours[seed][:, 1], contours[seed][:, 0], '-r', linewidth=2)\n plt.tight_layout()\nplt.show()"
},
{
"objectID": "exercises/canopy_cover.html#read-and-process-a-single-image",
Expand Down
70 changes: 39 additions & 31 deletions exercises/count_seeds.ipynb

Large diffs are not rendered by default.

0 comments on commit fc53dce

Please sign in to comment.