problems with blur and sharpen code
in
Programming Questions
•
1 year ago
I am trying to make three buttons to control an image. One to blur it, one to sharpen it and one to reset it to the original. I have also added a section where if the mouse is over the button the text turns red but it doesn't seem to be working.
I have added the code. please help...
//set variables
PImage photograph, photograph2, temp_original;
int dimension;
PFont font;
int option=0;
float v = 1.0/9.0;
float[][] kernel = { { v, v, v },
{ v, v, v },
{ v, v, v } };
/* ------------------- *
* ------ SETUP ------ *
* ------------------- */
void setup() {
//set image
photograph = loadImage("Photograph.png");
photograph2 = createImage(photograph.width, photograph.height, RGB);
dimension = (photograph.width*photograph.height);
temp_original = photograph;
size (photograph.width, photograph.height+99);
image (photograph, 0, 0);
//set font
font=loadFont("Arial-Black-48.vlw");
textFont(font, 30);
noLoop();
}
/* ---------------------------------------------------------------*/
/* ------------------ *
* ------ DRAW ------ *
* ------------------ */
void draw() {
//Blur
rectMode(CENTER);
rect(photograph.width/10.5, 588, 240, 100);
textFont(font,30);
fill(0);
text("BLUR", photograph.width/13, photograph.height+55);
if ( ( (mouseY>photograph.height)&(mouseY<photograph.height+99) ) & ( (mouseX>0)&(mouseX<photograph.width/3) ) ) {
textFont(font,30);
fill (255,0,0);
text("BLUR", photograph.width/13, photograph.height+55);
}
// Sharpen
rectMode(CENTER);
rect(photograph.width/2, 588, 190, 100);
fill(0);
text("SHARPEN", photograph.width/2.92, photograph.height+55);
if ( ( (mouseY>photograph.height)&(mouseY<photograph.height+99) ) & ( (mouseX>photograph.width*0.33)&(mouseX<photograph.width*0.66) ) ) {
fill (255,0,0);
text("SHARPEN", photograph.width/2.92, photograph.height+55);
}
// Reset
rectMode(CENTER);
rect(photograph.width/1.16, 588, 205, 100);
fill(0);
text("RESET", photograph.width/1.37, photograph.height+55);
if ( ( (mouseY>photograph.height)&(mouseY<photograph.height+99) ) & ( (mouseX>photograph.width*0.66)&(mouseX<photograph.width) ) ) {
fill (255,0,0);
text("RESET", photograph.width/1.37, photograph.height+55);
}
if (option==1) {
// this happens only if one of the buttons has been pressed
//create an opaque image of the same size as the original
PImage edgeImg = createImage(photograph.width, photograph.height, RGB);
// loop through the images pixels
for (int y = 1; y <photograph.height-1; y++) { // skip top and bottom edges
for (int x = 1; x <photograph.width-1; x++) { // skip left and right edges
float sum = 0; // kernalsum fro pixel
for (int kx = -1; kx <= 1; kx++) {
//calculate the adjacent pixels baced on the kernel values
sum += kernel[ky+1][kx+1] * val;
}
}
/*
.....
for this pixel in the new image, set the gray value
based on teh sum from the kernel
.....
*/
edgeImg.pixels[y*phtograph.width + x] = color(sum);
}
}
//state th
/*
.....
insert here the algorithm for blurring / sharpening (see labs!)
......
*/
if (option==2)
// loop through the images pixels
for (int y = 1; y <photograph.height-1; y++) { // skip top and bottom edges
for (int x = 1; x <photograph.width-1; x++) { // skip left and right edges
float sum = 0; // kernalsum fro pixel
for (int kx = -1; kx <= 1; kx++) {
//calculate the adjacent pixels baced on the kernel values
sum += kernel[ky+1][kx+1] * val;
}
}
/*
.....
for this pixel in the new image, set the gray value
based on teh sum from the kernel
.....
*/
edgeImg.pixels[y*phtograph.width + x] = color(sum);
}
//state that there are changes to edgeImg.pixels[]
edgeImg.updatePixels();
image(edgeImg, 100, 0); // draw the new image
if (option==3) {
loadImage("photograph.png");
}
}
/* ---------------------------------------------------------------*/
/* ------------------------- *
* ----- MOUSE PRESSED ----- *
* ------------------------- */
void mousePressed() {
//set 'if' options
if ( (mouseY>photograph.height)&(mouseY<photograph.height+99) ){
// -- BLUR
if ( (mouseX>0)&(mouseX<photograph.width*0.33) ) {
option=1;
kernel = { { v, v, v },
{ v, v, v },
{ v, v, v } };
}
// -- SHARPEN
else if ( (mouseX>photograph.width*0.33)&(mouseX<photograph.width*0.66) ) {
option=2;
kernel = { { -1, -1, -1 },
{ -1, 9, -1 },
{ -1, -1, -1 } };
}
// -- RESET
else if ( (mouseX>photograph.width*0.66)&(mouseX<photograph.width) ) {
option=3;
photograph = temp_original
}
}
}
/* ---------------------------------------------------------------*/
1