Detecting position of crop marks in a scanned document - java

I am working on a project in which user gives us a scanned copy of a form, and we need to identify various fields within the image.
I have a form like this:
The form has four crop marks/ registration marks at four corners of the page. Now the user is supposed to fill the form and give back a scanned copy of his filled form. The scanned form that we get may have been rotated by some angle. For instance see the rotated form below:
Now to extract the exact fields out of form, i.e. to extract any particular field like Title in the scanned form, we need to have exact coordinates but since our image has been rotated by unknown angle. We are unable to do so.
I read about registration marks and their use to align the page to a standard form.
I tried searching for these registration marks in the image, but since it is rotated we may not find the position of the mark in the rotated image.
I tried searching for the issue and found some questions on SO which although gave some direction but couldn't help much.
This Question gave me reference to LEADTools SDK which has functions to perform this tasks. Unfortunately this SDK is not for JAVA and along with that it's proprietary and not free.
Is there any other open source tools for the same purpose.
Additionally I am open to suggestions about other methods used to align the form.

You could use the coordinates of the markers in the corner of the document. Using the coordinates of the corners of the lines, you could measure the rotation angle of the paper in order to compensate it.
The post below addresses a similar issue:
"Image Processing Edge Detection in Java"
Below an approach to detect the coordinates using Java and Marvin.
output (some noise cause of the JPEG compression):
source code:
import marvin.image.MarvinImage;
import marvin.io.MarvinImageIO;
import marvin.util.MarvinAttributes;
import static marvin.MarvinPluginCollection.*;
public class DocumentMarks {
public DocumentMarks(){
MarvinImage image = MarvinImageIO.loadImage("./res/document.jpg");
thresholding(image, 250);
MarvinAttributes ret = moravec(image.clone(), image, 5, 100000);
image = showCorners(image, ret, 4);
MarvinImageIO.saveImage(image, "./res/document_out.jpg");
}
private static MarvinImage showCorners(MarvinImage image, MarvinAttributes attr, int rectSize){
MarvinImage ret = image.clone();
int[][] cornernessMap = (int[][]) attr.get("cornernessMap");
int rsize=0;
for(int x=0; x<cornernessMap.length; x++){
for(int y=0; y<cornernessMap[0].length; y++){
// Is it a corner?
if(cornernessMap[x][y] > 0){
rsize = Math.min(Math.min(Math.min(x, rectSize), Math.min(cornernessMap.length-x, rectSize)), Math.min(Math.min(y, rectSize), Math.min(cornernessMap[0].length-y, rectSize)));
ret.fillRect(x, y, rsize, rsize, Color.red);
}
}
}
return ret;
}
public static void main(String[] args) {
new DocumentMarks();
System.exit(0);
}
}

I successfully identified crop marks in the scanned document. I've described the approach on my blog here.

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...

How do I supply a Color Model to a .tiff image in Java when the image does not provide a Color Model to be used?

I am fairly new to Java, and so some of the classes like Color Model and JAI are not familiar to me. I have a tiff image I am reading into a program in Java. This is my read-in code:
BufferedImage img = null;
String input[] = {"LE70160412002112EDC00_sr_band5", "LE70160412002144EDC00_sr_band5"};
String filetype = "tif";
File file = new File(input[0] + "output.csv");
int numFiles = 0;
while (numFiles < 2){
if (filetype == "tif"){
FileSeekableStream stream = new FileSeekableStream(new File(input[numFiles] + ".tif"));
TIFFDecodeParam decodeParam = new TIFFDecodeParam();
decodeParam.setDecodePaletteAsShorts(true);
ParameterBlock params = new ParameterBlock();
params.add(stream);
RenderedOp image1 = JAI.create("tiff", params);
img = image1.getAsBufferedImage();
}
}
To be clear, other things are done further down in the while loop that I excluded such that the files are not overwriting each other. The problem I am having is not being able read in the file and get further into the loop. I had a tiff file that only had black and white pixels (0 or 255 red value for all pixels), and the code ran correctly because the file supplied the Color Model. The new tiff file I am trying to use is a greyscale picture (0 to 255 red value for all pixels), and every time I run the code it gives me the following error message:
Exception in thread "main" java.lang.IllegalArgumentException: No ColorModel is supplied and the image ColorModel is null.
at javax.media.jai.PlanarImage.getAsBufferedImage(PlanarImage.java:2500)
at javax.media.jai.PlanarImage.getAsBufferedImage(PlanarImage.java:2546)
at Soda.DoStuff.doStuff(DoStuff.java:60)
at Soda.Driver.main(Driver.java:6)
My first instinct given the error message is to create a new Color Model. There may also be a better way to use JAI to import the tiff file such that it supplies the Color Model for the greyscale image. My end goal is to get the red value for each pixel in the image, so I do not want the data coming in to be altered from it's original form.
Any help I can get would be much appreciated. I am open to any suggestions.
EDIT:
Someone commented to try and use the getDefaultColorModel class inside the PlanarImage library, so I changed the bottom line of the code to this:
cm = PlanarImage.getDefaultColorModel(0, 1); //
img = image1.getAsBufferedImage(null, cm);
This also did not completely work but provided a different error message:
Exception in thread "main" java.lang.IllegalArgumentException: SampleModel and ColorModel parameters must be non-null.
at com.sun.media.jai.util.JDKWorkarounds.areCompatibleDataModels(JDKWorkarounds.java:363)
at javax.media.jai.PlanarImage.getAsBufferedImage(PlanarImage.java:2505)
at Soda.DoStuff.doStuff(DoStuff.java:64)
at Soda.Driver.main(Driver.java:6)
I have extensively read through https://docs.oracle.com/cd/E17802_01/products/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/PlanarImage.html to learn about the PlanarImage class, but I still cannot figure out how to properly format a ColorModel. (0,1) creates a color model with 8 pixel bits and 1 component. I also tried with (1,1) which creates a color model with 16 pixel bits and 1 component. Both provided the same error message above.
EDIT2: Unfortunately, I cannot link the image itself. However, I can tell you how I got the image from USGS. Forewarning, getting this image requires you to make a free account, and then afterwards you have to 'order' the picture from USGS, which is simply they need to process the request and give a download link for a zip file. It WILL take some time before you can actually access the picture. I also suggest making the account first because it will not let you start the image checkout process until you have an account made.
Using this link, http://earthexplorer.usgs.gov/, you pick a point on the map, then set the date range so that it ends on 12/31/2002 (the start date does not matter). The go to the Data set tab where under the "Landsat Archive" bullet, you will hit the checkbox for "Landsat Surface Reflectance - L7 ETM+". Hit OK on the dialogue, then hit "Results" at the bottom of the screen.
Once you have signed into your account and done this search, you should see many images on the left side of your screen with similar names to the filenames in my code above. You want to hit the shopping cart next to one of the images (You only need one, my whole project required 2, but for the purposes of reading in the file, that's not necessary). The shopping cart should turn green. Then in the top right corner there is a link to an Item Basket. You hit "Proceed to Checkout" and "Submit Order" on successive screens, and then you wait for an email from USGS.
Finally unzip the file, and you should have about 10 images. As you can see in the code, I am using the image with the name "sr_band5", but I believe any of the bands are greyscale which I cannot read in. Hope this can help.

Store sprites from spritesheet in array Libgdx

I have 960x960 spritesheet stored as a png inside of my Libgdx android assets. In a class I have specified towards initializing sprites for use in my game, I am trying to make it so there is a 120x120 sprite cut from the spritesheet (so there should be 64 items in the array). How am I able to do this? This is what I have tried in a similar situation:
public static Texture spritesheet;
public static Sprite[] textures = new Sprite[64];
...
//inside method that gets called once to initialize variables
spritesheet = new Texture(
Gdx.app.getType() == Application.ApplicationType.Android ?
"...Spritesheet.png" :
"android/assets/...Spritesheet.png"
);
spritesheet.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
for (int x = 0; x < 64; x ++) {
textures[x] = new Sprite(spritesheet, x, (x%8), 64, 64);
textures[x].flip(false, true);
}
I then render the sprite in other classes with this:
batch.draw(Assets.textures[0 /*this can be any number*/], (float) x, (float) y, 108, 108);
When I do this, it acts really weird. It says there are elements filled in the array, but there's still Array Index Out of Bounds Exceptions or the sprites just render crazily. Overall, it's not working out. What I'm trying to do is make it so I don't have to initialize 64 different sprites separately, and make it so I can easily change the sprite by changing the index inputted when rendering the sprite so I can do some other things later on, like an animation. How can I go about doing this?
You should use a TextureAtlas for this purpose. A atlas is a file generated automatically from separate images by the LibGDX TexturePacker. It stores everything from image bounds within your sheet to NinePatch information. All you need to do is put your separate images in a folder and run the TexturePacker on that folder. This will create a sheet and a .atlas/.pack file for you that can easily loaded.
There exists a TexturePackerGui if you have difficulty with the commandline but I do recommend the command line or even using it within your app.
What I usually do is create these sheets on the fly when I'm developing. I can easily overwrite separate images and they have a immediate effect after I run my app again. I start with creating a new folder images at the root of my project. Then for each pack I need I create another folder. For this example I create the folder tileset in images. In the DesktopLauncher I will setup this folder to produce a atlas from the images.
TexturePacker.Settings settings = new TexturePacker.Settings();
settings.maxWidth = 1024;
settings.maxHeight = 1024;
The settings file specifies everything about your atlas. The maximum size of a single sheet, if it should strip transparency from images, padding, rotation, etc. They are all very straightforward and you can look these all up in the documentation. Using these settings you can create your atlas.
TexturePacker.process(settings,
"../../images/tileset", //where to find the images to be packed.
"../../android/assets/tileset/", //where to store the atlas and sheet
"tileset"); //what filename to use
Now you can open your .atlas file and you see it uses the filenames as a alias. This alias is used to look them up but let's load the atlas first.
TextureAtlas atlas = new TextureAtlas("tileset/tileset.atlas");
By passing just a string to the constructor it looks by default in your local path which in turn is by default in android/assets/. Now we can ask the atlas to hand over our assets in the sheet.
atlas.findRegion("alias"); //hands over the image on the sheet named "alias"
Looking up textures like this is somewhat expensive. You don't want to look up many textures like this each update so you still need to store them somewhere.
If you name your image sequence like image_01.png, image_02.png, image_03.png it stores them all under the same name and within the atlas it sorts them bu it's index. So if you want a separate array of certain textures you can name them with _xx and get them all in one go:
atlas.findRegions("alias");
This is especially handy for Animation. Just copy your image sequence to a folder and specify it to be packed. Name your sequence correctly and give the regions to the animation. Everything will work right off the bat.
Loading a TextureAtlas with the AssetManager is pretty much the same as a normal Texture except you would specify it's from a TextureAtlas.class. You always load the .atlas which in turn handles your image.
I always use AssetDescriptor to load my assets. If I where you I would get rid of the static Assets.textures[] since that will get you into trouble sooner or later.
//None static AssetManager with getter
private AssetManager manager = new AssetManager();
public AssetManager getManager() { return manager; }
//Specify a descriptor for the atlas, this is static so I can acces it anywhere.
//It's just a descriptor of the asset so this is safe.
public static final AssetDescriptor<TextureAtlas> TileSet = new AssetDescriptor<TextureAtlas>("tileset/tileset.atlas", TextureAtlas.class);
//then just load everything
public void load()
{
manager.load(tileSet);
//... load other stuff
}
Now just pass the AssetManager object anywhere you need access to your assets and you can load any asset just like so:
TextureAtlas tileset = assetManager.get(Assets.TileSet);
I think your for loop should look like this
for(int x = 0; x < 64; x ++){
textures[x] = new Sprite(
spritesheet,
(x%8)*64, //where x=3, (3%8)*64 = 3*64 = 192px sourceX
(x/8)*64, //where x=3, (int)(3/8)*64 = 0*64 = 0px sourceY
64, //source width
64 //source height
);
Another test case where x=20;
(20%8)*64 = 4*64 = 256px SourceX
(20/8)*64 = 2*64 = 128px SourceY

How to get image coordinate and corresponding location coordinate of a geo-referenced image in android

In my android app, I have a geo-referenced image which I am going to use as map. So I need to divide it into tiles. For that I want its top left and bottom right image coordinate(x,y) with location coordinate of corresponding image coordinates using java codes which is supported in android. Can anyone help me with the solution.
Update:
I got image coordinates of top left and bottom right corners but still I am not getting location coordinate (lat/long) of top left and bottom right corner. lat/long are already embedded in the image as it is already geo-referenced can anyone tell me how to fetch lat/long of the corners from image.
EDIT:
For calculating position of image add this to your activity oncreate and for location coordinates checkout below the exif interface code:
imageView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
pubic void onGlobalLayout() {
int height = imageView.getHeight();
int width = imageView.getWidth();
int x = imageView.getLeft();
int y = imageView.getTop();
//don't forget to remove the listener to prevent being called again by future layout events:
imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
You can embed EXIF data / set it when taking the picture before sending it to a remote server for processing. When posting / sending the data to a remote system, you could send the location data seperate, or leave it as attributes as part of the EXIF data.
Write/Geotag JPEGs (EXIF data) in Android
From reading the above, my assumption would be that the following would work:
ExifInterface exif;
exif = new ExifInterface("/sdcard/DCIM/"+filename+".jpeg");
double exifLatitude = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
double exifLongitude = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);

import json using box2d , I do not see the imported object

I have a problem with the rendering of a json object created with Box2D Editor .
My problem is that even if the object septum (correctly according to the tutorial found online ) , it is not seen on the stage.
private void createBottle() {
// 0. Create a loader for the file saved from the editor.
BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal("ball.json"));
// 1. Create a BodyDef, as usual.
BodyDef bd = new BodyDef();
bd.position.set(spaceShip.getX(), spaceShip.getY());
bd.type = BodyDef.BodyType.DynamicBody;
// 2. Create a FixtureDef, as usual.
FixtureDef fd = new FixtureDef();
fd.density = 1;
fd.friction = 0.5f;
fd.restitution = 0.3f;
// 3. Create a Body, as usual.
bottleModel = world.createBody(bd);
// 4. Create the body fixture automatically by using the loader.
loader.attachFixture(bottleModel, "test01", fd, 8);
}
In the create method i have this:
createBottle();
in the Render method:
Vector2 bottlePos = bottleModel.getPosition().sub(bottleModelOrigin);
bottleSprite.setPosition(bottlePos.x, bottlePos.y);
bottleSprite.setOrigin(bottleModelOrigin.x, bottleModelOrigin.y);
bottleSprite.setRotation(bottleModel.getAngle() * MathUtils.radiansToDegrees);
bottleSprite.draw(batch);
I do not understand why it is not added , I can not see on the screen .... I have other items assigned with the z position ... but nothing does not want to appear .
My goal is to enter the mazes made ​​with box2d editor and will move into a commanding object with a virtual joystick , this is the game I'm doing ...
if someone wants to enlighten me on how to import successfully , it is the first time you use an external editor with import json .
EDIT
I tried to follow the tutorial also before posting my question here on .
However the error is on Vector2 .... also the tutorial is clear but not working ... even online I saw other examples that they said that did not work ... there is not a resource running around? I have not found yet
You might try as it is stated here here:
LibGDX draws images with a reference to their bottom left corner. Therefore, if you left the reference point at the bottom left corner of your body, you can directly place the image at the location of the reference point (returned by Box2d engine with the getPosition() method).Else, if you changed the reference point location, you need to take it into account to draw the image.
Therefore, you need to store the reference point somewhere for later use:
bottleModelOrigin = loader.getOrigin("test01", BOTTLE_WIDTH).cpy();
Finally, in your render method, you can draw the image at the reference point location, offset by its local coordinates relatively to the image bottom left corner:
public void render() {
Vector2 bottlePos = bottleModel.getPosition().sub(bottleModelOrigin);
bottleSprite.setPosition(bottlePos.x, bottlePos.y);
bottleSprite.setOrigin(bottleModelOrigin.x, bottleModelOrigin.y);
bottleSprite.setRotation(bottleModel.getAngle() * MathUtils.radiansToDegrees);
}
The problem in your code seems to be that you don't have a variable named bottleModelOrigin atleast it is not visible in your first example. Although in that case a NullPointerException should have been thrown.

Categories

Resources