Activity Day           Wednesday, May 25, 2016
Due Date Friday, May 27, 2016 11:55 PM

 

Activity 2: [A2] Blending and Arithmetic in Image Processing.


     

Motivation:

Learn about basic techniques for manipulating and analyzing images by creating image filters, and blend them together, mathematically, and in practice. To enable this you will create a program in python using the openCV image library.

This activity will extend into a more in depth homework (assigned later this evening) were you will create a simple image calculator.

Setup:

Setup: Use OpenCV for this, and use 2 test images: gray-scale1-rgb.png and gray-scale2-rgb.png to test, and analyze your program. To simplify the computations use only a grayscale channel by converting the images into grayscale.

import cv2 #
import numpy as np

## convert to grayscale
cv2greyimg = cv2.cvtColor( cv2img, cv2.COLOR_RGB2GRAY )

## convert to grayscale
cv2greyimg = cv2.cvtColor( cv2img, cv2.COLOR_RGB2GRAY )

Other useful commands:

## CV2 read image and displays
fname = 'gray-scale2-rgb.png' # file name
cv2img = cv2.imread(fname)
cv2.imshow('CV2: Small IMAGE', cv2img)

cv2.waitKey( 0 ) # pauses program so you can see the display
cv2.destroyAllWindows()

cv2.imwrite('messigray.png',img)

## splits the rgb channels of images (bgr in opencv) into their component channels, and then merges them back together via the merge command
b,g,r = cv2.split(img) 2
img = cv2.merge((b,g,r))

Activities (and Questions) Exploring openCV and numpy.

Explore using simple mathematical operations, +, -, /, pow(x), and * and their combinations in openCV, and numpy and their result both on the image and the result matrix. You will also explore alpha blending where you have different weights to given images.

On the test images perform the below operation both with openCV (cv2) and numPY arithmetics, draw on the paper the resulting image, outline the numeric result (for one pixel) for cv2 and numpy, and discuss whether the operation is commutative or not.

Each activity consists of:
2 images, 2 discussions of pixel arithmetic, 2 discussion on whether operations commutative.

--- In class: (first operand should be gray_scale1, 2nd operand gray_scale2)
(1) Addition
(2) Subtraction
(3) Multiply
(4) Addition with alpha (alpha as .3 or .7).

----At home.
(5) Combination (screen mode blending ). Challenge. 1−(1−a)×(1−b), also look up soft light where you weight
(6) Experiment with alpha (optional)

Each activity consists of:
2 images, 2 discussions of pixel arithmetic, 2 discussion on whether operation is commutative or not.

Example:
Addition:

Image Result Add-CV1, Add-NumPY displayed (screen snapshot of the 2 images).
ADD-CV2 pixel level operates as follows ______and it is (or is not) commutative.
ADD-numpy pixel level operates as follows_____ and it is (or is not) commutative.

Caveat:

Note in numpy (therefor openCV) arithmetic operators on arrays apply element-wise. A new array is created and filled with the result.

Helful Code Snippets:

Commands performing mathematical operations on images.

##
result cv2.add(x,y)
print result

##
result = x + y # numpy addition
print result
##
cv2.add(x,y)
##
# image blending with alpha coefficient denoted after image in parameter list, the last parameter should be zero. If it is nonzero it will be a scalar added added to the sum.
DST = cv2.addWeighted(img1,0.7,img2,0.3,0)
##
cv2.add(x,y)
##
cv2.add(x,y)
##
cv2.add(x,y)

What to turn in:

On t-square, submit as attachment only (no zip files please):
1. activity2-code.py
2. activity2-discussion.pdf (with resulting images template)

In PDF For each activity (1)-(5) that you did NOT complete in class. If you completed ALL tasks you do not need to turn anything. If you are not sure if something is complete, it is better to turn in more than less.

* (2 images) : Magnified Screenshots of Image generated from CV2, and numPY
* discussion : see below for more details

Discussion of pixel operation of each package 1) cv2, and 2) numpy, highlight what happened in the image visually mathematically, i.e.,: is there evidence that:
a) It used modulus operator or does it 'truncates'
b) Does it 'standardize the value from 0-255, to 0.0 - 1.0
c) Is the operation commutative or not.

Note you do not need to resubmit images that you turned in, in class already, and for those that you did turn in, you will get automatic points added for (b) since that was not emphasized during class.

Unlimited resubmissions are allowed up to the deadline for the assignment.

Grade Criteria:

Grade 100 points total:
50 pts ------ 10 pts display each pair of images (a pair - cv2, numpy).
25 pts ------ 05 pts for each activity that you did not complete in class indicate if you observe proof of use of modulus, or truncation, {and pixel standardization) in displayed image. In class you were not asked about the standardization but in the take home please indicate whether you observe the use of this operation.
10 pts ------ 1 pt per image, please indicate whether the operation is commutative or not.
15 pts ------ activity2-code.py Submission of code and that it is correct syntactically.

Q/A: (anticipated).

(1) You do not resumbit an activity that you completed in class if all you are missing for the particular activity is the "pixel standardization" observation. You will get these points automatically for each completed task.

(2) If you are not sure whether you completed a task or not, it is better you submit the task in the pdf to be covered.

 

Resources:

openCV-Python documentation and tutorials:
http://docs.opencv.org/3.1.0/d6/d00/tutorial_py_root.html#gsc.tab=0

numpy (scipy) documentation and tutorials (operations on matrices, and on linear algebra!).
http://scipy.github.io/old-wiki/pages/Tentative_NumPy_Tutorial.html