.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/pixel/plot_0_image.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_pixel_plot_0_image.py: Image, 2D{3} datasets ^^^^^^^^^^^^^^^^^^^^^ .. GENERATED FROM PYTHON SOURCE LINES 6-14 The 2D{3} dataset is two dimensional, :math:`d=2`, with a single three-component dependent variable, :math:`p=3`. A common example from this subset is perhaps the RGB image dataset. An RGB image dataset has two spatial dimensions and one dependent variable with three components corresponding to the red, green, and blue color intensities. The following is an example of an RGB image dataset. .. GENERATED FROM PYTHON SOURCE LINES 14-20 .. code-block:: Python import csdmpy as cp filename = "https://www.ssnmr.org/sites/default/files/CSDM/image/raccoon_image.csdf" ImageData = cp.load(filename) print(ImageData.data_structure) .. rst-class:: sphx-glr-script-out .. code-block:: none { "csdm": { "version": "1.0", "read_only": true, "timestamp": "2016-03-12T16:41:00Z", "tags": [ "racoon", "image", "Judy Weggelaar" ], "description": "An RBG image of a raccoon face.", "dimensions": [ { "type": "linear", "count": 1024, "increment": "1.0", "label": "horizontal index" }, { "type": "linear", "count": 768, "increment": "1.0", "label": "vertical index" } ], "dependent_variables": [ { "type": "internal", "name": "raccoon", "numeric_type": "uint8", "quantity_type": "pixel_3", "component_labels": [ "red", "green", "blue" ], "components": [ [ "121, 138, ..., 119, 118" ], [ "112, 129, ..., 155, 154" ], [ "131, 148, ..., 93, 92" ] ] } ] } } .. GENERATED FROM PYTHON SOURCE LINES 21-23 The tuple of the dimension and dependent variable instances from ``ImageData`` instance are .. GENERATED FROM PYTHON SOURCE LINES 23-26 .. code-block:: Python x = ImageData.dimensions y = ImageData.dependent_variables .. GENERATED FROM PYTHON SOURCE LINES 27-29 respectively. There are two dimensions, and the coordinates along each dimension are .. GENERATED FROM PYTHON SOURCE LINES 29-31 .. code-block:: Python print("x0 =", x[0].coordinates[:10]) .. rst-class:: sphx-glr-script-out .. code-block:: none x0 = [0. 1. 2. 3. 4. 5. 6. 7. 8. 9.] .. GENERATED FROM PYTHON SOURCE LINES 32-34 .. code-block:: Python print("x1 =", x[1].coordinates[:10]) .. rst-class:: sphx-glr-script-out .. code-block:: none x1 = [0. 1. 2. 3. 4. 5. 6. 7. 8. 9.] .. GENERATED FROM PYTHON SOURCE LINES 35-36 respectively, where only first ten coordinates along each dimension is displayed. .. GENERATED FROM PYTHON SOURCE LINES 38-41 The dependent variable is the image data, as also seen from the :attr:`~csdmpy.DependentVariable.quantity_type` attribute of the corresponding :ref:`dv_api` instance. .. GENERATED FROM PYTHON SOURCE LINES 41-44 .. code-block:: Python print(y[0].quantity_type) .. rst-class:: sphx-glr-script-out .. code-block:: none pixel_3 .. GENERATED FROM PYTHON SOURCE LINES 45-47 From the value `pixel_3`, `pixel` indicates a pixel data, while `3` indicates the number of pixel components. .. GENERATED FROM PYTHON SOURCE LINES 49-53 As usual, the components of the dependent variable are accessed through the :attr:`~csdmpy.DependentVariable.components` attribute. To access the individual components, use the appropriate array indexing. For example, .. GENERATED FROM PYTHON SOURCE LINES 53-56 .. code-block:: Python print(y[0].components[0]) .. rst-class:: sphx-glr-script-out .. code-block:: none [[121 138 153 ... 119 131 139] [ 89 110 130 ... 118 134 146] [ 73 94 115 ... 117 133 144] ... [ 87 94 107 ... 120 119 119] [ 85 95 112 ... 121 120 120] [ 85 97 111 ... 120 119 118]] .. GENERATED FROM PYTHON SOURCE LINES 57-63 will return an array with the first component of all data values. In this case, the components correspond to the red color intensity, also indicated by the corresponding component label. The label corresponding to the component array is accessed through the :attr:`~csdmpy.DependentVariable.component_labels` attribute with appropriate indexing, that is .. GENERATED FROM PYTHON SOURCE LINES 63-65 .. code-block:: Python print(y[0].component_labels[0]) .. rst-class:: sphx-glr-script-out .. code-block:: none red .. GENERATED FROM PYTHON SOURCE LINES 66-69 To avoid displaying larger output, as an example, we print the shape of each component array (using Numpy array's `shape` attribute) for the three components along with their respective labels. .. GENERATED FROM PYTHON SOURCE LINES 71-73 .. code-block:: Python print(y[0].component_labels[0], y[0].components[0].shape) .. rst-class:: sphx-glr-script-out .. code-block:: none red (768, 1024) .. GENERATED FROM PYTHON SOURCE LINES 74-76 .. code-block:: Python print(y[0].component_labels[1], y[0].components[1].shape) .. rst-class:: sphx-glr-script-out .. code-block:: none green (768, 1024) .. GENERATED FROM PYTHON SOURCE LINES 77-79 .. code-block:: Python print(y[0].component_labels[2], y[0].components[2].shape) .. rst-class:: sphx-glr-script-out .. code-block:: none blue (768, 1024) .. GENERATED FROM PYTHON SOURCE LINES 80-82 The shape (768, 1024) corresponds to the number of points from the each dimension instances. .. GENERATED FROM PYTHON SOURCE LINES 84-90 .. note:: In this example, since there is only one dependent variable, the index of `y` is set to zero, which is ``y[0]``. The indices for the :attr:`~csdmpy.DependentVariable.components` and the :attr:`~csdmpy.DependentVariable.component_labels`, on the other hand, spans through the number of components. .. GENERATED FROM PYTHON SOURCE LINES 92-93 Now, to visualize the dataset as an RGB image, .. GENERATED FROM PYTHON SOURCE LINES 93-100 .. code-block:: Python import matplotlib.pyplot as plt ax = plt.subplot(projection="csdm") ax.imshow(ImageData, origin="upper") plt.tight_layout() plt.show() .. image-sg:: /auto_examples/pixel/images/sphx_glr_plot_0_image_001.png :alt: raccoon :srcset: /auto_examples/pixel/images/sphx_glr_plot_0_image_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.597 seconds) .. _sphx_glr_download_auto_examples_pixel_plot_0_image.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_0_image.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_0_image.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_