We are about to switch to a new forum software. Until then we have removed the registration on this forum.
import java.util.*;
class processImage{
int[][] kernal = {{-1,-1,-1},{-1,9,-1},{-1,-1,-1}};
int[][] edgeDetectionkernal = {{-1,-1,-1},{-1,8,-1},{-1,-1,-1}};
int[][] boxBlur = {{1,1,1},{1,1,1},{1,1,1}};
int[][] gaussianBlur = {{1,4,6,4,1},{4,16,24,16,4},{6,24,36,24,6},{4,16,24,16,4},{1,4,6,4,1}};
int[][] unsharpMasking = {{1,4,6,4,1},{4,16,24,16,4},{6,24,-476,24,6},{4,16,24,16,4},{1,4,6,4,1}};
int[][] blurApproximation={{1,2,1},{2,4,2},{1,2,1}};
processImage()
{
}
//start convolution that is contrast
color convolution(PImage image,int x,int y)
{
int red=0,green=0,blue=0;
for(int j=0;j<kernal.length;j++)
{
for(int i=0;i<kernal.length;i++)
{
int xoff = x+i;
int yoff = y+j;
int loc = xoff+yoff*image.width;
loc = constrain(loc,0,image.pixels.length-1);
red += red(image.pixels[loc])*kernal[i][j];
green += green(image.pixels[loc])*kernal[i][j];
blue += blue(image.pixels[loc])*kernal[i][j];
}
}
red= constrain(red,0,255);
green= constrain(green,0,255);
blue= constrain(blue,0,255);
return color(red,green,blue);
}
//end convolution
//start edge detection
color edgeDetection(PImage image,int x,int y)
{
int red=0,green=0,blue=0;
for(int j=0;j<edgeDetectionkernal.length;j++)
{
for(int i=0;i<edgeDetectionkernal.length;i++)
{
int xoff = x+i;
int yoff = y+j;
int loc = xoff+yoff*image.width;
loc = constrain(loc,0,image.pixels.length-1);
red += red(image.pixels[loc])*edgeDetectionkernal[i][j];
green += green(image.pixels[loc])*edgeDetectionkernal[i][j];
blue += blue(image.pixels[loc])*edgeDetectionkernal[i][j];
}
}
red= constrain(red,0,255);
green= constrain(green,0,255);
blue= constrain(blue,0,255);
return color(red,green,blue);
}
//end Edge detection
//Code to blur With normalized and weighted mtrix
color blurEffect(PImage image,int x,int y,String what)
{
int red=0,green=0,blue=0;
if(what.equalsIgnoreCase("boxblur"))
{
for(int j=0;j<boxBlur.length;j++)
{
for(int i=0;i<boxBlur.length;i++)
{
red+=red(image.get(x+i,y+j))*boxBlur[i][j];
green+=green(image.get(x+i,y+j))*boxBlur[i][j];
blue+=blue(image.get(x+i,y+j))*boxBlur[i][j];
}
}
red /= findSum(boxBlur);
green /= findSum(boxBlur);
blue /= findSum(boxBlur);
return color(red,green,blue);
}
else if(what.equalsIgnoreCase("gaussian")){
for(int j=0;j<blurApproximation.length;j++)
{
for(int i=0;i<blurApproximation.length;i++)
{
red+=red(image.get(x+i,y+j))*blurApproximation[i][j];
green+=green(image.get(x+i,y+j))*blurApproximation[i][j];
blue+=blue(image.get(x+i,y+j))*blurApproximation[i][j];
}
}
red /= findSum(blurApproximation);
green /= findSum(blurApproximation);
blue /= findSum(blurApproximation);
return color(red,green,blue);
}else if(what.equalsIgnoreCase("gaussianLarge")) {
print("large");
for(int j=0;j<gaussianBlur.length;j++)
{
for(int i=0;i<gaussianBlur.length;i++)
{
red+=red(image.get(x+i,y+j))*gaussianBlur[i][j];
green+=green(image.get(x+i,y+j))*gaussianBlur[i][j];
blue+=blue(image.get(x+i,y+j))*gaussianBlur[i][j];
}
}
red /= findSum(gaussianBlur);
green /= findSum(gaussianBlur);
blue /= findSum(gaussianBlur);
return color(red,green,blue);
}else if(what.equalsIgnoreCase("unsharpMasking")) {
for(int j=0;j<unsharpMasking.length;j++)
{
for(int i=0;i<unsharpMasking.length;i++)
{
red+=red(image.get(x+i,y+j))*unsharpMasking[i][j];
green+=green(image.get(x+i,y+j))*unsharpMasking[i][j];
blue+=blue(image.get(x+i,y+j))*unsharpMasking[i][j];
}
}
red /= findSum(unsharpMasking);
green /= findSum(unsharpMasking);
blue /= findSum(unsharpMasking);
return color(red,green,blue);
}
return color(0);
}
//finding the total value of the normalized matrix
private int findSum(int[][] filter)
{
int sum=0;
for(int i=0;i<filter.length;i++)
{
for(int j=0;j<filter[i].length;j++)
{
sum+=filter[i][j];
}
}
return sum;
}
// end blur code
//filters :
//1>Airthmatic Mean Filter
color airthmaticMeanFilter(PImage image,int x,int y)
{
int value=0;
int red=0,green=0,blue=0;
for(int j=0;j<3;j++){
for(int i=0;i<3;i++){
int xoff = x+j;
int yoff = y+i;
int loc = constrain(xoff+yoff*image.width,0,image.pixels.length-1);
red += red(image.pixels[loc]);
green += green(image.pixels[loc]);
blue += blue(image.pixels[loc]);
value++;
}
}
red /= value;
green/=value;
blue /=value;
return color(red,green,blue);
}
//2> Harmonic Filter
color harmonicFilter(PImage image,int x,int y)
{
float red=0,green=0,blue=0;
for(int j=0;j<2;j++){
for(int i=0;i<2;i++){
int xoff = x+i;
int yoff = y+j;
int loc= xoff+yoff*image.width;
loc = constrain(loc,0,image.pixels.length-1);
red += 1/red(image.pixels[loc]);
green += 1/green(image.pixels[loc]);
blue += 1/blue(image.pixels[loc]);
}
}
red = 4/red;
green = 4/green;
blue = 4/blue;
return color(red,green,blue);
}
//3> Median Filter:
color median(PImage image,int x,int y){
float red,green,blue;
ArrayList<Integer> array = new ArrayList<Integer>();
for(int j=0;j<3;j++)
{
for(int i=0;i<3;i++)
{
int xoff = x+i;
int yoff = y+j;
int loc = xoff+yoff*image.width;
loc = constrain(loc,0,image.pixels.length-1);
array.add(image.pixels[loc]);
}
}
Collections.sort(array);
int size = array.size();
if(size %2 ==0)
{
int index = (size/2)+1;
red = red(array.get(index));
green = green(array.get(index));
blue = blue(array.get(index));
}
else{
int index = (size+1)/2;
red = red(array.get(index));
green = green(array.get(index));
blue = blue(array.get(index));
}
red=constrain(red,0,255);
green=constrain(green,0,255);
blue=constrain(blue,0,255);
return color(red,green,blue);
}
//Min Filter
color minFilter(PImage image,int x,int y){
ArrayList<Integer> array = new ArrayList<Integer>();
float red=0,green=0,blue=0;
for(int j=0;j<3;j++)
{
for(int i=0;i<3;i++)
{
int xoff = x+i;
int yoff = y+j;
int loc = constrain(xoff+yoff*image.width,0,image.pixels.length-1);
array.add(image.pixels[loc]);
}
}
int value = Collections.min(array);
red = red(value);
green = green(value);
blue = blue(value);
return color(red,green,blue);
}
//Max Filter
color maxFilter(PImage image,int x,int y){
ArrayList<Integer> array = new ArrayList<Integer>();
float red=0,green=0,blue=0;
for(int j=0;j<3;j++)
{
for(int i=0;i<3;i++)
{
int xoff = x+i;
int yoff = y+j;
int loc = constrain(xoff+yoff*image.width,0,image.pixels.length-1);
array.add(image.pixels[loc]);
}
}
int value = Collections.max(array);
red = red(value);
green = green(value);
blue = blue(value);
return color(red,green,blue);
}
color midFilter(PImage image,int x,int y){
ArrayList<Integer> array = new ArrayList<Integer>();
float red=0,green=0,blue=0,alpha=0;
for(int j=0;j<3;j++)
{
for(int i=0;i<3;i++)
{
int xoff = x+i;
int yoff = y+j;
int loc = constrain(xoff+yoff*image.width,0,image.pixels.length-1);
array.add(image.pixels[loc]);
}
}
int value = Collections.min(array);
int value1 = Collections.max(array);
int result = (value+value1)/2;
red = red(result);
green = green(result);
blue = blue(result);
return color(red,green,blue);
}
}
Comments
@Anupam It will be great if you can provide an example using the filter and adding a bit of details of what your code does. You could even provide a sample image here in the forum for other users to see the same results.
Kf
@Anupam -- you requested feedback -- what kind of feedback are you looking for? Ideas for optimization?
Yup if u use this class you will find some of the filter taker more time to do work
a lot of the code for each of the filters is VERY similar. the whole point of using a kernel is that the code is common, only the kernel changes.
indentation is terrible. use ctrl-t to fix it.
red /= findSum(boxBlur); green /= findSum(boxBlur); blue /= findSum(boxBlur);
you are calling findSum 3 times for every pixel. the return value is the same every time. call it once, save it in a variable. (i've pointed this out in a question you've asked before.)
use bitshifting for speed.
don't call things red. there's a processing method called red() and this is confusing. ditto green and blue.
10 blank lines. why?