Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.eclipse.swt.graphics.Image;
- import org.eclipse.swt.graphics.ImageData;
- //The class for carrying out the filtering
- public class ImgFil{
- // Median filter
- public Image filMedian(Image image) {
- // indice for expressing the x,y differences from the center
- int[] iDx = { -1, 0, 1, -1, 0, 1, -1, 0, 1 };
- int[] iDy = { -1, -1, -1, 0, 0, 0, 1, 1, 1 };
- // Getting the pixel array of the original image 3
- ImageData imd = image.getImageData();
- // Setting the pixel array for the new result image
- ImageData imd2 =image.getImageData();
- for (int y = 1; y < (imd.height-1); y++) {
- for (int x = 1; x < (imd.width-1); x++) {
- int iRsum = 0;
- int iGsum = 0;
- int iBsum = 0;
- for (int i = 0; i < 9; i++) {
- int iC = imd.getPixel(x+iDx[i], y+iDy[i]);
- int iR = iC & 0x00ff;
- int iG = (iC >> 8) & 0x00ff;
- int iB = (iC >> 16) & 0x00ff;
- // Adding the RGB values of pixels in the eight neighborhood repeatedly
- iRsum += iR;
- iGsum += iG;
- iBsum += iB;
- }
- // Getting 5th large number
- int iRd = iRsum;
- int iGd = iGsum;
- int iBd = iBsum;
- int iCd = (iBd << 16) + (iGd << 8) + iRd;
- imd2.setPixel(x, y, iCd);
- }
- }
- Image newImage = new Image(null, imd2);
- return newImage;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement