Skip to content

Instantly share code, notes, and snippets.

@aishwarya-singh25
Last active November 14, 2023 17:50
Show Gist options
  • Save aishwarya-singh25/57637da528de738e7adb4da824843863 to your computer and use it in GitHub Desktop.
Save aishwarya-singh25/57637da528de738e7adb4da824843863 to your computer and use it in GitHub Desktop.
Preprocessing Techniques
from skimage import exposure
#adjusting brightness
image = imread('images.jpeg')
image_bright = exposure.adjust_gamma(image, gamma=0.5,gain=1)
image_dark = exposure.adjust_gamma(image, gamma=1.5,gain=1)
# plotting images
plt.subplot(131), imshow(image)
plt.title('Original Image')
plt.subplot(132),imshow(image_bright)
plt.title('Bright Image')
plt.subplot(133),imshow(image_dark)
plt.title('Dark Image')
plt.show()
image = imread('warriors.jpg')
# selecting part of the image only
cropped = image[100:(img.shape[0]-100),100:(img.shape[1]-100)]
plt.subplot(121), imshow(image)
plt.title('Original Image')
plt.subplot(122),imshow(cropped)
plt.title('Cropped Image')
plt.show()
from numpy import fliplr, flipud
dog = imread('Puppy.jpg')
cat = imread('whiskers.jpg')
dog_flip = fliplr(dog)
cat_flip = fliplr(cat)
plt.subplot(141), imshow(dog)
plt.subplot(142), imshow(dog_flip)
plt.subplot(143), imshow(cat)
plt.subplot(144), imshow(cat_flip)
plt.show()
from skimage.io import imread, imshow
from skimage import data
image = data.astronaut()
imshow(image)
from skimage.filters import median
image = imread('images.jpeg', as_gray=True)
image_median = median(image)
# plotting images
plt.subplot(121), imshow(image)
plt.title('Original Image')
plt.subplot(122),imshow(image_median)
plt.title('Smooth Image')
plt.show()
image_gray = imread('images.jpeg', as_gray=True)
print(image_gray.shape)
print(image_gray)
from skimage.io import imread, imshow
import matplotlib.pyplot as plt
%matplotlib inline
image_color = imread('images.jpeg', as_gray=False)
print(image_color.shape)
imshow(image_color)
from skimage.io import imread, imshow
import matplotlib.pyplot as plt
%matplotlib inline
image_gray = imread('images.jpeg', as_gray=True)
imshow(image_gray)
red_channel = image_color*[1,0,0]
imshow(red_channel)
from skimage.transform import rescale
img = imread('images.jpeg')
img_rescaled = rescale(img, scale=(0.5, 0.5))
plt.subplot(121), imshow(img)
plt.title('Original Image')
plt.subplot(122), imshow(img_rescaled)
plt.title('Rescaled Image')
plt.show()
from skimage.transform import resize
img = imread('images.jpeg')
#resize image
img_resized = resize(img, (300, 300))
#plot images
plt.subplot(121), imshow(img)
plt.title('Original Image')
plt.subplot(122), imshow(img_resized)
plt.title('Resized Image')
plt.show()
from skimage.color import rgb2gray
img = imread('images.jpeg')
img_new = rgb2gray(img)
plt.subplot(121), imshow(img)
plt.title('RGB Format')
plt.subplot(122), imshow(img_new)
plt.title('Grayscale Format')
plt.show()
from skimage.color import rgb2hsv
img = imread('images.jpeg')
img_new = rgb2hsv(img)
plt.subplot(121), imshow(img)
plt.title('RGB Format')
plt.subplot(122), imshow(img_new)
plt.title('HSV Format')
plt.show()
from skimage.transform import rotate
image = imread('tilt_image.png')
image_rotated = rotate(image, angle=45)
imshow(image_rotated)
from skimage.transform import rotate
image = imread('tilt_image.png')
image_rotated = rotate(image, angle=45, resize=True)
imshow(image_rotated)
from skimage.filters import sobel_h
image = imread('images.jpeg', as_gray=True)
image_sobelh = sobel_h(image)
# plotting images
plt.subplot(121), imshow(image)
plt.title('Original Image')
plt.subplot(122),imshow(image_sobelh, cmap = True)
plt.title('Horizontal Edge')
plt.show()
@Tikquuss
Copy link

Thanks

@AlkisPis
Copy link

AlkisPis commented Sep 13, 2021

'rescale_image.py' is very problematic (at least for me (Python 3.7, scikit-image 0.18.3)):

  1. img_rescaled = rescale(img, scale=(0.5, 0.5)) produces "ValueError: Supply a single scale, or one value per spatial axis" !!
    If I change it to img_rescaled = rescale(img, 0.5), it works
  2. plt.subplot(122), imshow(img_rescaled) produces "TypeError: Invalid shape (140, 114, 2) for image data" !!

Come on, this is too much guys!

@AlkisPis
Copy link

Where are the images used in the code stored ??????????????

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment