Filling a 2D AMP array row by row - gpgpu

Say I have a source of CPU side data that looks like this:
vector< vector<int> > cpuBlobOData;
Assume every row vector inside has the same size (a jagged 2d vector that isn't jagged).
I want to copy this to single block of GPU memory, inside an array. Is there a way to do this row by row? Something like this:
array<int, 2> gpuArray(cpuBlobOData.size(), numColumns);
for(size_t i=0; i<cpuBlobOData.size(); ++i)
{
auto cpuRow = cpuBlobOData[i];
concurrency::copy(cpuRow.begin(), cpuRow.end(), &gpuArray[i]);
}
I know this doesn't compile, it shows what I am trying to do. Is there something that accomplishes this? The only solution I could find was to copy the cpu vector into a 1D vector, and map that to an array_view, or copy it into an array.
That works, but it is wasteful, and for some algorithms, the continuous 1D vector may not fit into the remaining memory space.
Any advice is welcome, as I am struggling to figure out AMP.

Yes, you can copy it row by row. Your solution is almost there, just remove '&' from the gpuArray in the line with concurrency::copy:
array<int, 2> gpuArray(cpuBlobOData.size(), numColumns);
for(size_t i=0; i<cpuBlobOData.size(); ++i)
{
auto cpuRow = cpuBlobOData[i];
concurrency::copy(cpuRow.begin(), cpuRow.end(), gpuArray[i]);
}
What happens is that array<int,2> accessed with subscript operator returns an array_view<int,1> that represents a row 'i', this is called "projection". For more details please check out a blog post that covers C++ AMP projections in details.

Related

Processing: Using rollover with text from table

I have a CSV file that was created from a plain text file. In column A there is a list of unique words and in column B it lists their frequency within that text.
I am using Processing and loadTable to draw a list of the words. I would like to use rollover so that when the mouse hovers over them, an ellipse appears that has a size relative to the integer associated with that word's frequency.
I am having a hard time finding a good example of the syntax for using rollover() while in a loop that includes data from a CSV file.
Any help is appreciated!
void setup() {
table = loadTable("tabletest.csv", "header");
size(600,1000);
}
void draw() {
background(252, 245, 224);
for (int i = 0; i < table.getRowCount(); i++) {
TableRow row = table.getRow(i);
String w = row.getString("Word");
int f = row.getInt("Frequency");
textSize(10);
text(w, width/2, 15*i);
fill(8, 114, 105);
textAlign(CENTER);
}
}
There is no built-in rollover() function that I know of. You're going to have to write this functionality yourself.
You can check whether the cursor is inside a given rectangle. See this guide for more info, but basically you check whether mouseX is between the left and right and whether mouseY is between the top and bottom of the rectangle.
If so, then you know the mouse is inside that table cell and you can take the appropriate action. I recommend breaking your problem down into smaller steps and taking those steps on one at a time. For example, start over with a basic sketch that shows a single rectangle that changes color when the mouse is inside it. Get that working perfectly before trying to make it work with multiple rectangles.
Then if you get stuck, you can post a MCVE along with a more specific technical question. Good luck.

Matlab: Performing operations with 'struct' in image processing

I have two images - one binarized, and one original.
I use the binarized image to segment using bwconncomp and then for every blob/region, I want to sum the pixel-intensities from the original image.
I do that by:
blobMeasurements = regionprops(binarizedImage, originalImage, 'pixelvalues');
Now, I have a struct with a 'p x 1' vector for each blob/region. I need to sum these pixel intensities, such that I have one 'sum' value for each blob/region. How do I perform this operation? Is there a better way of doing this?
Thanks.
Try this:
blobIntensities = arrayfun(#(x) sum(x.pixelvalues(:)), blobMeasurements);
arrayfun runs the given function #(x) sum(x.pixelvalues(:)) on each of the pelement of the structure array blobMeasurements. Hope this helps.

VTK - How to read Tensors/Matrix per cell from a NIFTI Image?

I'm trying to implement a MRT-DTI real-time fibertracking visualization tool based on VTK.
Therefore we need to read the DTI tensors/matrices per cell stored in a NIFTI Image (.nii) and I really can't figure out how to do this.
It's not a problem to retrieve a single scalar value from the NIFTI file, but I don't know how to get the tensor (3x3/4x4 matrix).
We would really appreciate any help !
Since the NIFTIImageReader is supposed to read a tensor NIFTI image as a multi-component vtkImage we tried this:
vtkSmartPointer<vtkImageExtractComponents> extractTupel1 = vtkSmartPointer<vtkImageExtractComponents>::New();
extractTupel1->SetInputConnection(reader->GetOutputPort());
extractTupel1->SetComponents(0,1,2);
extractTupel1->Update();
vtkSmartPointer<vtkImageExtractComponents> extractTupel2 = vtkSmartPointer<vtkImageExtractComponents>::New();
extractTupel2->SetInputConnection(reader->GetOutputPort());
extractTupel2->SetComponents(3, 4, 5);
extractTupel2->Update();
vtkSmartPointer<vtkImageExtractComponents> extractTupel3 = vtkSmartPointer<vtkImageExtractComponents>::New();
extractTupel3->SetInputConnection(reader->GetOutputPort());
extractTupel3->SetComponents(6, 7, 8);
extractTupel3->Update();
extractTupel1->GetOutput()->GetPoint(pointId, tupel1);
extractTupel2->GetOutput()->GetPoint(pointId, tupel2);
extractTupel3->GetOutput()->GetPoint(pointId, tupel3);
But it doesn't work. Maybe the GetPoint-Method is the wrong choice?
Please help :)
Answer by David Gobbi, really much thanks to him!:
No, the GetPoint() method will not return the tensor value. It will return the coordinates of the voxel. So vtkImageExtractComponents is not necessary here, either.
A vtkImageData always stores the voxel values as its "Scalars" array, even if the voxel values are not scalar quantities.
A simple (but inefficient way) to get the scalar values is this method:
GetScalarComponentAsDouble (int x, int y, int z, int component)
For each voxel, you would call this method 9 times with component = [0..8].
A much more efficient way of getting the tensors is to get the scalar array from the data, and then look up the tensors via the pointId:
reader->Update();
vtkDataArray *tensors = reader->GetOutput()->GetPointData()->GetScalars();
double tensor[9];
tensors->GetTuple(pointId, tensor);
This is orders of magnitude more efficient than GetScalarComponentAsDouble().

Random World Generation

I'm starting to go into random world generating, I have an idea on how random number generating works (Actually pseudorandom numbers), but I don't have a good idea of how to make the world look "nice", in other words not just place blocks based on a random x, y that it gives me, but make it look smooth.
This will be a 1 time generation per world. So everything is created at start.
I was thinking of an algorithm a few moments ago, but the problem is that it would just use be an endless amount of nested if loops, which would probably take a more than the necessary time. I was thinking of the following:
Choose a random location on the map and place the spawn point in that location.
Start building the street based on the spawn location, like if the spawn location is 16
spaces near the edge of the world build a house, otherwise start building a street.
Based on the previously generated street's place structures around.
Place misc.
Conceptualizing the algorithm isn't much of a problem, what I'm having difficulty with is starting the actual code from step 2 and below. Based on the above algorithm or an algorithm you think of, how would you start the code? I'm not asking for the actual code to be made, just an idea of how it would look.
I know this question isn't precise and can have multiple answers, but I've seen many questions similar to this one having a strange approach.
hmm looks like planar(or cubic) map filling. from my point of view firstly you need some databases
struct _object
{
string name,help,info; // texts for later use
int siz[3]; // grid size of object
int pos[3]; // placement pos (center or what ever)
// other stuff like: mesh-id,color,hit points,...
};
struct _dependency
{
int objid
List<int> near; // can be near this objects (can add probability)
List<int> far; // cannot be near this objects (can add probability,min distance)
};
List<_object> object; // DBS of all object types
List<_dependency> depend; // DBS of object dependency
Then you need to initialize this DBS from ini files or whatever. After that you need to create world map. For simplicity let it by only a single squared town and single layer(no sub-terrain), size and placement can be random.
List<_object> obj; // DBS of placed objects, could be lesser derivate of previous _object to conserve memory requirements
const int N=100;
int[N][N] map; // placement map, containing placed obj id, or -1 for empty space
so now you need some town generating function that fills map[N][N]:
void genere()
{
int i,j,x,y,xx,yy,objid,objix;
int _min_complexity=N/10; // this can also be random
int _max_complexity=N; // this can also be random
// clear map
for (i=0;i<N;i++)
for (j=0;j<N;j++)
map[i][j]=-1;
int complexity=_min_complexity+random(_max_complexity-_min_complexity);
for (i=0;i<complexity;)
{
// random placenet position
x=random(N);
y=random(N);
// random object, should take in mind object[].near and closest objects in map[y][x]
objid=random(object.num);
if (check if map[][] is empty enough to place object[objid] to x,y,z)
if (check if near x,y position is not bad type of object already object[].far)
{
// add new object to list
objix=obj.add(object[objid]);
// add new object to map
int *siz=obj[objix].siz
int *pos=obj[objix].pos
x+=pos[0];
y+=pos[1];
for (yy=0;yy<siz[1];yy++)
for (xx=0;xx<siz[0];xx++)
map[y+yy][x+xx]=objid;
i++;
}
}
}
also the position can be double[3] + orientation matrix and map coordinates will than be aligned to grid. There are many ways to tweak this code, its just an starting template. Hope it helps a little.
P.S.
List<type> l;
l.num - number of items in list
l[i] - i item from list
i=l.add(a) - add new item a to list and returns its index

Converting a image to a quadtree

I need to convert a image (square) into a quadtree, in the 'usual' way - slice it in four, check if there's only one color into each piece; if yes: close the node, else: repeat;
Anyone know an opensource program to it?
Preferably in Java, but I can use any language.
Thanks.
I imagine you could easily write a program that does it for you in OpenCV. Provided you already have some data structure to hold the actual tree, the main function would look something like this (I've written it for gray images, but it's only the test that needs repeating three times for color):
void divideAndConquer(Mat im, QuadTree &tree, int parent){
if(parent<0)
return;
double min,max;
minMaxLoc(im,&min,&max);
if(max-min<0.01)
tree.addNode(parent,closed);
else{
tree.addNode(parent,open);
Mat im0=Mat(im,Range(0,image.rows/2-1),Range(0,image.cols/2-1));
Mat im1=Mat(im,Range(image.rows/2,image.rows),Range(0,image.cols/2-1));
Mat im2=Mat(im,Range(0,image.rows/2-1),Range(image.cols/2,image.cols));
Mat im3=Mat(im,Range(image.rows/2,image.rows),Range(image.cols/2-1,image.cols));
divideAndConquer(im0, tree, parent/4);
divideAndConquer(im1, tree, parent/4+1);
divideAndConquer(im2, tree, parent/4+2);
divideAndConquer(im3, tree, parent/4+3);
}
}

Resources