Log in

View Full Version : Getting BinaryImage write to work


loafdog
02-02-2012, 14:02
Hi,

My team has tried using BinaryImage write() but could not get any files to appear in root dir on crio. I was playing with it a bit last night and eventually got images to appear. I have a few questions.
Given something like:
BinaryImage img = image.thresholdRGB(0, 40, 25, 255, 0, 40);

1- img.write("thresh.jpg"); did not work. img.write("/tmp/thresh.jpg"); did not work. img.write("/tmp/thresh.png") works. I did not try other file types. Not sure if writing a .png to / works or not... it was late and I was happy to get something working. Any ideas why using jpg does not work? Any reason writing to / may not work? I don't remember if I tried writing png to /. I am able to ftp the camera test sample jpg images to /. I did not try creating/writing to a text file yet.

2- the png image was either all black or had some red pixels/particles. We are using a green ring light. Our camera, RGB values, and filters need tuning but we should get something other than black/red. I looked in sunspotfrcsdk/lib/WPILibJ/src/edu/wpi/first/wpilibj/image/BinaryImage.java and found:

public void write(String fileName) throws NIVisionException{
Pointer colorTable = new Pointer(1024);
//Black Background
colorTable.setByte(0, (byte)0); //B
colorTable.setByte(1, (byte)0); //G
colorTable.setByte(2, (byte)0); //R
colorTable.setByte(3, (byte)0); //Alpha
//Red Particles:
colorTable.setByte(4, (byte)0); //B
colorTable.setByte(5, (byte)0); //G
colorTable.setByte(6, (byte)255); //R
colorTable.setByte(7, (byte)0); //Alpha
try {
NIVision.writeFile(image, fileName, colorTable);
} finally {
colorTable.free();
}
}

I changed two lines to:

colorTable.setByte(5, (byte)255); //G
colorTable.setByte(6, (byte)0); //R

After that saved images contained green pixels. I could see what thresholdRGB and convexHull and other filters were doing. Is this the right thing to do to get green images to appear? Seems like a hacky kind of way to make it work, but I'll take it for now.

-Maciej

RufflesRidge
02-02-2012, 14:08
A binary image, has by definition two possible states for each pixel. What color you make the two states appear as really doesn't matter. If you want them to be black and green because you have a green ring light, go for it. If you want them to orange and purple because those are you favorite colors, you can make it do that instead.

Your method seems to be the correct way of having the "true" pixels output as green to me.

mryfrmr
04-02-2012, 12:14
We have found this morning that the write method on the binaryImage does not work with .jpg format, everything comes out black. We changed the format to .png and everything looked as we expected. So use .png format.

cgmv123
04-02-2012, 18:33
We have found this morning that the write method on the binaryImage does not work with .jpg format, everything comes out black. We changed the format to .png and everything looked as we expected. So use .png format.

Or .bmp or .tiff