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:
+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