#### Piecewise Linear Transform

{
"cells": [
{
"cell_type": "markdown",
"id": "2f62a3e1",
"source": [
"# Piecewise Linear Transform"
]
},
{
"cell_type": "markdown",
"id": "bd9e1408",
"source": [
"Piece-wise Linear Transformation is type of gray level transformation that is used for image enhancement.\n",
"It is a spatial domain method.\n",
"It is used for manipulation of an image so that the result is more suitable than the original for a specific application."
]
},
{
"cell_type": "markdown",
"id": "d4706d72",
"source": [
"Three types of piece-wise linear transformation: Contrast stretching, Gray level slicing, Bit plane slicing\n",
"\n",
"Contrast is the difference between highest gray level and the lowest gray level of an image."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "e42c813d",
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"from matplotlib import pyplot as plt\n",
"import pylab # this allows you to control figure size \n",
"pylab.rcParams['figure.figsize'] = (8.0, 6.0) # this controls figure size in the notebook"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "866334db",
"outputs": [
{
"data": {
"text/plain": [
"<Figure size 576x432 with 1 Axes>"
]
},
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n",
"\n",
"r1 = 100\n",
"s1 = 50\n",
"r2=180\n",
"s2=200"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "d7e9bd85",
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.image.AxesImage at 0x2197f1b9220>"
]
},
"execution_count": 3,
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"<Figure size 576x432 with 1 Axes>"
]
},
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"def pixel_value(img, r1,s1,r2, s2):\n",
"         if (0 <=img and img<=r1):\n",
"             return (s1/r1)*img\n",
"         elif (r1 <= img and img <=r2):\n",
"             return ((s2-s1)/(r2-r1))* (img-r1)+s1\n",
"         else:\n",
"             return ((255-s2)/(255-r2))* (img-r2)+s2\n",
"picture_value = np.vectorize(pixel_value)\n",
"\n",
"Piecewise_value = picture_value(image, r1,s1,r2, s2)\n",
"Piecewise_value = Piecewise_value.astype(np.uint8)\n",
"plt.imshow(cv2.cvtColor(Piecewise_value, cv2.COLOR_BGR2RGB))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "2103ea04",
"outputs": [
{
"data": {
"text/plain": [
"<Figure size 576x432 with 1 Axes>"
]
},
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"x = np.arange(0,255)\n",
"y = picture_value(x, r1,s1,r2, s2)\n",
"\n",
"plt.plot(x,x,'k--')\n",
"plt.plot(x,y, 'r')\n",
"plt.xlim([0,255])\n",
"plt.ylim([0,255])\n",
"plt.grid()\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "27c93479",
"outputs": [
{
"data": {
"text/plain": [
"<Figure size 576x432 with 1 Axes>"
]
},
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))\n",
"\n",
"r1 = 100\n",
"s1 = 100\n",
"r2=150\n",
"s2=150\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "7c5277cf",
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.image.AxesImage at 0x2197c418310>"
]
},
"execution_count": 6,
"output_type": "execute_result"
},
{
"data": {
"text/plain": [
"<Figure size 576x432 with 1 Axes>"
]
},
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"def pixel_value(img, r1,s1,r2, s2):\n",
"         if (0 <=img and img<=r1):\n",
"             return (s1/r1)*img\n",
"         elif (r1 <= img and img <=r2):\n",
"             return 255 # 0 \n",
"         else:\n",
"             return ((255-s2)/(255-r2))* (img-r2)+s2\n",
"picture_value = np.vectorize(pixel_value)\n",
"\n",
"Piecewise_value = picture_value(image, r1,s1,r2, s2)\n",
"Piecewise_value = Piecewise_value.astype(np.uint8)\n",
"plt.imshow(cv2.cvtColor(Piecewise_value, cv2.COLOR_BGR2RGB))\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "0ddd5180",
"outputs": [
{
"data": {
"text/plain": [
"<Figure size 576x432 with 1 Axes>"
]
},
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"x = np.arange(0,255)\n",
"y = picture_value(x, r1,s1,r2, s2)\n",
"\n",
"plt.plot(x,x,'k--')\n",
"plt.plot(x,y,'r')\n",
"plt.xlim([0,255])\n",
"plt.ylim([0,255])\n",
"plt.grid()\n",
"plt.show()\n"
]
}
],
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}


# Piecewise Linear Transform

Piece-wise Linear Transformation is type of gray level transformation that is used for image enhancement. It is a spatial domain method. It is used for manipulation of an image so that the result is more suitable than the original for a specific application.

Three types of piece-wise linear transformation: Contrast stretching, Gray level slicing, Bit plane slicing

Contrast is the difference between highest gray level and the lowest gray level of an image.

import cv2
import numpy as np
from matplotlib import pyplot as plt
import pylab # this allows you to control figure size
pylab.rcParams['figure.figsize'] = (8.0, 6.0) # this controls figure size in the notebook
image = cv2.imread('peppers.tif',0)
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

r1 = 100
s1 = 50
r2=180
s2=200
def pixel_value(img, r1,s1,r2, s2):
if (0 &lt;=img and img&lt;=r1):
return (s1/r1)*img
elif (r1 &lt;= img and img &lt;=r2):
return ((s2-s1)/(r2-r1))* (img-r1)+s1
else:
return ((255-s2)/(255-r2))* (img-r2)+s2
picture_value = np.vectorize(pixel_value)

Piecewise_value = picture_value(image, r1,s1,r2, s2)
Piecewise_value = Piecewise_value.astype(np.uint8)
plt.imshow(cv2.cvtColor(Piecewise_value, cv2.COLOR_BGR2RGB))
&lt;matplotlib.image.AxesImage at 0x2197f1b9220&gt;