Median filter implementation using java - java

I am implementing a median filter using java, but the output i am getting is not satisfactory . I have done using a 3*3 neighborhood . Here is my code :
for(i=1;i<height-1;i++)
{
for(j=1;j<width-1;j++)
{
Color c=new Color(img.getRGB(j,i));
red=c.getRed();
green=c.getGreen();
blue=c.getBlue();
x=j;
y=i;
x1=x-1;
y1=y;
x2=x+1;
y2=y;
x3=x;
y3=y-1;
x4=x;
y4=y+1;
x5=x+1;
y5=y+1;
x6=x-1;
y6=y-1;
x7=x-1;
y7=y+1;
x8=x+1;
y8=y-1;
Color c1=new Color(img.getRGB(x1,y1));
red1=c1.getRed();
green1=c1.getGreen();
blue1=c1.getBlue();
Color c2=new Color(img.getRGB(x2,y2));
red2=c2.getRed();
green2=c2.getGreen();
blue2=c2.getBlue();
Color c3=new Color(img.getRGB(x3,y3));
red3=c3.getRed();
green3=c3.getGreen();
blue3=c3.getBlue();
Color c4=new Color(img.getRGB(x4,y4));
red4=c4.getRed();
green4=c4.getGreen();
blue4=c4.getBlue();
Color c5=new Color(img.getRGB(x5,y5));
red5=c5.getRed();
green5=c5.getGreen();
blue5=c5.getBlue();
Color c6=new Color(img.getRGB(x6,y6));
red6=c6.getRed();
green6=c6.getGreen();
blue6=c6.getBlue();
Color c7=new Color(img.getRGB(x7,y7));
red7=c7.getRed();
green7=c7.getGreen();
blue7=c7.getBlue();
Color c8=new Color(img.getRGB(x8,y8));
red8=c8.getRed();
green8=c8.getGreen();
blue8=c8.getBlue();
a[0]=red1;
a[1]=red2;
a[2]=red3;
a[3]=red4;
a[4]=red5;
a[5]=red6;
a[6]=red7;
a[7]=red8;
a=bubbleSort(a);
redf=a[2];
a[0]=green1;
a[1]=green2;
a[2]=green3;
a[3]=green4;
a[4]=green5;
a[5]=green6;
a[6]=green7;
a[7]=green8;
a=bubbleSort(a);
greenf=a[2];
a[0]=blue1;
a[1]=blue2;
a[2]=blue3;
a[3]=blue4;
a[4]=blue5;
a[5]=blue6;
a[6]=blue7;
a[7]=blue8;
a=bubbleSort(a);
bluef=a[2];
Color nc=new Color(redf,greenf,bluef);
img.setRGB(x,y,nc.getRGB());
}
}
The outer loop is from 1 to height-1 . It's not getting printed

As it was mentioned, you are using same image img for both reading and writing into, so it affects the pixels calculations. You may also visit this JavaScript tutorial http://fiveko.com/tutorials/dip/median. It describes a histogram based approach for median values extraction and seems promising for bigger kernel sizes.

Related

Swing JLabel icon: use same image for all resolutions

I know they are quite a few forums that kinda talks about this, but my problem is that I have a JLabel with an icon which I do NOT want to scale when resolution is <> 100% ! I.e. (sorry, in french):
Initially, I set my JLabel icon with something like:
lblImage.setIcon( getNoteIcon() );
//...
ImageIcon getNoteIcon(){
return new ImageIcon( getClass().getResource('path/to/file.png') );
}
This results are that my image displays blurry when HiDPI resolution is set to higher than 100%. Examples:
At 100%:
And at 150%:
You can see that the music note image is fuzzier in the 150% resolution.
What I want: I DO NOT want it to up or downscale the image! I want to keep the original size, unless I can provide different image sizes for various resolutions.
That being said, I looked and tried to use the BaseMultiResolutionImage class to deal with resolutions, passing along an array of the "same size image", but I get the same results when I change the resolution. This is the code I tried :
ImageIcon getNoteIcon(){
List<Image> imgList = new ArrayList<Image>();
/* all images are width = 108 height = 83 */
imgList.add(ImageIO.read(new File('path/to/file.png'))); // 100%
imgList.add(ImageIO.read(new File('path/to/file.png'))); // 125%
imgList.add(ImageIO.read(new File('path/to/file.png'))); // 150%
imgList.add(ImageIO.read(new File('path/to/file.png'))); // 175%
imgList.add(ImageIO.read(new File('path/to/file.png'))); // 200%
BaseMultiResolutionImage multiResolutionImage = new BaseMultiResolutionImage(imgList.toArray(new Image[0]));
return new ImageIcon(
multiResolutionImage
);
}
I don't understand what I'm doing wrong, or if I'm even using BaseMultiResolutionImage correctly. Finally, if I do manage say to give 3 different images for 3 resolutions, (i.e. 100, 125, 150%), is there a way to tell it the "default" one to use when I do not have an image for a given resolution (i.e. 200%) ?
Can anyone shed a light on all of this? Seems like a lot of confusion on my end just to get an image to NOT scale in my window...
Much thanks!
Pat
UPDATE AFTER "PROPER" USE OF BaseMultiResolutionImage:
try {
List<Image> imgList = new ArrayList<Image>();
imgList.add(ImageIO.read(getClass().getResource("images/notes/hidpi_100/file.png"))); // 100% - 108x83
imgList.add(ImageIO.read(getClass().getResource("images/notes/hidpi_125/file.png"))); // 125% - 135x104
imgList.add(ImageIO.read(getClass().getResource("images/notes/hidpi_150/file.png"))); // 150% - 162x124
imgList.add(ImageIO.read(getClass().getResource("images/notes/hidpi_175/file.png"))); // 175% - 189x145
imgList.add(ImageIO.read(getClass().getResource("images/notes/hidpi_200/file.png"))); // 200% - 216x166
BaseMultiResolutionImage multiResolutionImage = new BaseMultiResolutionImage(imgList.toArray(new Image[0]));
ImageIcon icon = new ImageIcon(multiResolutionImage);
return icon;
}
catch(IOException ex){
ex.printStackTrace();
return null;
}
On screen font DPI set to 175% in Windows, this results in:
Before using BaseMultiResolutionImage
After using BaseMultiResolutionImage
Here is a comparison of what I get (left), with the actual image file it's supposed to show on the right hand side (mind you Paint doesn't take into account background transparency):
Any ideas as to why this "choppy" image is showing? What am I missing ?
Thanks again.
Pat

Custom Coloring in Tableau by Row

I've exhausted my understanding of Tableau. I'm trying to color every icon below the risk average the color red, and above the color green. Here is an attempt at an articulation in Java of my intention (this code may not be correct, but I want the intention translated into Tableau).
This is my Tableau code that doesn't work:
IF [Commercial Services] <= [Risk Average] *The variable I want to create*
THEN "Red"
ELSEIF [Commercial Services] > [Risk Average]
THEN "Green"
END
Here's the Java code I think is closer to what I need converted:
final int NUM_ROWS = 39;
int count = 0;
while(count < NUM_ROWS) {
if(count.commercial_services < count.risk_average) {
shape.color = "Red";
} elseif(count.commercial_services > count.risk_average) {
shape.color = "Green";
} else {
shape.color = "Blue";
}
count++;
}
Please help me create a variable that I could use to color each risk in reference to the risk average.
I have attached a spreadsheet and an image for reference.
Thanks in advance.
That is not the way Tableau works. For such purposes, you should first create a flag-like calculated field to form values according to your conditions. And then use that field as a shape or color identifier dragging it to the Marks card.
So for your case you may create a field like below (which is almost the same you did), which means it will have the value of "Red" for CS<=RA condition and "Green" for the other condition. Only that Tableau does not know what to do with these string variables at the moment. You may assign -1 or 1 instead of these strings, it does make no change.
IF [Commercial Services] <= [Risk Average]
THEN "Red"
ELSE "Green"
END
And now you have new column in your data which have "Red" or "Green" values at each row. You may now drag this field to Marks card and select is as Color or as normal pill and select its type as Shape then use standard arrows or make your custom shapes according to your needs. At this phase, you will be selecting with which colours or shapes should your "Red" & "Green" values be represented by.
I or someone else might be in more help if you share your tableau workbook with what you have done so far.

How to calibrate the Lego NXT color sensor with LeJos NXJ?

I'm currently writing on a program about detecting different colored balls and sorting them with the Lego NXT and its color sensor.
At the beginning it worked quite good but now, the color sensor always returnes the color ID "7" (white), no matter what i do.
On the documentation page i found something about a calibration (calibrateHigh() and calibrateLow()).
Does anybody know how to use this calibration or is my color sensor broken?
I tried it with this code:
package com.mydomain;
import lejos.nxt.*;
public class HelloWorld {
public static void main(String[] args) throws Exception {
ColorSensor color = new ColorSensor(SensorPort.S2);
while (true) {
LCD.drawInt(color.getColorID(), 1, 1, 1);
}
}
}
First you have to be aware of these conditions:
The color sensor should be about 1cm over the color.
The darker your room is, the more your sensor can detect.
So here is my code to control if the color is white:
public boolean isWhite() {
//Gives "True" if the color is white
ColorSensor cs = new ColorSensor(SensorPort.S3);
cs.setFloodlight(false);
if (cs.getColor().getColor() == Color.WHITE) {
return true;
}
return false;
}
Explanation:
The first cs.getColor() in the if - clause gives the rgb value of the color back.
If you call the method cs.getColor() after the first cs.getColor() (so it will be: cs.getColor().getColor) than it will return its color ID.
In this case Color.WHITEis 6.
You can see all numbers here.
Note:
I'm sorry for my bad english and also sorry if this explanation isn't detailed too! I'm not really a java programmer, I just needed that for my school project, therefore sorry that I couldn't explained it more detailed, but I hope this answer can help some people! ;)

setting more than eight colors for data series in JavaFX ScatterChart

I'm trying to set the colors of eleven data series in a ScatterChart. Unfortunately JavaFX repeats colors and symbols every eight series due to the nextClearBit which is set to mudulo eight by design. There is a solution to overcome this by setting the colors manually for each data series in CSS like:
.default-color0.chart-series-line { -fx-stroke: #e9967a; }
.default-color1.chart-series-line { -fx-stroke: #f0e68c; }
.default-color2.chart-series-line { -fx-stroke: #dda0dd; }
...and so on.
This works for LineCharts. So I tryed a similar approach for my ScatterChart, but...
.default-color0.chart-symbol { -fx-stroke: #e9967a; }
.default-color1.chart-symbol { -fx-stroke: #f0e68c; }
.default-color2.chart-symbol { -fx-stroke: #dda0dd; }
...and so on works until...
.default-color8.chart-symbol { -fx-stroke: #e9a0dd; }
At this point Java ignores my CSS and jumps to the formatting of the first data series following the nextClearBit rule.
Does anyone know how to fix this?
JavaFX Scatter chart repeats its color combination after the 8th series. So there is no styleclass .default-color8 in the JavaFX style css file modena.css.
If you want to color your series, you should be depend on the styleclass which represents the series and not the color itself.
Try using series0, .series1, and so on, as your style class instead of .default-color0, .default-color1 ...

Drawing in Java using Hexadecimal

I've seen this in a code of tetris game and I'm wondering how these values are able to draw the line, T-shape, S-shape, Z-shape, L-shape, inverted-L and a sqaure.
I get the four parametes maybe because of the four blocks needed per piece. But how did they come up with values like that? Have those something to do with the color too?
int blocks[][] = {
{0x0f00, 0x4444, 0x0f00, 0x4444}, // LINE
{0x04e0, 0x0464, 0x00e4, 0x04c4}, // T
{0x4620, 0x6c00, 0x4620, 0x6c00}, // S
{0x2640, 0xc600, 0x2640, 0xc600}, // Z
{0x6220, 0x1700, 0x2230, 0x0740}, // 7
{0x6440, 0x0e20, 0x44c0, 0x8e00}, // inverted 7
{0x0660, 0x0660, 0x0660, 0x0660}, // square
}
I'm new in Java and I want to learn to "draw" using those values. Thank you very much!
Each row of the table is one shape. Each column is a different rotation for the shape. The square, for instance, is the same no matter how you rotate it. The line (first row) flips between two patterns. I found this in a web search:
http://codeincomplete.com/posts/2011/10/10/javascript_tetris/

Categories

Resources