Digital Image Processing (PYTHON)
grace_1.png
grace_2.png
Jenkinsfile
pipeline {
agent { docker { image 'pmantini/assignment-cosc6380:latest' } }
environment {
PATH = "env/bin/:$PATH"
}
stages {
stage('build') {
steps {
sh 'python dip_hw0.py -il grace_1.png -ir grace_2.png -c 155'
sh 'python dip_hw0.py -il grace_1.png -ir grace_2.png -c 200'
sh 'python dip_hw0.py -il grace_1.png -ir grace_2.png -c 155 -a 0.5 -b 0.5'
sh 'python dip_hw0.py -il grace_1.png -ir grace_2.png -c 155 -a 0.75 -b 0.25'
}
}
}
post {
always {
archiveArtifacts artifacts: 'output/**/*.* ', onlyIfSuccessful: true
}
}
}
dip_hw0.py
"""dip_hw0.py: Starter file to run homework 0"""
__author__ = "Khadija Khaldi"
# revised by Zhenggang Li
#revised by Shishir Shah
__version__ = "1.0.1"
import cv2
import sys
from datetime import datetime
from image_op import operations
def display_image(window_name, image):
"""A function to display image"""
cv2.namedWindow(window_name)
cv2.imshow(window_name, image)
cv2.waitKey(0)
def main():
""" The main function that parses input arguments, calls the appropriate
method and writes the output image"""
#Parse input arguments
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("-il", "--image-left", dest="image_l",
help="specify the name of the left image", metavar="IMAGELEFT")
parser.add_argument("-ir", "--image-right", dest="image_r",
help="specify the name of the right image", metavar="IMAGERIGHT")
parser.add_argument("-c", "--column", dest="column",
help="specify column (c) for merging", metavar="COLUMN")
parser.add_argument("-a", "--alpha", dest="alpha",
help="specify scaling factor for the left section of the image", metavar="ALPHA")
parser.add_argument("-b", "--beta", dest="beta",
help="specify scaling factor for the right section of the image", metavar="BETA")
args = parser.parse_args()
# Load image
if args.image_l is None:
print("Please specify the name of image")
print("use the -h option to see usage information")
sys.exit(2)
else:
input_image_l = cv2.imread(args.image_l, 0)
if args.image_r is None:
print("Please specify the name of image")
print("use the -h option to see usage information")
sys.exit(2)
else:
input_image_r = cv2.imread(args.image_r, 0)
# Check the location to merge
if args.column is None:
print("Merging location Y is not specified using default (155)")
print("use the -h option to see usage information")
column = 155
else:
column = int(args.column)
if not column in range(0, input_image_l.shape[1]):
print("column value outside image bounds, value should be between 0 and %s" % (input_image_l.shape[1]))
print("Using default value (155)")
column = 155
# Check the alpha is between 0 to 1
if args.alpha is None:
print('ALPHA not specified using default (1)')
alpha = 1
else:
alpha = float(args.alpha)
if alpha < 0 and alpha > 1:
print("Alpha value should be between 0 and 1")
print("Using default value (0.5)")
alpha = 0.5
if args.beta is None:
print('BETA not specified using default (1)')
beta = 1
else:
beta = float(args.beta)
if beta < 0 and beta > 1:
print("Beta value should be between 0 and 1")
print("Using default value (0.5)")
beta = 0.5
# Write output file
outputDir = 'output/'
operation_obj = operations.Operation()
merged_image = operation_obj.merge(input_image_l, input_image_r, column=column)
output_image_name = outputDir + 'merged_image' + ".jpg"
cv2.imwrite(output_image_name, merged_image)
scaled_image =operation_obj.intensity_scaling(merged_image, column=column, alpha=alpha, beta=beta)
output_image_name = outputDir + 'scaled_image' + ".jpg"
cv2.imwrite(output_image_name, scaled_image)
centralized_pixels = operation_obj.centralize_pixel(merged_image, column=column)
output_image_name = outputDir + 'centralized_image' + ".jpg"
cv2.imwrite(output_image_name, centralized_pixels)
if __name__ == "__main__":
main()
requirements.txt
opencv-python==3.1.0.4
matplotlib
1. Image Merging: Write a code to merge two images of the computer scientist Grace Hopper. In 1952, Grace Hopper and her team created the first compiler for computer languages. The inputs to your function are: (i) image left: grace_1.png (size: 350 Rows X 340 Cols), (ii) image right: grace_2.png (size: 350 Rows X 340 Cols), (iii) image column (c) at which the images should be merged. image_op/operations.py: Edit the function merge o Define a new image as follows: the first c columns (default value = 155) should be equal to the first c columns of the left image (grace_1.png). The remaining columns should be equal to the columns greater than c from the right image (grace_2.png). 2. Intensity Scaling: Write a code to scale the intensity of the left half of the merged image by scaling factor of alpha and the right half by a scaling factor of beta. Image Scaling: Image/Intensity scaling is a linear image operation where each pixel is multiplied by a scaling factor. The scaling factor is a value between 0 and 1. The inputs to your function are: (i) input image, (ii) image column at which left section ends, (iii) alpha: scaling factor for the left section (0 <= alpha <= 1) and (iv) beta: scaling factor for the right section ((0 <= beta <= 1).
image_op/operations.py: Edit the function intensity_scaling. 3. Centralizing pixel intensities: Write a code to centralize the intensities of the two sections of the image. Centralizing Pixels: When merging, the two images can have very different overall brightness values. Here the goal is to make sure that the average intensities of the left section and the right section are equal and centralized (= 128). After centralizing pixels, the average of all the pixels in the left section will be ~128 and the average of all the pixels in the right section will be ~128 as well. Let M be the input image. Let l be all the intensities in left section of the image and r all the intensities in the right section of the image. Steps: 1. Compute average intensity of left pixels. (mi). 2. Compute offset. (01 = 128 - mi). 3. For each pixel M(x,y) in the left section add the offset. (M/(x,y) + 01). 4. Compute average intensity of right pixels. (mr). 5. Compute offset. (or = 128 - mr). 6. For each pixel M:(x,y) in the right section add the offset. (M(x,y) + 0,). Note: After applying the operation, some pixels may endup being <0 or > 255. Clip these pixels, such that any value greater than 255 is assigned a value of 255, and any value less than 0 will be assigned a value of 0. The inputs to your function are: (i) image, (ii) image column at which left section ends. image_op/operations.py: Edit the function centralize_pixel. Do not use np.mean to compute the averages. O Note: Do not use any in-built functions from opencv and numpy (E.g: np.mean). In general, you can use function from math library. Only Functions allowed for this part are: np.array(, np.matrix(), np.zeros(), np.ones(), cv2.imread(), cv2.namedWindow(), cv2.waitKey().
. Please do not change the code structure Usage: python dip_hwo.py -ir <image-name> -il <image2- name> -C <c> -a <alpha> -b <beta> Example: python dip_hwo.py -il grace_1.png -ir grace_2.png -C 155 -a 0.2 - 0.8 - . Please make sure the code runs when you run the above command from prompt/terminal All the output images and files are saved to "output/" folder Two images are provided for testing: grace_1.jpg and grace_2.png PS. Please do not change: dip_hwo.py, requirements.txt, and Jenkinsfile.