A modified image... |
|
|
|
|
.. before ..
[image courtesy of pixabay] |
.. after .. |
The Algorithm |
|
|
The dimensions of the edited image has been set to be the same as the original image. A border was placed around the image. The blur effect has been adopted from the Coursera lesson, adjusted such that only particular parts of the image would be blurred. For this case, the blur effect is within the lower and upper half of the set border size. |
|
The Coding |
|
var originalImage = new SimpleImage("tea-1170555__180.jpg");
var imgWidth = originalImage.getWidth();
var imgHeight = originalImage.getHeight();
var editedImage = new SimpleImage(imgWidth, imgHeight);
// variables
var borderSize = 6;
var lowerLimit = borderSize/2;
var higherLimit = borderSize - lowerLimit;
var distRange = 10;
var randomRange = 0.5;
// manage pixels
for (var px of originalImage.values()) {
var newPx = px;
var x = newPx.getX();
var y = newPx.getY();
// place border
if (isWithinBorderDimension(x, y)) {
newPx = setBorderColor(newPx);
}
// blur inner & outer half of border
// ** actual blur process: adopted from lesson
if (isWithinHorizontalBlur(x, y) || isWithinVerticalBlur(x, y)) {
if (Math.random() > randomRange) {
newPx = getPixelNearby(originalImage, x, y, distRange);
}
}
editedImage.setPixel(x, y, newPx);
}
// print before and after images
print(originalImage, editedImage);
/************************
* FUNCTIONS *
************************/
/**
* Sets the border color
*
* @param pixel pixel to be edited
*
* @return edited pixel
*/
function setBorderColor(pixel) {
pixel.setRed(222);
pixel.setGreen(199);
pixel.setBlue(165);
return pixel;
}
/**
* Determines if pixel coordinates are within the defined border dimension
*
* @param xValue: x-coordinate
* @param yValue: y-coordinate
*
* @return true / false
*/
function isWithinBorderDimension(xValue, yValue) {
if (xValue <= borderSize || yValue <= borderSize ||
xValue >= (imgWidth - borderSize) || yValue >= (imgHeight - borderSize)) {
return true;
}
return false;
}
/**
* Determines if pixel coordinates are within the defined horizontal blur dimension
*
* @param xValue: x-coordinate
* @param yValue: y-coordinate
*
* @return true / false
*/
function isWithinHorizontalBlur(xValue, yValue) {
if ( xValue > lowerLimit && xValue < (imgWidth - lowerLimit)
&& ( (yValue > lowerLimit && yValue < higherLimit)
|| (yValue > (imgHeight - higherLimit) && yValue < (imgHeight - lowerLimit)) ) ) {
return true;
}
return false;
}
/**
* Determines if pixel coordinates are within the defined vertical blur dimension
*
* @param xValue: x-coordinate
* @param yValue: y-coordinate
*
* @return true / false
*/
function isWithinVerticalBlur(xValue, yValue) {
if ( yValue > lowerLimit && yValue < (imgHeight - lowerLimit)
&& ( (xValue > lowerLimit && xValue < higherLimit)
|| (xValue > (imgWidth - higherLimit) && xValue < (imgWidth - lowerLimit)) ) ) {
return true;
}
return false;
}
/**
* Retrieves the nearby pixel values
* ** [adopted from lesson]
*
* @param image: image to be processed
* @param xValue: x-coordinate
* @param yValue: y-coordinate
* @param range: defined area within the image
*
* @return edited pixel
*/
function getPixelNearby(image, xValue, yValue, range) {
var dx = Math.random() * range - range / 2;
var dy = Math.random() * range - range / 2;
var nx = isInImage(xValue + dx, image.getWidth());
var ny = isInImage(yValue + dy, image.getHeight());
return image.getPixel(nx, ny);
}
/**
* Determines if the x- or y- coordinate is within the image size
* ** [adopted from lesson]
*
* @param coordinate: x- or y- coordinate
* @param size: image size
*
* @return 0 / size - 1 / coordinate value
*/
function isInImage(coordinate, size) {
// coordinate cannot be negative
if (coordinate < 0) return 0;
// coordinate must be in range [0 .. size-1]
if (coordinate >= size) return size - 1;
return coordinate;
}
|
|