Add Java and Python code for the following tutorials:
- Changing the contrast and brightness of an image!
- Operations with images
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
from __future__ import division
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
|
||||
# Snippet code for Operations with images tutorial (not intended to be run)
|
||||
|
||||
def load():
|
||||
# Input/Output
|
||||
filename = 'img.jpg'
|
||||
## [Load an image from a file]
|
||||
img = cv.imread(filename)
|
||||
## [Load an image from a file]
|
||||
|
||||
## [Load an image from a file in grayscale]
|
||||
img = cv.imread(filename, cv.IMREAD_GRAYSCALE)
|
||||
## [Load an image from a file in grayscale]
|
||||
|
||||
## [Save image]
|
||||
cv.imwrite(filename, img)
|
||||
## [Save image]
|
||||
|
||||
def access_pixel():
|
||||
# Accessing pixel intensity values
|
||||
img = np.empty((4,4,3), np.uint8)
|
||||
y = 0
|
||||
x = 0
|
||||
## [Pixel access 1]
|
||||
intensity = img[y,x]
|
||||
## [Pixel access 1]
|
||||
|
||||
## [Pixel access 3]
|
||||
blue = img[y,x,0]
|
||||
green = img[y,x,1]
|
||||
red = img[y,x,2]
|
||||
## [Pixel access 3]
|
||||
|
||||
## [Pixel access 5]
|
||||
img[y,x] = 128
|
||||
## [Pixel access 5]
|
||||
|
||||
def reference_counting():
|
||||
# Memory management and reference counting
|
||||
## [Reference counting 2]
|
||||
img = cv.imread('image.jpg')
|
||||
img1 = np.copy(img)
|
||||
## [Reference counting 2]
|
||||
|
||||
## [Reference counting 3]
|
||||
img = cv.imread('image.jpg')
|
||||
sobelx = cv.Sobel(img, cv.CV_32F, 1, 0);
|
||||
## [Reference counting 3]
|
||||
|
||||
def primitive_operations():
|
||||
img = np.empty((4,4,3), np.uint8)
|
||||
## [Set image to black]
|
||||
img[:] = 0
|
||||
## [Set image to black]
|
||||
|
||||
## [Select ROI]
|
||||
smallImg = img[10:110,10:110]
|
||||
## [Select ROI]
|
||||
|
||||
## [BGR to Gray]
|
||||
img = cv.imread('image.jpg')
|
||||
grey = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
|
||||
## [BGR to Gray]
|
||||
|
||||
src = np.ones((4,4), np.uint8)
|
||||
## [Convert to CV_32F]
|
||||
dst = src.astype(np.float32)
|
||||
## [Convert to CV_32F]
|
||||
|
||||
def visualize_images():
|
||||
## [imshow 1]
|
||||
img = cv.imread('image.jpg')
|
||||
cv.namedWindow('image', cv.WINDOW_AUTOSIZE)
|
||||
cv.imshow('image', img)
|
||||
cv.waitKey()
|
||||
## [imshow 1]
|
||||
|
||||
## [imshow 2]
|
||||
img = cv.imread('image.jpg')
|
||||
grey = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
|
||||
sobelx = cv.Sobel(grey, cv.CV_32F, 1, 0)
|
||||
# find minimum and maximum intensities
|
||||
minVal = np.amin(sobelx)
|
||||
maxVal = np.amax(sobelx)
|
||||
draw = cv.convertScaleAbs(sobelx, alpha=255.0/(maxVal - minVal), beta=-minVal * 255.0/(maxVal - minVal))
|
||||
cv.namedWindow('image', cv.WINDOW_AUTOSIZE)
|
||||
cv.imshow('image', draw)
|
||||
cv.waitKey()
|
||||
## [imshow 2]
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
from __future__ import print_function
|
||||
from builtins import input
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
import argparse
|
||||
|
||||
# Read image given by user
|
||||
## [basic-linear-transform-load]
|
||||
parser = argparse.ArgumentParser(description='Code for Changing the contrast and brightness of an image! tutorial.')
|
||||
parser.add_argument('--input', help='Path to input image.', default='../data/lena.jpg')
|
||||
args = parser.parse_args()
|
||||
|
||||
image = cv.imread(args.input)
|
||||
if image is None:
|
||||
print('Could not open or find the image: ', args.input)
|
||||
exit(0)
|
||||
## [basic-linear-transform-load]
|
||||
|
||||
## [basic-linear-transform-output]
|
||||
new_image = np.zeros(image.shape, image.dtype)
|
||||
## [basic-linear-transform-output]
|
||||
|
||||
## [basic-linear-transform-parameters]
|
||||
alpha = 1.0 # Simple contrast control
|
||||
beta = 0 # Simple brightness control
|
||||
|
||||
# Initialize values
|
||||
print(' Basic Linear Transforms ')
|
||||
print('-------------------------')
|
||||
try:
|
||||
alpha = float(input('* Enter the alpha value [1.0-3.0]: '))
|
||||
beta = int(input('* Enter the beta value [0-100]: '))
|
||||
except ValueError:
|
||||
print('Error, not a number')
|
||||
## [basic-linear-transform-parameters]
|
||||
|
||||
# Do the operation new_image(i,j) = alpha*image(i,j) + beta
|
||||
# Instead of these 'for' loops we could have used simply:
|
||||
# new_image = cv.convertScaleAbs(image, alpha=alpha, beta=beta)
|
||||
# but we wanted to show you how to access the pixels :)
|
||||
## [basic-linear-transform-operation]
|
||||
for y in range(image.shape[0]):
|
||||
for x in range(image.shape[1]):
|
||||
for c in range(image.shape[2]):
|
||||
new_image[y,x,c] = np.clip(alpha*image[y,x,c] + beta, 0, 255)
|
||||
## [basic-linear-transform-operation]
|
||||
|
||||
## [basic-linear-transform-display]
|
||||
# Show stuff
|
||||
cv.imshow('Original Image', image)
|
||||
cv.imshow('New Image', new_image)
|
||||
|
||||
# Wait until user press some key
|
||||
cv.waitKey()
|
||||
## [basic-linear-transform-display]
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
from __future__ import print_function
|
||||
from __future__ import division
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
import argparse
|
||||
|
||||
alpha = 1.0
|
||||
alpha_max = 500
|
||||
beta = 0
|
||||
beta_max = 200
|
||||
gamma = 1.0
|
||||
gamma_max = 200
|
||||
|
||||
def basicLinearTransform():
|
||||
res = cv.convertScaleAbs(img_original, alpha=alpha, beta=beta)
|
||||
img_corrected = cv.hconcat([img_original, res])
|
||||
cv.imshow("Brightness and contrast adjustments", img_corrected)
|
||||
|
||||
def gammaCorrection():
|
||||
## [changing-contrast-brightness-gamma-correction]
|
||||
lookUpTable = np.empty((1,256), np.uint8)
|
||||
for i in range(256):
|
||||
lookUpTable[0,i] = np.clip(pow(i / 255.0, gamma) * 255.0, 0, 255)
|
||||
|
||||
res = cv.LUT(img_original, lookUpTable)
|
||||
## [changing-contrast-brightness-gamma-correction]
|
||||
|
||||
img_gamma_corrected = cv.hconcat([img_original, res]);
|
||||
cv.imshow("Gamma correction", img_gamma_corrected);
|
||||
|
||||
def on_linear_transform_alpha_trackbar(val):
|
||||
global alpha
|
||||
alpha = val / 100
|
||||
basicLinearTransform()
|
||||
|
||||
def on_linear_transform_beta_trackbar(val):
|
||||
global beta
|
||||
beta = val - 100
|
||||
basicLinearTransform()
|
||||
|
||||
def on_gamma_correction_trackbar(val):
|
||||
global gamma
|
||||
gamma = val / 100
|
||||
gammaCorrection()
|
||||
|
||||
parser = argparse.ArgumentParser(description='Code for Changing the contrast and brightness of an image! tutorial.')
|
||||
parser.add_argument('--input', help='Path to input image.', default='../data/lena.jpg')
|
||||
args = parser.parse_args()
|
||||
|
||||
img_original = cv.imread(args.input)
|
||||
if img_original is None:
|
||||
print('Could not open or find the image: ', args.input)
|
||||
exit(0)
|
||||
|
||||
img_corrected = np.empty((img_original.shape[0], img_original.shape[1]*2, img_original.shape[2]), img_original.dtype)
|
||||
img_gamma_corrected = np.empty((img_original.shape[0], img_original.shape[1]*2, img_original.shape[2]), img_original.dtype)
|
||||
|
||||
img_corrected = cv.hconcat([img_original, img_original])
|
||||
img_gamma_corrected = cv.hconcat([img_original, img_original])
|
||||
|
||||
cv.namedWindow('Brightness and contrast adjustments')
|
||||
cv.namedWindow('Gamma correction')
|
||||
|
||||
alpha_init = int(alpha *100)
|
||||
cv.createTrackbar('Alpha gain (contrast)', 'Brightness and contrast adjustments', alpha_init, alpha_max, on_linear_transform_alpha_trackbar)
|
||||
beta_init = beta + 100
|
||||
cv.createTrackbar('Beta bias (brightness)', 'Brightness and contrast adjustments', beta_init, beta_max, on_linear_transform_beta_trackbar)
|
||||
gamma_init = int(gamma * 100)
|
||||
cv.createTrackbar('Gamma correction', 'Gamma correction', gamma_init, gamma_max, on_gamma_correction_trackbar)
|
||||
|
||||
on_linear_transform_alpha_trackbar(alpha_init)
|
||||
on_gamma_correction_trackbar(gamma_init)
|
||||
|
||||
cv.waitKey()
|
||||
Reference in New Issue
Block a user