Skip to content

Instantly share code, notes, and snippets.

@ce-dric
Created March 4, 2024 04:23
Show Gist options
  • Save ce-dric/be3407e9d00e73651dc0f16f59ed5cbb to your computer and use it in GitHub Desktop.
Save ce-dric/be3407e9d00e73651dc0f16f59ed5cbb to your computer and use it in GitHub Desktop.
extract line and plot
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"private_outputs": true,
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"source": [
"!wget https://upload.wikimedia.org/wikipedia/commons/f/fa/Grayscale_8bits_palette_sample_image.png"
],
"metadata": {
"id": "TQwyJ7Wbps1b"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import cv2\n",
"import numpy as np\n",
"from matplotlib import pyplot as plt\n",
"\n",
"# Load the image in grayscale\n",
"img = cv2.imread('Grayscale_8bits_palette_sample_image.png')\n",
"plt.imshow(img)"
],
"metadata": {
"id": "_5PsleQgpgoz"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Extract a line of pixels (from (0, 10) to (0, 60))\n",
"line_pixels = img[50, 10:60]\n",
"\n",
"plt.figure(figsize=(10, 4))\n",
"plt.plot(line_pixels, marker='o')\n",
"plt.title('Pixel Intensities')\n",
"plt.xlabel('Pixel Position')\n",
"plt.ylabel('Intensity')\n",
"plt.grid(True)\n",
"plt.show()\n",
"\n",
"img_highlighted = img.copy()\n",
"img_highlighted[50, 10:60] = 255\n",
"\n",
"plt.figure(figsize=(6, 6))\n",
"plt.imshow(img_highlighted, cmap='gray')\n",
"plt.title('Image with Highlighted Line')\n",
"plt.axis('off')\n",
"plt.show()\n",
"\n"
],
"metadata": {
"id": "aZIAwLDBpqgU"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "SVPJ1IIFsNlx"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment