I am using OpenCV 2.2 on Windows 7.
I am making a mask where the rows are all 1 up to row 400 and 0 for rows beyond that. I initialize the mask with cv::Mat::ones() and was wondering what would be the most efficient way to zero the rows beyond 400. I could use for loops but was wondering if there was a more efficient, tidier way to do it.
Thanks,
Peter.
There is more than one way to do it:
First, sub-matrices
Mat bigImg(width, height, CV_8UC3);
bigImg(Rect(0,0,width, height/2)) = Scalar::all(1); // upper half ones
bigImg(Rect(0,height/2,width, height/2)) = Scalar::all(0); // lower half zeros
Or you can use the RowRange and ColRange for the same effect
bigImg(rowRange, colRange) = Scalar::all(n);
Just check the docs on how to use ranges
The only way I know of is to create a matrix of 400xm with cv::Mat::ones() and a matrix of 400x(n-m) with cv::Mat::zeros() and then join the two together. However this has the overhead of making the two matrices and then resizing one to be big enough to contain the other.
I think looping is definitely more efficient. It's C/C++ anyway I assume, and that's about the fastest way for this particular sort of operation.
Related
I want to encrypt some parts of image and embed them into least significant bits of another image.I have pictures in the form of small picturebox in windows forms c#.Can anyone help me with encrypting these blocks?
I doubt it is the correct or fastest way, but likely the easiest way is to just use modulus operator. So for instance if you want to squeeze 2 images into one which has greyscale data in byte format (0-255). For simplicity lets assume you want an even split of 4 bits per image. 2^4=16. So if you take every pixel in that image and mod it:
pic1Pixel = pic1Pixel -pic1Pixel %16
that is going to peel the bottom significance out of that image. Then in the other image do this:
pic2Pixel = floor(pic2Pixel /16)
Do whatever you need to do (casting and floor or whatnot) to ensure the operation happens and then is rounded correctly (language dependent).
Then simply add your two bitmaps pixel by pixel.
compoundPixel = pic1Pixel + pic2Pixel
If after that you want to pull out the first image:
pic1Pixel = 16*(floor(compoundPixel/16))
second image:
pic2Pixel = 16* (compoundPixel%16)
There is almost certainly a cleaner way to do it with simple bit shifting, but I don't feel like debugging/testing anything right now and don't know the sintax off hand. In short though you would just shift in the first 4 bits from first pic then the first 4 bits from the second pic. To recall you would shift out appropriately or mask and normalize.
Has anyone been able to do spatial operations with #ApacheSpark? e.g. intersection of two sets that contain line segments?
I would like to intersect two sets of lines.
Here is a 1-dimensional example:
The two sets are:
A = {(1,4), (5,9), (10,17),(18,20)}
B = {(2,5), (6,9), (10,15),(16,20)}
The result intersection would be:
intersection(A,B) = {(1,1), (2,4), (5,5), (6,9), (10,15), (16,17), (18,20)}
A few more details:
- sets have ~3 million items
- the lines in a set cover the entire range
Thanks.
One approach to parallelize this would be to create a grid of some size, and group line segments by the grids they belong to.
So for a grid with sizes n, you could flatMap pairs of coordinates (segments of line segments), to create (gridId, ( (x,y), (x,y) )) key-value pairs.
The segment (1,3), (5,9) would be mapped to ( (1,1), ((1,3),(5,9) ) for a grid size 10 - that line segment only exists in grid "slot" 1,1 (the grid from 0-10,0-10). If you chose a smaller grid size, the line segment would be flatmapped to multiple key-value pairs, one for each grid-slot it belongs to.
Having done that, you can groupByKey, and for each group, calculation intersections as normal.
It wouldn't exactly be the most efficient way of doing things, especially if you've got long line segments spanning multiple grid "slots", but it's a simple way of splitting the problem into subproblems that'll fit in memory.
You could solve this with a full cartesian join of the two RDDs, but this would become incredibly slow at large scale. If your problem is smallish, sure, this is an easy and cheap approach. Just emit the overlap, if any, between every pair in the join.
To do better, I imagine that you can solve this by sorting the sets by start point, and then walking through both at the same time, matching one's current interval versus another and emitting overlaps. Details left to the reader.
You can almost solve this by first mapping each tuple (x,y) in A to something like ((x,y),'A') or something, and the same for B, and then taking the union and sortBy the x values. Then you can mapPartitions to encounter a stream of labeled segments and implement your algorithm.
This doesn't quite work though since you would miss overlaps between values at the ends of partitions. I can't think of a good simple way to take care of that off the top of my head.
I've ran in to an issue concerning generating floating point coordinates from an image.
The original problem is as follows:
the input image is handwritten text. From this I want to generate a set of points (just x,y coordinates) that make up the individual characters.
At first I used findContours in order to generate the points. Since this finds the edges of the characters it first needs to be ran through a thinning algorithm, since I'm not interested in the shape of the characters, only the lines or as in this case, points.
Input:
thinning:
So, I run my input through the thinning algorithm and all is fine, output looks good. Running findContours on this however does not work out so good, it skips a lot of stuff and I end up with something unusable.
The second idea was to generate bounding boxes (with findContours), use these bounding boxes to grab the characters from the thinning process and grab all none-white pixel indices as "points" and offset them by the bounding box position. This generates even worse output, and seems like a bad method.
Horrible code for this:
Mat temp = new Mat(edges, bb);
byte roi_buff[] = new byte[(int) (temp.total() * temp.channels())];
temp.get(0, 0, roi_buff);
int COLS = temp.cols();
List<Point> preArrayList = new ArrayList<Point>();
for(int i = 0; i < roi_buff.length; i++)
{
if(roi_buff[i] != 0)
{
Point tempP = bb.tl();
tempP.x += i%COLS;
tempP.y += i/COLS;
preArrayList.add(tempP);
}
}
Is there any alternatives or am I overlooking something?
UPDATE:
I overlooked the fact that I need the points (pixels) to be ordered. In the method above I simply do scanline approach to grabbing all the pixels. If you look at the 'o' for example, it would grab first the point on the left hand side, then the one on the right hand side. I would need them to be ordered by their neighbouring pixels since I want to draw paths with the points later on (outside of opencv).
Is this possible?
You should look into implementing your own connected components labelling. The concept is very simple: you scan the first line and assign unique labels to each horizontally connected strip of pixels. You basically check for every pixel if it is connected to its left neighbour and assign it either that neighbour's label or a new label. In the second row you do the same, but you also check against the pixels above it. Sometimes you need a label merge: two strips that were not connected in the previous row are joined in the current row. The way to deal with this is either to keep a list of label equivalences or use pointers to labels (so you can easily do a complete label change for an object).
This is basically what findContours does, but if you implement it yourself you have the freedom to go for 8-connectedness and even bridge a single-pixel or two-pixel gap. That way you get "almost-connected components labelling". It looks like you need this for the "w" in your example picture.
Once you have the image labelled this way, you can push all the pixels of a single label to a vector, and order them something like this. Find the top left pixel, push it to a new vector and erase it from the original vector. Now find the pixel in the original vector closest to it, push it to the new vector and erase from the original. Continue until all pixels have been transferred.
It will not be very fast this way, but it should be a start.
How to make a 2d world with fixed size, which would repeat itself when reached any side of the map?
When you reach a side of a map you see the opposite side of the map which merged togeather with this one. The idea is that if you didn't have a minimap you would not even notice the transition of map repeating itself.
I have a few ideas how to make it:
1) Keeping total of 3x3 world like these all the time which are exactly the same and updated the same way, just the players exists in only one of them.
2) Another way would be to seperate the map into smaller peaces and add them to required place when asked.
Either way it can be complicated to complete it. I remember that more thatn 10 years ago i played some game like that with soldiers following each other in a repeating wold shooting other AI soldiers.
Mostly waned to hear your thoughts about the idea and how it could be achieved. I'm coding in XNA(C#).
Another alternative is to generate noise using libnoise libraries. The beauty of this is that you can generate noise over a theoretical infinite amount of space.
Take a look at the following:
http://libnoise.sourceforge.net/tutorials/tutorial3.html#tile
There is also an XNA port of the above at: http://bigblackblock.com/tools/libnoisexna
If you end up using the XNA port, you can do something like this:
Perlin perlin = new Perlin();
perlin.Frequency = 0.5f; //height
perlin.Lacunarity = 2f; //frequency increase between octaves
perlin.OctaveCount = 5; //Number of passes
perlin.Persistence = 0.45f; //
perlin.Quality = QualityMode.High;
perlin.Seed = 8;
//Create our 2d map
Noise2D _map = new Noise2D(CHUNKSIZE_WIDTH, CHUNKSIZE_HEIGHT, perlin);
//Get a section
_map.GeneratePlanar(left, right, top, down);
GeneratePlanar is the function to call to get the sections in each direction that will connect seamlessly with the rest of your world.
If the game is tile based I think what you should do is:
Keep only one array for the game area.
Determine the visible area using modulo arithmetics over the size of the game area mod w and h where these are the width and height of the table.
E.g. if the table is 80x100 (0,0) top left coordinates with a width of 80 and height of 100 and the rect of the viewport is at (70,90) with a width of 40 and height of 20 you index with [70-79][0-29] for the x coordinate and [90-99][0-9] for the y. This can be achieved by calculating the index with the following formula:
idx = (n+i)%80 (or%100) where n is the top coordinate(x or y) for the rect and i is in the range for the width/height of the viewport.
This assumes that one step of movement moves the camera with non fractional coordinates.
So this is your second alternative in a little bit more detailed way. If you only want to repeat the terrain, you should separate the contents of the tile. In this case the contents will most likely be generated on the fly since you don't store them.
Hope this helped.
i am trying to check the degree of overlap between 2 CGPaths.
the easiest way i have come up with is get the percentage of the overlap between the bounding CGRects. I know this will fail when different paths occupy similar bounds. but oh well, if you know of a better way ... please help.
anyway, the current question regards calculating the percentage overlap between the rects.
i see the CGRectIntersection function to obtain the rectangle of intersection. I can calculate the area of this rect, but there doesn't seem to be an easy way to get the area of the non-intersected regions. any ideas? would subtracting that area from the area of the rectUnion make sense? if i understand rectUnion correctly, if the union and the intersection are the same size, they completely overlap?
Not quite understanding, I think. Isn't the "non-intersecting region" of a CGRect A with another one B just A's area minus the intersecting region? Or more to the point, isn't the percentage overlap just equal to the intersecting area divided by the combined total area:
Area(A ^ B)/(Area(A) + Area(B) - Area(A^B))
(BTW, I don't think you want to deal with RectUnion as that potentially has a huge amount of space in neither A or B. )
Oh, and on your original question, that's beyond my graphics ability, but the basic technique seems to be to draw both Paths in a graphic context (maybe with an XOR) and see which pixels are still left on. There seems to be some code pointing the way here: Clipping CGPath to a CGRect