Delete rows from 3d array with zeros - multidimensional-array

I am working with images using OpenCV. The end image gives output with black and red pixels. For further processing I only want non-black pixels to be processed. If I check the data in NumPy it is seen as 3d array. How can I keep only non-zero rows; Below is the code;
numpy.ndarray
Thanks in advance

You can write something like
array = np.array([[0, 10, 20], [20, 30, 40]])
array = array[array > 0]
This code drops your zeros.

Related

Is there an efficient way to count dots in cells?

I have graphs of sets of points like:-
There are up to 1 million points on each graph. You can see that the points are scattered over a grid of cells, each sized 200 x 100 units. So there are 35 cells shown.
Is there an efficient way to count how many points there are in each cell? The brute force approach seems to be to parse the data 35 times with a whole load of combined is less or greater than statements.
Some of the steps below could be optimized in the sense that you could perform some of these as you build up the data set. However I'll assume you are just given a series of points and you have to find which cells they fit into. If you can inject your own code into the step that builds up the graph, you could do the stuff I wrote below along side of building the graph instead of after the fact.
You're stuck with brute force in the case of just being given the data, there's no way you can know otherwise since you have to visit each point at least once to figure out what cell it is in. Therefore we are stuck with O(n). If you have some other knowledge you could exploit, that would be up to you to utilize - but since it wasn't mentioned in the OP I will assume we're stuck with brute force.
The high level strategy would be as follows:
// 1) Set rectangle bounds to have minX/Y at +inf, and maxX/Y to be -inf
// or initialize it with the first point
// 2) For each point:
// Set the set the min with min(point.x, bounds.min.x)
// Same for the max as well
// 3) Now you have your bounds, you divide it by how many cells fit onto each
// axis while taking into account that you might need to round up with division
// truncating the results, unless you cast to float and ceil()
int cols = ceil(float(bounds.max.x - bounds.min.x) / CELL_WIDTH);
int rows = ceil(float(bounds.max.y - bounds.min.y) / CELL_HEIGHT);
// 4) You have the # of cells for the width and height, so make a 2D array of
// some sort that is w * h cells (each cell contains 32-bit int at least) and
// initialize to zero if this is C or C++
// 5) Figure out the cell number by subtracting the bottom left corner of our
// bounds (which should be the min point on the x/y axis that we found from (1))
for (Point p in points):
int col = (p.x - minX) / cellWidth;
int row = (p.y - minY) / cellHeight;
data[row][col]++;
Optimizations:
There are some ways we might be able to speed this up off the top of my head:
If you have powers of two with the cell width/height, you could do some bit shifting. If it's a multiple of ten, this might possibly speed things up if you aren't using C or C++, but I haven't profiled this so maybe hotspot in Java and the like would do this for you anyways (and no idea about Python). Then again 1 million points should be pretty fast.
We don't need to go over the whole range at the beginning, we could just keep resizing our table and adding new rows and columns if we find a bigger value. This way we'd only do one iteration over all the points instead of two.
If you don't care about the extra space usage and your numbers are positive only, you could avoid the "translate to origin" subtraction step by just assuming everything is already relative to the origin and not subtract at all. You could get away with this by modifying step (1) of the code to have the min start at 0 instead of inf (or the first point if you chose that). This might be bad however if your points are really far out on the axis and you end up creating a ton of empty slots. You'd know your data and whether this is possible or not.
There's probably a few more things that can be done but this would get you on the right track to being efficient with it. You'd be able to work back to which cell it is as well.
EDIT: This assumes you won't have some really small cell width compared to the grid size (like your width being 100 units, but your graph could span by 2 million units). If so then you'd need to look into possibly sparse matrices.

How do I copy a 1d linear array into a 3d array without conflicts? (CUDA)

This isn't necessarily CUDA specific.
Basically, on the GPU I have a 1D array, but in the C code, I have a 3d array. I need to copy this array back and forth, and I need to be able to linear-ize the indices properly. That means that I have the pick the correct numbering scheme, as in is (1, 0, 0) linear-ized as 1, or is (0, 1, 0), or (0, 0, 1), in the form (x, y, z)?
The 3d arrays are declared as array[X][Y][Z]. When I copy that 3D array into an equal sized 1d array, how will the computer naturally number it? However the computer collapses down the 3d array, I need to do the exact same when I try to convert between the two. I hope I am making clear what I want to say, but if you have any questions, please ask. Thank you.
Arrays in C are kept in row-major order, see this for more details.
So the first element is array[0][0][0], the second is array[0][0][1], and etc.

I wants to read rows of gray scale Image into an array of integer for processing in Qt

I have one Gray scale image, which I wants to read row by row and process( to find the median)
I created one QImage by loading the file. Then I used scanline(x) to read the row. But I am getting some wrong values( hope values should be 0-256). Scanline() gets the RGBA values I think. How to use this method for gray scale. Or any other solution please
Thanks
What do you mean wrong values? If you get RGBA(45, 45, 45, 255), that's gray. If you want a 0-255 value, then just read the red component only (for example).

How do I rotate an image?

See also: Why is my image rotation algorithm not working?
This question isn't language specific, and is a math problem. I will however use some C++ code to explain what I need as I'm not experienced with the mathematic equations needed to express the problem (but if you know about this, I’d be interested to learn).
Here's how the image is composed:
ImageMatrix image;
image[0][0][0] = 1;
image[0][1][0] = 2;
image[0][2][0] = 1;
image[1][0][0] = 0;
image[1][1][0] = 0;
image[1][2][0] = 0;
image[2][0][0] = -1;
image[2][1][0] = -2;
image[2][2][0] = -1;
Here's the prototype for the function I'm trying to create:
ImageMatrix rotateImage(ImageMatrix image, double angle);
I'd like to rotate only the first two indices (rows and columns) but not the channel.
The usual way to solve this is by doing it backwards. Instead of calculating where each pixel in the input image ends up in the output image, you calculate where each pixel in the output image is located in the input image (by rotationg the same amount in the other direction. This way you can be sure that all pixels in the output image will have a value.
output = new Image(input.size())
for each pixel in input:
{
p2 = rotate(pixel, -angle);
value = interpolate(input, p2)
output(pixel) = value
}
There are different ways to do interpolation. For the formula of rotation I think you should check https://en.wikipedia.org/wiki/Rotation_matrix#In_two_dimensions
But just to be nice, here it is (rotation of point (x,y) angle degrees/radians):
newX = cos(angle)*x - sin(angle)*y
newY = sin(angle)*x + cos(angle)*y
To rotate an image, you create 3 points:
A----B
|
|
C
and rotate that around A. To get the new rotated image you do this:
rotate ABC around A in 2D, so this is a single euler rotation
traverse in the rotated state from A to B. For every pixel you traverse also from left to right over the horizontal line in the original image. So if the image is an image of width 100, height 50, you'll traverse from A to B in 100 steps and from A to C in 50 steps, drawing 50 lines of 100 pixels in the area formed by ABC in their rotated state.
This might sound complicated but it's not. Please see this C# code I wrote some time ago:
rotoZoomer by me
When drawing, I alter the source pointers a bit to get a rubber-like effect, but if you disable that, you'll see the code rotates the image without problems. Of course, on some angles you'll get an image which looks slightly distorted. The sourcecode contains comments what's going on so you should be able to grab the math/logic behind it easily.
If you like Java better, I also have made a java version once, 14 or so years ago ;) ->
http://www.xs4all.nl/~perseus/zoom/zoom.java
Note there's another solution apart from rotation matrices, that doesn't loose image information through aliasing.
You can separate 2D image rotation into skews and scalings, which preserve the image quality.
Here's a simpler explanation
It seems like the example you've provided is some edge detection kernel. So if what you want to is detect edges of different angles you'd better choose some continuous function (which in your case might be a parametrized gaussian of x1 multiplied by x2) and then rotate it according to formulae provided by kigurai. As a result you would be able to produce a diskrete kernel more efficiently and without aliasing.

plot a 3 axis graph as a mesh

I have seen 3d surface plots of data before but i do not know what software i could use to make it.
I have 3 series of data (X, Y, Z) basically i want each of the rows on the table to be a point in 3d space, all joined as a mesh. The data is currently csv, but i can change the format, as it is data i generated myself.
Can anyone help
If your x & y points topologically lie on a grid, then you can use MESH. They don't need to have even spacing; they just need to be organized so that x(r:r+1,c:c+1) and y(r:r+1,c:c+1) define a quadrilateral on your mesh, for each row r and column c.
If your data do not lie on a grid, but you know what the faces should be, look at the PATCH function.
If you only have points and you don't know anything about the surface, you need to first solve the surface reconstruction problem. I've used cocone; there are other good packages there too. Once you have the reconstructed surface, then you can use PATCH to display it.
Have you looked at using vtk? If you have Matlab then you should be able to use plot3d or surf with meshgrid and griddata to generate 3D surface plots or patch as suggested by Mr. Fooz.
gnuplot or scilab
Below is a script for SciLab that I wrote awhile back. It reads in three columns separated by tabs. You can easily change this to fit your needs, pretty self-explanatory. Here is a quick guide to reading/writing in scilab and the one I reference below is here:
function plot_from_file(datafile)
//
// Make a simple x-y-z plot based on values read from a datafile.
// We assume that the datafile has three columns of floating-point
// values seperated by tabs.
// set verbose = 1 to see lots of diagnostics
verbose = 1;
// open the datafile (quit if we can't)
fid = mopen(datafile, 'r');
if (fid == -1)
error('cannot open datafile');
end
// loop over all lines in the file, reading them one at a time
num_lines = 0;
while (true)
// try to read the line ...
[num_read, val(1), val(2), val(3)] = mfscanf(fid, "%f\t%f\t%f");
if (num_read <= 0)
break
end
if (verbose > 0)
fprintf(1, 'num_lines %3d num_read %4d \n', num_lines, num_read);
end
if (num_read ~= 3)
error('didn''t read three points');
end
// okay, that line contained valid data. Store in arrays
num_lines = num_lines + 1;
x_array(num_lines) = val(1);
y_array(num_lines) = val(2);
z_array(num_lines) = val(3);
end
// now, make the plot
plot3d2(x_array, y_array, z_array);
// close the datafile
mclose(fid);
endfunction

Resources