# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/lmcanavals/dip/blob/main/pdi_0101_rgb2gray.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="kP-PlUh3uLNq"
# # RGB to Gray

# + id="m-dc45rb325J"
import cv2
import numpy as np
import matplotlib.pyplot as plt

# + colab={"base_uri": "https://localhost:8080/", "height": 35} id="kOiJ9kj05SwL" outputId="e6bdf5ca-d265-4911-ac98-481e637b6a72"
cv2.__version__

# + colab={"base_uri": "https://localhost:8080/", "height": 313} id="iHmn5-hV4miO" outputId="8224ece8-ac98-4aa6-8ce7-995a19816f3f"
im = cv2.imread("mafalda.jpg")
print(im[300, 600])
im = im[:,:,::-1]
print(im[300, 600])
plt.imshow(im)

# + colab={"base_uri": "https://localhost:8080/"} id="NsdXVuw64zgv" outputId="bfc16567-4082-4ce5-ceaa-6bfe6e4411cb"
im.shape


# + [markdown] id="_oMebm2h81NW"
# # RGB to Gray
#
# $color = 0.2989 * r + 0.5870 * g + 0.1140 * b$

# + id="9cLTgsyM9Jlh"
def rgb2grey1(im):
  return (im[:,:,0] + im[:,:,1] + im[:,:,2]) // 3
def rgb2grey2(im):
  return np.floor(0.2989 * im[:,:,0] + 0.587 * im[:,:,1] + 0.114 * im[:,:,2])


# + colab={"base_uri": "https://localhost:8080/", "height": 258} id="7YqYSUTt9zSw" outputId="28bd3fc7-ed5e-434f-ac2e-46b2e64e972e"
plt.imshow(rgb2grey1(im), cmap="Greys_r")

# + colab={"base_uri": "https://localhost:8080/", "height": 258} id="huOlefMk-PlA" outputId="df30e83c-dd3b-4b59-c373-ee692058b93c"
plt.imshow(rgb2grey2(im), cmap="Greys_r")

# + colab={"base_uri": "https://localhost:8080/", "height": 287} id="AVOZSBFr_pbT" outputId="95cff07f-04c4-4c10-f2b9-4e5040ecc3b8"
merkel = cv2.imread("merkel.jpg")
merkel = merkel[:,:,::-1]
plt.imshow(merkel)

# + colab={"base_uri": "https://localhost:8080/", "height": 287} id="ZUVSPZya_0vA" outputId="c90b91de-cd32-4538-95b8-b62863321feb"
plt.imshow(rgb2grey1(merkel), cmap="Greys_r")

# + colab={"base_uri": "https://localhost:8080/", "height": 287} id="CymO1B_8_7Wq" outputId="5dff8b11-a6d0-4f51-d847-40cbbe329374"
plt.imshow(rgb2grey2(merkel), cmap="Greys_r")


# + id="856_42Ke_9Jp"
def hist(grayim):
  h = np.zeros((256))
  H, W = grayim.shape
  for i in range(H):
    for j in range(W):
      h[math.floor(grayim[i, j])] += 1

  return h


# + id="RlLaVl52Ag1k"
grayim1 = rgb2grey1(im)
grayim2 = rgb2grey2(im)
h1 = hist(grayim1)
h2 = hist(grayim2)

# + colab={"base_uri": "https://localhost:8080/", "height": 283} id="QuhfeetHBsvo" outputId="048fce3e-70a5-41d8-ec0e-7d5965ad0a16"
plt.plot(h1)

# + colab={"base_uri": "https://localhost:8080/", "height": 283} id="LeAI31_gCAtp" outputId="8fea2075-a98d-4cb1-abf2-1f87256d6268"
plt.plot(h2)

# + colab={"base_uri": "https://localhost:8080/", "height": 258} id="aZEIpo7MDOsA" outputId="69900987-9709-40f6-ecc0-15251321589b"
plt.imshow(grayim1, cmap="Greys_r")

# + colab={"base_uri": "https://localhost:8080/", "height": 258} id="xqpUxRXKDgO3" outputId="9578fdd7-7fd1-4102-e424-5d339f40d708"
plt.imshow(grayim2, cmap="Greys_r")

# + id="N2NtZ8EdDiQ5"


StarcoderdataJupyterScriptsDedupFiltered