0% found this document useful (0 votes)
6 views5 pages

Dip Lab2

Uploaded by

hdaro96ykl0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Dip Lab2

Uploaded by

hdaro96ykl0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

"source": [

"from PIL import Image,ImageChops,ImageEnhance\n",


"import cv2\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"]

"source": [
"# ‫ط© ط§ظ‚ظ‬،‫ط§ط‬±‫ط„ط‬µ‫ظ‬±‫ظ ‡ظˆط‬ƒ‫ظ‬ƒ‫ط †ط§ط¦ظ‬µ‫ظ‬±‫\‡ظˆط‬n",
"im = Image.open(r\"C:\\Users\\Lenovo\\Desktop\\BB.jpg\")\n",
"im1 = Image.open(r\"C:\\Users\\Lenovo\\Desktop\\RR.png\")"]

"source": [
"im2= cv2.imread(r\"C:\\Users\\Lenovo\\Desktop\\BB.jpg\")\n",
"im3 = cv2.imread(r\"C:\\Users\\Lenovo\\Desktop\\RR.png\")"]

"source": [
"im3"]

"source": [
"im2"

"source": [
"type(im1)"]

"source": [
"type(im2)"]

"source": [
"gim=im.convert(\"L\")\n",
"gim.show()\n",
"# L ---> gray image\n",
"# RGB\n",
"# RGBA\n",
"# CMYK"]

"source": [
"gim"]

"source": [
"rim=im.convert(\"RGB\")\n",
"# ‫ﻇﭩط¯ ظ‬¹‫ظ…ط§ط¦ظ‚طهط‬³‫ ظ…ط© ط¨ط§ط‬،‫ظˆط§طه †ظ‚ط§ط‬
‫\†ظˆط§ظ„ط§ظ„ط§ظ‬n",
"rim.getbands()"]

"source": [
"# ‫ﻇﭩظ‚طهظ‬³‫ط„ط§ظ …ط‬µ‫ظ‬±‫ظ„ط§ظ ‡ظˆط‬‰ ‫ط§ط» „ط»ظ„ط§ظ‬
‫\ظˆط§طه†ظ‚ظ„ط§ظ‬n",
"red,green,blue=rim.split()"]

"source": [
"# ‫طهظ‬ƒ‫ط…ظ †ظˆﻇﭩظ‬µ‫ﻇﭙظˆﻇﭙط© ط§ط‬µ‫ ط¨ظ‬±‫ طط¬ظ†ﻇﭙط§ط‬³‫…ﻇﭙط‬
‫ط…ظ‬µ‫ط…ط§طظ„ط§ظ †ظˆظ„ظ„ﻇﭙظˆﻇﭙط© ط§ظ‬±\n",
"zeros=red.point(lambda _:0)\n",
"# ‫طهظ‬ƒ‫ط„ط§ظ †ظˆﻇﭩظ‬µ‫ظ‬±‫ط…ط¨ظ ‡ظˆط‬µ‫„ط§ظ †ظˆظ„ظ„ﻇﭙظˆﻇﭙط© ط§ظ‬-‫ط§ط‬
‫ ﻇﭙظ…ظ‬±‫\·ط‚ط‬n",
"red_im=Image.merge(\"RGB\",(red,zeros,zeros))\n",
"red_im.show()"]

"source": [
"green_im=Image.merge(\"RGB\",(zeros,green,zeros))\n",
"green_im.show()"
]

"source": [
"blue_im=Image.merge(\"RGB\",(zeros,zeros,blue))\n",
"blue_im.show()"]

"source": [
"im=im.convert(\"RGB\")\n",
"inv_im=ImageChops.invert(im)\n",
"inv_im"]

"source": [
"#‫ظ„ط´ط¯ط© ط§ظ‬،‫طهط¶ط§ط‬³‫\ ‡ط§ط‬n",
"# ‫ط§ ظ‬°‫ط§ط‬ƒ‫ظ„ط§ظ †ط§ظ‬±‫ط§ظ …ظ‚ط‬ƒ‫ ظ‬±‫ظˆط§طط¯ ﻇﭩظﭙطهط „ط§ظ †ظ…ط¨ط‬
‫ط„ط§ظ‬µ‫ظ‬±‫ظ ‡ظˆط‬ƒ‫ط§ ظ…ط§ظ „ظ…ط§ظ‬°‫ط§ ط§ط‬ƒ‫ظˆط§طط¯ „ط§ظ †ظ…ظ „ظ‚ط§ظ †ط§ظ‬
‫ط§ظ‬ƒ‫ظˆط§ط‬³‫ ط‬±‫\¯ط»ط‬n",
"#factor>1 --> more light\n",
"#factor <1 -->more dark\n",
"enhancer=ImageEnhance.Brightness(im)"]
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"

"source": [
"factor=0.3\n",
"en_im=enhancer.enhance(factor)\n",
"en_im"]

"source":[
"# ‫\تزيد اﻻبيض للبياض وتزيد اﻻسود للسواد‬n",
"enhancer=ImageEnhance.Contrast(im)\n",
"factor=3\n",
"en_im=enhancer.enhance(factor)\n",
"en_im"]
"source": [
"Basic gray levels transformation"]

"source": [
"# Image negative\n",
"inv_im=ImageChops.invert(im)\n",
"inv_im"]

"source": [
"# Log transformation\n",
"c = 255 / np.log(1 + np.max(im2)) \n",
"log_image = c * (np.log(im2 + 1)) \n",
"\n",
"# float value will be converted to int \n",
"log_image = np.array(log_image, dtype = np.uint8) \n",
"\n",
"plt.imshow(im2) \n",
"plt.show() \n",
"plt.imshow(log_image) \n",
"plt.show() "]

"source": [
"# power transformation\n",
"gamma = 0.1 \n",
"gamma_corrected = np.array(255*(im2 / 255) ** gamma,
dtype = 'uint8') \n",
"cv2.imwrite('gamma_transformed'+str(gamma)+'.jpg',
gamma_corrected) \n",
"\n",
"gamma =0.5\n",
"gamma_corrected = np.array(255*(im2 / 255) ** gamma,
dtype = 'uint8') \n",
"cv2.imwrite('gamma_transformed'+str(gamma)+'.jpg',
gamma_corrected)\n",
"\n",
"gamma = 1.2\n",
"gamma_corrected = np.array(255*(im2 / 255) ** gamma,
dtype = 'uint8') \n",
"cv2.imwrite('gamma_transformed'+str(gamma)+'.jpg',
gamma_corrected)\n",
"\n",
"gamma = 2.2 \n",
"gamma_corrected = np.array(255*(im2 / 255) ** gamma,
dtype = 'uint8') \n",
"cv2.imwrite('gamma_transformed'+str(gamma)+'.jpg',
gamma_corrected)" ]

"source": [
"# Piecewise linear transformation\n",
"# 1- contrast stretching\n",
"\n",
"def pixelVal(pix, r1, s1, r2, s2):\n",
" if (0 <= pix and pix <= r1): \n",
" return (s1 / r1)*pix \n",
" elif (r1 < pix and pix <= r2): \n",
" return ((s2 - s1)/(r2 - r1)) * (pix - r1) + s1
\n",
" else: \n",
" return ((255 - s2)/(255 - r2)) * (pix - r2) + s2
\n",
" \n",
"\n",
"\n",
" \n",
"# Define parameters. \n",
"r1 = 70\n",
"s1 = 0\n",
"r2 = 140\n",
"s2 = 255\n",
" \n",
"\n",
"pixelVal_vec = np.vectorize(pixelVal) \n",
"contrast_stretched = pixelVal_vec(im2, r1, s1, r2, s2)
\n",
" \n",
"cv2.imwrite('contrast_stretch.jpg', contrast_stretched)"]

"source": [
"# 2- gray level slicing\n",
"# Without backgroung\n",
"# gray image\n",
"img = cv2.imread(r\"C:\\Users\\Lenovo\\Desktop\\BB.jpg\",
0)\n",
"\n",
"row, column= img.shape\n",
"\n",
"# Create an zeros array to store the sliced image\n",
"img1 = np.zeros((row,column),dtype = 'uint8')\n",
"\n",
"min_range = 80\n",
"max_range = 140\n",
"\n",
"\n",
"for i in range(row):\n",
" for j in range(column):\n",
" if img[i,j]>min_range and img[i,j]<max_range:
\n",
" img1[i,j] = 255\n",
" else: \n",
" img1[i,j] = 0\n",
" #img1[i,j] = img[i,j] # with background\n",
"\n",
"cv2.imwrite('slicedimage.jpg', img1)\n",
"\n"]

"source": [
"# color image\n",
"\n",
"img =
cv2.imread(r\"C:\\Users\\Lenovo\\Desktop\\BB.jpg\")\n",
"\n",
"row, column, channels = img.shape\n",
"\n",
"img1 = np.zeros((row, column, channels),
dtype='uint8')\n",
"\n",
"min_range = np.array([80, 80, 80]) # ‫نطاق الحد اﻷدنى‬
‫(لﻜﻞ قناة‬R, G, B)\n",
"max_range = np.array([140, 140, 140]) # ‫نطاق الحد اﻷعلى‬
‫(لﻜﻞ قناة‬R, G, B)\n",
"\n",
"for i in range(row):\n",
" for j in range(column):\n",
" if np.all(img[i, j] > min_range) and
np.all(img[i, j] < max_range):\n",
" img1[i, j] = [255, 255, 255] \n",
" else:\n",
" img1[i, j] = 0\n",
" # img1[i, j] = img[i, j] # with
background\n",
"\n",
"cv2.imwrite(\"sliced_image.jpg\", img1)"]

"source": [
"# Histogram\n",
"img =
cv2.imread(r\"C:\\Users\\Lenovo\\Desktop\\BB.jpg\",0)\n",
"cv2.imwrite(\"gray_image.jpg\", img)\n",
"# ravel ‫تحول ﻣﻦ ﻣصفوفه ذات ابعاد ﻣختلفه الى ﻣصفوفه ذات‬
‫\بعد واحد ﻻن الهيستوجرام ﻻ يتعاﻣﻞ اﻻ ﻣﻊ بعد واحد‬n",
"plt.hist(img.ravel(),256,[0,255])\n",
"plt.show"
]
"source": [
"# equalization \n",
"equ=cv2.equalizeHist(img)\n",
"\n",
"cv2.imwrite(\"equ_image.png\",equ)\n",
"plt.hist(equ.ravel(),256,[0,255])\n",
"plt.show"]

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy