
The image processor is a python script used to manually change the values of an input image (Edit the image before running the script).
Using numpy and cv2, the script gives the user image edit options (Invert, Grayscale, RGB, Contrast, Left Sobel Filter, Right Sobel Filter, Box Blur, Gaussian Blur, K-Means)
Original Image Invert Right Sobel Left Sobel RGB Multiplication Contrast Sharpen Gaussian Blur
import cv2
import numpy as np
# load in an image
image = cv2.imread("dawso.jpg")
# resize an image
resizeFactor = 1.0
newH = int(image.shape[0]*resizeFactor) # get height of image (and multiply by factor)
newW = int(image.shape[1]*resizeFactor) # get width of image (and multiply by factor)
image = cv2.resize(image, (newW, newH)) # resize image to new dimensions
contin = "Yes"
while contin == "Yes":
cv2.destroyAllWindows()
choice = input("What function would you like to do today? Invert, Grayscale, RGB, Contrast, Filter, K-Means: ")
if choice == "Grayscale":
# creates empty image
grayIm = np.zeros((newH, newW, 1), np.uint8)
# iterate through pixels of image
# rows
for i in range(newH):
# cols
for j in range(newW):
grayIm[i,j,0] = (int(0.309*image[i,j,0]) + int(0.609*image[i,j,1]) + int(0.082*image[i,j,2]))
# show images
cv2.imshow("Original Image", image)
cv2.imshow("Grayscale Image", grayIm)
# save an image
cv2.imwrite("testSave.png", grayIm)
cv2.waitKey(0)
if choice == "Invert":
# creates empty image
invertIm = np.zeros((newH, newW, 3), np.uint8)
# iterate through pixels of image
# rows
for i in range(newH):
# cols
for j in range(newW):
invertIm[i,j,0] = 255-(int(image[i,j,0]))
invertIm[i,j,1] = 255-(int(image[i,j,1]))
invertIm[i,j,2] = 255-(int(image[i,j,2]))
# show images
cv2.imshow("Original Image", image)
cv2.imshow("Inverted Image", invertIm)
# save an image
cv2.imwrite("testSave.png", invertIm)
cv2.waitKey(0)
if choice == "RGB":
# creates empty image
rgbIm = np.zeros((newH, newW, 3), np.uint8)
# iterate through pixels of image
# rows
for i in range(newH):
# cols
for j in range(newW):
#B
blue = (int(1.75*image[i,j,0]))
#G
green = (int(1.1*image[i,j,1]))
#R
red = (int(1*image[i,j,2]))
#prevents overflow
if blue < 0:
blue = 0
elif blue > 255:
blue = 255
if green < 0:
green = 0
elif green > 255:
green = 255
if red < 0:
red = 0
elif red > 255:
red = 255
rgbIm[i,j,0] = blue
rgbIm[i,j,1] = green
rgbIm[i,j,2] = red
# show images
cv2.imshow("Original Image", image)
cv2.imshow("RGB'd Image", rgbIm)
# save an image/keep window open
cv2.imwrite("testSave.png", rgbIm)
cv2.waitKey(0)
if choice == "Contrast":
# creates empty image
contIm = np.zeros((newH, newW, 3), np.uint8)
# iterate through pixels of image
# rows
for i in range(newH):
# cols
for j in range(newW):
#255 * 0.33 = 84.15
blue = (int(image[i, j, 0])-84)*3
green = (int(image[i, j, 1])-84)*3
red = (int(image[i, j, 2])-84)*3
#prevents overflow
if blue < 0:
blue = 0
elif blue > 255:
blue = 255
if green < 0:
green = 0
elif green > 255:
green = 255
if red < 0:
red = 0
elif red > 255:
red = 255
#B
contIm[i,j,0] = blue
#G
contIm[i,j,1] = green
#R
contIm[i,j,2] = red
# show images
cv2.imshow("Original Image", image)
cv2.imshow("Contrasted Image", contIm)
# save an image/keep window open
cv2.imwrite("testSave.png", contIm)
cv2.waitKey(0)
if choice == "Filter":
fType = input("What type of filter would you like to use? Box Blur, Gaussian Blur, Sharpen, Sobel1, Sobel2: ")
if fType == "Box Blur":
# creates empty image
filtIm = np.zeros((newH, newW, 3), np.uint8)
# iterate through pixels of image
# rows
boxFilter = np.array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])
for i in range(newH-1):
# cols
for j in range(newW-1):
blueArray = np.array([[int(image[i-1, j+1, 0]), int(image[i, j+1, 0]), int(image[i+1, j+1, 0])],
[int(image[i-1, j, 0]), int(image[i, j, 0]), int(image[i+1, j, 0])],
[int(image[i-1, j-1, 0]), int(image[i, j-1, 0]), int(image[i+1, j-1, 0])]])
greenArray = np.array([[int(image[i-1, j+1, 1]), int(image[i, j+1, 1]), int(image[i+1, j+1, 1])],
[int(image[i-1, j, 1]), int(image[i, j, 1]), int(image[i+1, j, 1])],
[int(image[i-1, j-1, 1]), int(image[i, j-1, 1]), int(image[i+1, j-1, 1])]])
redArray = np.array([[int(image[i-1, j+1, 2]), int(image[i, j+1, 2]), int(image[i+1, j+1, 2])],
[int(image[i-1, j, 2]), int(image[i, j, 2]), int(image[i+1, j, 2])],
[int(image[i-1, j-1, 2]), int(image[i, j-1, 2]), int(image[i+1, j-1, 2])]])
filterSum = np.sum(boxFilter)
blueSum = np.sum(blueArray * boxFilter)
greenSum = np.sum(greenArray * boxFilter)
redSum = np.sum(redArray * boxFilter)
#B
blue = (blueSum/filterSum)
#G
green = (greenSum/filterSum)
#R
red = (redSum/filterSum)
# prevents overflow
if blue < 0:
blue = 0
elif blue > 255:
blue = 255
if green < 0:
green = 0
elif green > 255:
green = 255
if red < 0:
red = 0
elif red > 255:
red = 255
# B
filtIm[i, j, 0] = blue
# G
filtIm[i, j, 1] = green
# R
filtIm[i, j, 2] = red
if fType == "Gaussian Blur":
# creates empty image
filtIm = np.zeros((newH, newW, 3), np.uint8)
# iterate through pixels of image
# 3x3 Gaussian filter in a numpy array
gaussFilter = np.array([[1, 2, 1],
[2, 4, 2],
[1, 2, 1]])
for i in range(newH - 1):
# cols
for j in range(newW - 1):
blueArray = np.array(
[[int(image[i - 1, j + 1, 0]), int(image[i, j + 1, 0]), int(image[i + 1, j + 1, 0])],
[int(image[i - 1, j, 0]), int(image[i, j, 0]), int(image[i + 1, j, 0])],
[int(image[i - 1, j - 1, 0]), int(image[i, j - 1, 0]), int(image[i + 1, j - 1, 0])]])
greenArray = np.array(
[[int(image[i - 1, j + 1, 1]), int(image[i, j + 1, 1]), int(image[i + 1, j + 1, 1])],
[int(image[i - 1, j, 1]), int(image[i, j, 1]), int(image[i + 1, j, 1])],
[int(image[i - 1, j - 1, 1]), int(image[i, j - 1, 1]), int(image[i + 1, j - 1, 1])]])
redArray = np.array(
[[int(image[i - 1, j + 1, 2]), int(image[i, j + 1, 2]), int(image[i + 1, j + 1, 2])],
[int(image[i - 1, j, 2]), int(image[i, j, 2]), int(image[i + 1, j, 2])],
[int(image[i - 1, j - 1, 2]), int(image[i, j - 1, 2]), int(image[i + 1, j - 1, 2])]])
#sum of gaussian filter values
filterSum = np.sum(gaussFilter)
#sum of 3x3 R,G,B values * 3x3 gauss filter
blueSum = np.sum(blueArray * gaussFilter)
greenSum = np.sum(greenArray * gaussFilter)
redSum = np.sum(redArray * gaussFilter)
# B divided by sum of filter values
blue = (blueSum / filterSum)
# G
green = (greenSum / filterSum)
# R
red = (redSum / filterSum)
# prevents overflow
if blue < 0:
blue = 0
elif blue > 255:
blue = 255
if green < 0:
green = 0
elif green > 255:
green = 255
if red < 0:
red = 0
elif red > 255:
red = 255
# B
filtIm[i, j, 0] = blue
# G
filtIm[i, j, 1] = green
# R
filtIm[i, j, 2] = red
if fType == "Sharpen":
# creates empty image
filtIm = np.zeros((newH, newW, 3), np.uint8)
# iterate through pixels of image
# 3x3 Gaussian filter in a numpy array
sharpFilter = np.array([[-1, -1, -1],
[-1, 9, -1],
[-1, -1, -1]])
for i in range(newH - 1):
# cols
for j in range(newW - 1):
blueArray = np.array(
[[int(image[i - 1, j + 1, 0]), int(image[i, j + 1, 0]), int(image[i + 1, j + 1, 0])],
[int(image[i - 1, j, 0]), int(image[i, j, 0]), int(image[i + 1, j, 0])],
[int(image[i - 1, j - 1, 0]), int(image[i, j - 1, 0]), int(image[i + 1, j - 1, 0])]])
greenArray = np.array(
[[int(image[i - 1, j + 1, 1]), int(image[i, j + 1, 1]), int(image[i + 1, j + 1, 1])],
[int(image[i - 1, j, 1]), int(image[i, j, 1]), int(image[i + 1, j, 1])],
[int(image[i - 1, j - 1, 1]), int(image[i, j - 1, 1]), int(image[i + 1, j - 1, 1])]])
redArray = np.array(
[[int(image[i - 1, j + 1, 2]), int(image[i, j + 1, 2]), int(image[i + 1, j + 1, 2])],
[int(image[i - 1, j, 2]), int(image[i, j, 2]), int(image[i + 1, j, 2])],
[int(image[i - 1, j - 1, 2]), int(image[i, j - 1, 2]), int(image[i + 1, j - 1, 2])]])
#sum of gaussian filter values
filterSum = np.sum(sharpFilter)
#sum of 3x3 R,G,B values * 3x3 gauss filter
blueSum = np.sum(blueArray * sharpFilter)
greenSum = np.sum(greenArray * sharpFilter)
redSum = np.sum(redArray * sharpFilter)
blue = blueSum/filterSum
green = greenSum/filterSum
red = redSum/filterSum
#prevents overflow
if blue < 0:
blue = 0
elif blue > 255:
blue = 255
if green < 0:
green = 0
elif green > 255:
green = 255
if red < 0:
red = 0
elif red > 255:
red = 255
# B
filtIm[i, j, 0] = blue
# G
filtIm[i, j, 1] = green
# R
filtIm[i, j, 2] = red
if fType == "Sobel1":
# creates empty image
filtIm = np.zeros((newH, newW, 1), np.uint8)
# iterate through pixels of image
# 3x3 Gaussian filter in a numpy array
sobelFilter = np.array([[1, 0, -1],
[2, 0, -2],
[1, 0, -1]])
for i in range(newH - 1):
# cols
for j in range(newW - 1):
blueArray = np.array(
[[int(image[i - 1, j + 1, 0]), int(image[i, j + 1, 0]), int(image[i + 1, j + 1, 0])],
[int(image[i - 1, j, 0]), int(image[i, j, 0]), int(image[i + 1, j, 0])],
[int(image[i - 1, j - 1, 0]), int(image[i, j - 1, 0]), int(image[i + 1, j - 1, 0])]])
#sum of gaussian filter values
filterSum = np.sum(sobelFilter)
#sum of 3x3 R,G,B values * 3x3 gauss filter
blueSum = np.sum(blueArray * sobelFilter)
blue = blueSum
#prevents overflow
if blue < 0:
blue = 0
elif blue > 255:
blue = 255
# B
filtIm[i, j, 0] = blue
if fType == "Sobel2":
# creates empty image
filtIm = np.zeros((newH, newW, 1), np.uint8)
# iterate through pixels of image
# 3x3 Gaussian filter in a numpy array
sobelFilter = np.array([[1, 2, 1],
[0, 0, 0],
[-1, -2, -1]])
for i in range(newH - 1):
# cols
for j in range(newW - 1):
blueArray = np.array(
[[int(image[i - 1, j + 1, 0]), int(image[i, j + 1, 0]), int(image[i + 1, j + 1, 0])],
[int(image[i - 1, j, 0]), int(image[i, j, 0]), int(image[i + 1, j, 0])],
[int(image[i - 1, j - 1, 0]), int(image[i, j - 1, 0]), int(image[i + 1, j - 1, 0])]])
#sum of gaussian filter values
filterSum = np.sum(sobelFilter)
#sum of 3x3 R,G,B values * 3x3 gauss filter
blueSum = np.sum(blueArray * sobelFilter)
blue = blueSum
#prevents overflow
if blue < 0:
blue = 0
elif blue > 255:
blue = 255
# B
filtIm[i, j, 0] = blue
# show images
cv2.imshow("Original Image", image)
cv2.imshow("Filtered Image", filtIm)
# save an image/keep window open
cv2.imwrite("testSave.png", filtIm)
cv2.waitKey(0)
contin = input("Continue? Yes/No ")
cv2.destroyAllWindows()