processing copy/duplicate resize issue - java

not posted before, so be patient.
I'm having an issue in the processing IDE with copying PImages and then resizing. Resizing a copy of an image also appears to resize the original.
void setup(){
size(10,10,P2D);
PImage img;
img = loadImage("nickwire.jpg");
println(img.width);
PImage dupe;
dupe=img;
dupe.resize(10,10);
print(img.width);print("\t");println(dupe.width);
}
//console outputs:
//263
//10 10
//I'm expecting
//263
//263 10
What am I doing wrong?

You are not creating a copy of your image, you are creating a new reference and pointing it to the same image.
In order to copy the image to the new reference, take a look at the PImage#get() method.

Related

javaFx Image Height and Width got backwards?

I am making a game in javaFx. The way entities in my game is rendered is dependent on one of their fields called "angle", like this:
gc.save();
gc.transform(new Affine(new Rotate(angle, getCenterX(), getCenterY())));
gc.drawImage(image, x, y);
gc.restore();
"Gc" is the GraphicsContext. The getCenter methods will use the height and width of the images. However, the images are not rendered properly. I ran some tests and found that when I use the get Width method, it actually returns the height of the image. When I call the get Height method, it returns the width. My guess is that it's because the png images I use have been rotated 90 degrees in respect to their original state. However, when I go to preview and look at their size, it seems that everything is fine. I tried duplicating the images, but it doesn't really work. Any ideas how to fix this? I don't have a verifiable code example because I believe this question is more related to properties of png files and javaFx's Image methods than my program's logic.
Here is the verifiable code example:
import javafx.embed.swing.JFXPanel;
import javafx.scene.image.Image;
public class TestImage {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFXPanel jfxPanel = new JFXPanel();
Image BLUE01 = new Image("/blue01.png");
System.out.println("height : "+ BLUE01.getHeight());
System.out.println("wdith : "+ BLUE01.getWidth());
}
}
And here is the image:
It seems that the problem comes from not my code but my use of IDE... It turns out that after I edit or add image in my resource folder, I need to click "refresh" in eclipse...

Resize and crop an image using ImageJ

I'm trying to resize and crop an image using ImageJ. Here's the code:
ImagePlus ip1 = IJ.openImage("_Pic.jpg");
ImagePlus ip2 = IJ.openImage("_Pic.jpg");
ImageProcessor imgP1 = ip1.getProcessor();
ImageProcessor imgP2 = ip2.getProcessor();
FileSaver fs1 = new FileSaver(ip1);
FileSaver fs2 = new FileSaver(ip2);
/* Trying to resize */
imgP1.resize(100); // also tried with width and height
fs1.saveAsJpeg("Resized.jpg");
/* Trying to crop */
imgP2.setRoi(100, 100, 200, 200);
imgP2.crop();
fs2.saveAsJpeg("Cropped.jpg");
Unfortunately, the newly created files are identical to the original one.
So far I've found out how to blur, smooth, invert, translate, rotate, ..., but these two are giving me hard time. Anybody has an idea?
Your cross-posted question to the ImageJ forum was answered there by Stefan Helfrich:
If you take a look at the Javadocs for ImageProcessor you'll see that resize() as well as crop() return new ImageProcessor instances and do not operate on this. That's why you'll have to use the ImagePlus.setProcessor(ImageProcessor) method to add the returned ImageProcessors to ip1 and ip2.
When cross-posting like this, please always include links to the other posts, so people finding this question later will have a chance to follow the discussion.
circle crop: https://youtu.be/OyiOFh1pD3k
resize: https://youtu.be/N_jddMMhzqc
combine both code.

Java Icon Image Maximum File Size

I am working on a chess game on Java. I have been importing images onto Eclipse and then assigning them to ImageIcons, and then subsequently assigning these ImageIcons onto buttons to form a grid.
At one point three out of my four bishop images were not being assigned to their respective buttons and so I looked at the file size and it turns out that the sizes of the three images that weren't being assigned were ~1,100KB, ~1,200KB, and ~40KB. The image that was being assigned to the button was around 25KB. I thought this was odd (especially since all four images are very similar) so I exported the three problematic images in a lower resolution (all under 30KB), and then re-imported them into Eclipse. When I ran my program again they were assigned to the right buttons and everything ran smoothly again.
The buttons that I am using are all 75 x 75 pixels, and the pixels were the same for each image (75 x 75), so I am confused why this happened. I looked for any questions relating to this, but I could not find any. If anyone could help explain why this could happen to me that would be very helpful so I can avoid this problem in the future.
I've definitely loaded images much bigger than that into ImageIcons and other components, so I suspect that your issue is that when you are assigning the Image to the ImageIcon before the Image is fully loaded. You can use MediaTracker to help solve this problem. From ImageIcon:
/** * Loads an image into memory */
public static Image loadImage(String fn){
try {
Image image=java.awt.Toolkit.getDefaultToolkit().createImage(fn);
MediaTracker tracker=new MediaTracker(lblForFM); tracker.addImage(image,0);
tracker.waitForID(0);
if (MediaTracker.COMPLETE != tracker.statusID(0,false)) throw new
IllegalStateException("Unable to load image from " + fn);
else return image; } catch ( InterruptedException e) {
throw new RuntimeException("Interrupted while loading image from " + fn,e);
}
}
I recommend using png for transparent images and icons, jpg for non-transparent images - and only if compression artifacts don't matter (lossless JPEG sadly isn't widely spread). bmp is one of the worst file formats out there if it comes to file size. As suggested by the others, load images in java with the ImageIO API:
public class Program {
public static void main(String[] args) {
InputStream imageSource = Program.class.getResourceAsStream("bishop"); // may be a URL, File or ImageInputStream instead
try {
BufferedImage bishopImage = ImageIO.read(imageSource); // read image
ImageIcon bishopIcon = new ImageIcon(bishopImage); // use adapter for Icon interface
System.out.println(bishopIcon); // do something with it
} catch (IOException e) {
e.printStackTrace(); // read failed
}
}
}

BackgroundSubtractor getBackgroundImage() function return empty Image

I am using OpenCV 3.0 in Java, I want to use BackgroundSubtractor to get foreground objects and also I want to get the background image, but when I call getBackgroundImage() method of BackgroundSubtractor return me an empty image. The code is bellow:
Mat backgroundImage = new Mat();
mog2.getBackgroundImage(backgroundImage);
According to the OpenCV Documentation this method can return the background image and put the data to the given Mat:
Computes a background image.
C++: void BackgroundSubtractor::getBackgroundImage(OutputArray backgroundImage) const
Parameters:
backgroundImage – The output background image.
What will be the problem? If anyone knows about this, please let me know.
Thanks in advance!

Applying sobel filter on jpg picture with OpenCV 2.4.10 in Java

I'm trying to add sobel operator on a jpg picture with Java. I found example here: http://www.tutorialspoint.com/java_dip/applying_sobel_operator.htm but it doesn't work. Instead it prints black image. Could someone explain to me what I did wrong, please? Other imgproc functions works well.
Here is my code:
Mat sourceImage = Highgui.imread(sourcePath, Highgui.CV_LOAD_IMAGE_GRAYSCALE);
Mat destinationImage = new Mat(sourceImage.rows(), sourceImage.cols(), sourceImage.type());
Mat kernel = new Mat(kernelSize,kernelSize, CvType.CV_32F){
{
put(0,0,-1);
put(0,1,0);
put(0,2,1);
put(1,0-2);
put(1,1,0);
put(1,2,2);
put(2,0,-1);
put(2,1,0);
put(2,2,1);
}
};
Imgproc.filter2D(sourceImage, destinationImage, -1, kernel);
Highgui.imwrite(destinationPath, destinationImage);
//display
new ShowImage(sourcePath, sourceImage);
new ShowImage(destinationPath, destinationImage);
First up, is there a reason why you're not using
Imgproc.Sobel(Mat src, Mat dst, int ddepth, int dx, int dy);
I'm not sure of the Java syntax used here. When does the block of puts get executed? Is it assumed to part of a constructor for a subclass of Mat that you define ? Having said that, assuming that this does what it looks like it's intended to do then it's possible that your output file type is not correctly specified; what is the value of destinationPath ?
Have you tried opening the saved file in an alternative image viewer to determine if it's the ShowImage() code or the saved file that's at fault ?
Have you tried opening the saved file in a hex editor to see if it has 'sensible' looking values or is all zeros ?

Categories

Resources