Java - Find a sequence of pixels - java

Was just wondering how you would be able to check each pixel, from top of your screen to bottom (or just a 500x500 rectangle) for a pattern of pixels in it. Example look through all the pixels, and see if there is 20 red pixels in a row (Search the screen for a red box).
Sorry for the terrible description, let me know and ill try to make it more specific.

What you are trying to do is called Template Matching in the Image Processing community, you should first check this.
For the implementation part, you can access the pixels of BufferedImage (using getRGB/getRGBs methods), or even better you can use JAI or JavaCV if you want to do anything serious.

Related

How do I make 2d- Flashlight effect in Processing 3

For reference the effect I'm going for is this:
I'm working in Processing 3, NOT p5.js.
I've looked around processing forums, but i can't find anything that works in the current version or doesn't use PGraphics and a mask which from what I've read can be expensive to use.
My current ideas and implementations have resulted to drawing shapes around the player and filling the gaps in with a circles with no fill that has a large stroke weight.
Does anyone know of any methods to easily and inexpensively draw a black background over everything except a small circular area?
If this is the wrong place to ask this question just send me on my way I guess, but please be nice. Thank you:)
You could create an image (or PGraphics) that consists of mostly black, with a transparent circle in it. This is called image masking or alpha compositing. Doing a Google image search for "alpha composite" returns a bunch of the images I'm talking about.
Anyway, after you have the image, it's just a matter of drawing it on top of your scene wherever the player is. You might also use the PImage#mask() function. More info can be found in the reference.

opencv java - detect all the rectangles and fill them with an image

I am currently working on a solution for an automatic image edit.
And I have used Canny Edge Detection and Closing.
But What I ultimately want to accomplish is to find all the rectangles from blueprint and fill them with an image what I have.
I want to know the process that I need to take, not the exact code or solution!
Please suggest me what steps should I take to accomplish it, thx.
Image that has the rectangles
Image that needs to go into all the rectangles
what i have done so far
2018-02-12 EDITTED(clean rectangle detected)
// I have done finding the rectangles and drawing lines over them, but the result is not really reliable than I expected(it draws line on rectangles those are not a parking space), and I do not know how to put an image on those rectangle instead of drawing line on them. please help me out!
P.S : Only in JAVA please !
In your case, you don't need Canny. Your image is really clean and edges are really visible already. A simple Threshold, will work better.
For rectangle detection take a look at this example (included with opencv) that uses findcontours and checks the angles: https://github.com/opencv/opencv/blob/master/samples/cpp/squares.cpp
In your case, you can skip the Canny step because you don't have gradients. You may need to modify the filtering, like side dimensions for your case.
Once the rectangles look good, you just need to copy your image in the location. If rectangles are rotated, you will have to rotate your image as well.
EDIT:
To copy the small image onto the big image you can do the following
Mat submatImg = bigImage.submat(new Rect(x, y, smallImage.width(), smallImage.height());
smallImage.copyTo(submatImg);
If you need to do resizing and rotations, take a look at geometric transformations

How would I detect the edges of an image (transparent background) in Java?

I have my image with a transparent background. I want to find the edges of the image and form a polygon from the outline. There are multiple methods/ways I could do this. I want to do it in Java (its for my game, which is using JBox2d Polygons for collision detection).
I have had some thought about this and am thinking how this would work. I could try detecting all transparent pixels, then invert the selection and only select the pixels that have 1 adjacent transparent pixel. This is all very complicated and I would like some guidence.
There are two aspects to your question.
Edge-detection. If your image has an alpha-channel, then you should be able to choose a suitable threshold and perform edge-detection to find the 'edges' of the transparent/opaque pixels. If it's just a GIF with a 'transparent' color, then it should a bit easier since you're effectively working with a black & white image.
Vectorization. This is where it gets (really) tricky. The field of raster to vector conversion is fertile ground. I would look at how solutions such as Potrace (GPL) are implemented then maybe attempt to build my own off that.
However, for a game, personally, I wouldn't even try real-time edge/collision detection this way. Since I'm working with sprites, I would use bounding boxes and other raster-based techniques.
If I really want polygon-based edge/collision detection, then I would probably opt to manually trace edges beforehand and just store them along with each raster image, then perform computations on those (trade space for time). I presume that the images you work with aren't dynamically generated at run-time making pre-computation possible.
This isn't really an answer to your question to make pixel-perfect collision, but I want to say that it is a bad idea to make the fixtures depending on the images.
Box2D doesn't support concave fixtures.
Box2D (the original version in C++, I don't know how this works in JBox2D) has a limit to 8 vertices per polygon.
Because of these two reason, you might think to create one square fixture per pixel, but that is going to be very expensive in processing time.
In my almost finished game, I'm defining my fixtures with a level editor.
Here is an excerpt of one of my levels (xml):
<body id="STONE" depth="13" angle="0.000000" type="static" x="7.939437" y="0.750494" tags="" >
<image id="stone_img" img="IMG_STONE" depth="0" x="-0.362081" y="0.526663" w="1.400000" h="1.600000" angle="0.000000" colorize="ffffffff"/>
<fixture id="" density="1.000000" friction="0.300000" restitution="0.300000" sensor="false" shape="rect" x="-0.188671" y="0.806253" w="1.000000" h="2.200000" angle="0.545597" tags="" />
<fixture id="" density="1.000000" friction="0.300000" restitution="0.300000" sensor="false" shape="rect" x="0.412080" y="-0.097607" w="1.000000" h="2.200000" angle="0.000000" tags="" />
</body>
I think this is the best way of working with Box2D.
Hopefully this inspires you :D

Scalable clipping mask

I need to to clip variablesized images into puzzle shaped pices like this(not squares): http://www.fernando.com.ar/jquery-puzzle/
I have considered the posibility of doing this with a php library like Cairo or GD, but have little to no experience with these librays, and see no immidiate soulution for creating a clipping mask dynamicaly scalable for different sized images.
I'm looking for guidance/tips on which serverside programing language to use to accomplish this task, and preferably an approach to this problem.
You can create an image using GD with the size of the puzzle piece. and then copy the full image on that image with the right cropping to get the right part of the image.
Then you can just dynamically color in every part of the piece you want to remove with a distinct color (eg #0f0) and then use imagecolorallocatealpha to make that color transparent. Do it for each piece and you have your server side image pieces.
However, if I where you I would create the clipping mask of each puzzle peace in advance in the distinct color. That would make two images per connection (one with the "circle" connecter sticking out and one where this circle connector fits into). That way you can just copy these masks onto the image to create nice edges quickly.
GD is quite complicated, I've heard very good things about Image Magick for which there is a PHP version and lots of documentation on php.net. However, not all web servers would have this installed by default.
http://www.php.net/manual/en/book.imagick.php
If you choose to do it using PHP with GD then the code here may help:
http://php.amnuts.com/index.php?do=view&id=15&file=class.imagemask.php
Essentially what you need to do with GD is to start with a mask at a particular size and then use the imagecopyresampled function to copy the mask image resource to a larger or smaller size. To see what I mean, check out the _getMaskImage method class shown at the url above. A working example of the output can be seen at:
http://php.amnuts.com/demos/image-mask/
The problem with doing it via GD, as far as I can tell, is that you need to do it a pixel at a time if you want to achieve varying opacity levels, so processing a large image could take a few seconds. With ImageMagick this may not be the case.

Trying to convert a 2D image into 3D objects in Java

Hey, I'm trying to take a simple image, something like a black background with colored blocks representing walls. I'm trying to figure out how to go about starting on something like this. Do I need to parse the image and look at each pixel or is there an easier way to do it?
I'm using Java3D but it doesn't seem to have any sort of built in support for that...
This might be more compex than you think.
A solution would basically include the following steps:
Edge detection using Java 2D ConvolveOp Filter
Vectorizing the edges into a 2D model.
Extrusion to 3D
Turns out what I really wanted was a height mapper. I mapped each pixel to a certain height based on its gray-scale RGB value. If I wanted color to be independent of height, I would have two images, one with the gray-scale height map and another with a colored image of what I want the, in this case, room to look like.
As for recognizing colors from an image as a specific object other than by pixel, definitely requires something more complex. A friend was suggesting something like painter's algorithm for something like that. However, at least for me, that was being the scope of my application.

Categories

Resources