Rasterize pointcloud with PCL OctreePointCloudVoxelCentroid - pcl

I try to rasterize a X-0-Z pointcloud via PCL::OctreePointCloudVoxelCentroid in order to use some image processing algorithms on the pointcloud afterwards. But the output is not what I expect. I want the centroid for each box in an axis aligned octree, if at least one point is in a box. The octree's bounding box should be well defined.
For testing I use 4 points: (-1.8000000 1 -1.8000000) (-1.4000000 1 -1.4000000) (-0.5000000 1 0.4000000) (0.4000000 1 1.8000000)
The code I use:
pcl::octree::OctreePointCloudVoxelCentroid<pcl::PointXYZ> octree (1.0f);
octree.defineBoundingBox(-2.0,-1.5,-2.0,2.0,2.5,2.0);
octree.setInputCloud (fourpoints);
octree.addPointsFromInputCloud ();
Black: original points; red: algorithm output; green: desired points.
Algorithm Output: coordinates(-1.6 1 -1.6)(-0.5 1 0.4)(0.4 1 1.8)

OctreePointCloudOccupancy instead of OctreePointCloudVoxelCentroid solved my problem.

Related

Find number of white/black pixels in binary image

I have an binary word image with grid and i want to find the foreground pixels (number of black and white in each segments) in each segments
So I just wanted to get the total number of black pixels for an image and I didn't want to segment it. Didn't feel like having to learn how to segment a matrix.
Easy fix to get the sum of the black pixels:
sum(sum(bw == 0))
Conversely to get the white pixels
sum(sum(bw == 1))
The operation function sum(bw == 0) just gets everything to a [1xn] matrix, so you will need to perform sum and you will get the whole image matrix summed.
Thought that cumsum would do the trick, but sum(sum()) is just better for this case. Tells you how little I know about coding. Have fun with this info, note that I'm just using this to determine the area coverage of a binarized image, but for anything else particularly useful.
Assume your black and white image is bw
sum(bw==0) // number of black pixels
sum(bw==1) // number of white pixels
For each grid find the range in both directions, then apply the same idea.
For example, from x1 to x2 and y1 to y2 is one grid:
sum(bw(x1:x2, y1:y2) == 0) // will give you the black pixels you want
That is easy with logical indexing.
For each segment, do:
n_white=sum(segment(:)==1);
n_black=sum(segment(:)==0);

Algorithm to find positions list on a grid

I'm creating a small 2D game (With Javascript) on a grid. In this game, I have 3 kind of unit types let's say:
green: unit which have a shot range of 1 tile (they can attack a target 1 tile around)
orange: unit which have a shot range of 2 tiles
blue: unit which have a shot range of 3 tiles
Here's a schema:
The black square is the target. The gray one is the unit.
The movement of the unit is OK, I can move on wathever square when I want. The target is fixed.
My question is:
How can I find, according the unit color (range 1,2 or 3), all the tiles where the unit will be able to shot?
I mean:
Blue unit can shot from: blue, orange and green tiles
Orange unit can shot from: orange and green tiles
Green unit can shot from: green tiles
I though about an ugly solution with two nested loops but, maybe there is a known algorithm to do this...
I have a (x,y) position for the target and the unit
I saw this one Algorithm for finding spaces to attack target within move-attack area on a 2D grid game board but my probleme seems to me more simple:
Can you help me?
What about this?
boolean canShot(Unit unit, Target target){
if ((unit.minimumShotDistance <= Math.abs(unit.position.x - target.position.x) &&
unit.minimumShotDistance <= Math.abs(unit.position.y - target.position.y)){
return true;
}
return false;
}
Depending on color of unit tile, set value of range=1(green), 2(orange) or 3(blue).
Now, all tiles in range (x,y) such that:
x = targetX - range to targetX + range
y = targetY - range to targetY + range
and (x,y) is within board limits
will give all the tiles from where your unit can shoot.
If you want to know that whether from its current position, unit can shoot the target or not, check if unit's coordinates (x,y) satisfy above conditions.

Matlab detect rectangle from image [duplicate]

I need to know how to align an image in Matlab for further work.
for example I have the next license plate image and I want to recognize all
the digits.
my program works for straight images so, I need to align the image and then
preform the optical recognition system.
The method should be as much as universal that fits for all kinds of plates and in all kinds of angles.
EDIT: I tried to do this with Hough Transform but I didn't Succeed. anybody can help me do to this?
any help will be greatly appreciated.
The solution was first hinted at by #AruniRC in the comments, then implemented by #belisarius in Mathematica. The following is my interpretation in MATLAB.
The idea is basically the same: detect edges using Canny method, find prominent lines using Hough Transform, compute line angles, finally perform a Shearing Transform to align the image.
%# read and crop image
I = imread('http://i.stack.imgur.com/CJHaA.png');
I = I(:,1:end-3,:); %# remove small white band on the side
%# egde detection
BW = edge(rgb2gray(I), 'canny');
%# hough transform
[H T R] = hough(BW);
P = houghpeaks(H, 4, 'threshold',ceil(0.75*max(H(:))));
lines = houghlines(BW, T, R, P);
%# shearing transforma
slopes = vertcat(lines.point2) - vertcat(lines.point1);
slopes = slopes(:,2) ./ slopes(:,1);
TFORM = maketform('affine', [1 -slopes(1) 0 ; 0 1 0 ; 0 0 1]);
II = imtransform(I, TFORM);
Now lets see the results
%# show edges
figure, imshow(BW)
%# show accumlation matrix and peaks
figure, imshow(imadjust(mat2gray(H)), [], 'XData',T, 'YData',R, 'InitialMagnification','fit')
xlabel('\theta (degrees)'), ylabel('\rho'), colormap(hot), colorbar
hold on, plot(T(P(:,2)), R(P(:,1)), 'gs', 'LineWidth',2), hold off
axis on, axis normal
%# show image with lines overlayed, and the aligned/rotated image
figure
subplot(121), imshow(I), hold on
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1), xy(:,2), 'g.-', 'LineWidth',2);
end, hold off
subplot(122), imshow(II)
In Mathematica, using Edge Detection and Hough Transform:
If you are using some kind of machine learning toolbox for text recognition, try to learn from ALL plates - not only aligned ones. Recognition results should be equally well if you transform the plate or dont, since by transforming, no new informations according to the true number will enhance the image.
If all the images have a dark background like that one, you could binarize the image, fit lines to the top or bottom of the bright area and calculate an affine projection matrix from the line gradient.

Compute contour of a binary image using matlab

I am trying to compute contour of a binary image. Currently i identify the first non zero and the last non zero pixel in the image through looping. Is there a better way? i have encountered few functions:
imcontour(I)
bwtraceboundary(bw,P,fstep,conn,n,dir)
But the first doesn't return the x and y coordinates of the contour. The second function requires a seed point which i cannot provide. An example of the image is shown below. Thanks.
I'm surprised you didn't see bwperim. Did you not try bwperim? This finds the perimeter pixels of all closed objects that are white in a binary image. Using your image directly from StackOverflow:
im = im2bw(imread('http://i.stack.imgur.com/yAZ5L.png'));
out = bwperim(im);
imshow(out);
We get:
#rayryeng have already provided the correct answer. As another approach (might be that bwperim performs this operations internally) boundaries of a binary image can be obtained by calculating the difference between the dilated and the eroded image.
For a given image:
im = im2bw(imread('http://i.stack.imgur.com/yAZ5L.png'));
and a given binary structural element:
selem = ones(3,3); %// square, 8-Negihbours
% selem = [0 1 0; 1 0 1; 0 1 0]; %// cross, 4-Neighbours
The contour of the object can be extracted as:
out = imerode(im, selem) ~= imdilate(im, selem);
Here, however, the boundary is thicker than using bwperim, as the pixels are masked in both inside and outside of the object.
I had the same problem, stumbled across this question and just wanted to add that imcontour(Img); does return a matrix. The first row contains the x-values, the second row contains the y-values.
contour = imcontour(Img); x = contour(1,:); y = contour(2,:);
But I would discard the first column.

Break down picture in reactangles

I'm looking for an algorithm.
I want to draw a image (2d-array of pixels) with the lowest number of rectangles . It's possible to overwrite already drawed areas by a new rectangles.
In the first step I convert every pixel of the picture to a quad with the size 1x1 and a color. Than I want to reduce the number of objects by creating bigger rectangles.
In the end I want an array of rectangles. When I iterate over it and draw it on the pane, I want to have the original picture.
Is there any algorithm?
The runtime doesn't matter.
Example1:
|.bl.|.bl.|.bl.|-----|.bl...........|
|.bl.|.gr.|.bl.| -> |...............| + |.gr.|
|.bl.|.bl.|.bl.|-----|..............|
bl = black, gr = green
Example2:
|....|....|.bl.|
|.bl.|.bl.|.bl.| --> |.bl.|.bl.|.bl.| + |.bl.|
|.bl.|.bl.|.bl.|-----|.bl.|.bl.|.bl.|
I was looking for Quad Tree Compression :)
http://www.gitta.info/DataCompress/en/html/rastercomp_chain.html

Resources