Calculate centers of multiple point clusters in matlab - matlab

For a task for my image processing course we need to decode a QR code. The first step in this is to find the centers of the alignment blocks. We can find the horzontal an vertical centers of the alignment blocks trough a row and column scan on the ratios, but we want to try to increase accuracy of this by taking the results of multiple line scan actions on different rows in the neighbourhood. Instead of one horizontal scan and one vertical scan.
This results in 3 clusters of multiple points. How can we reduce these 3 clusters to 3 single points in the center of these respective clusters?
(The stray points in this image will still be filtered by matching it with a vertical scan.)

Related

Intersection over union for rectangles with different orientation

I need to implement an algorithm in swift to find the intersection over union (IoU) between two rectangles with different orientations in 2-dimensional space. I could not find any tutorials or sample codes to teach how to implement such an algorithm.
Could someone provide relevant resources?
You can use O'Rourke algorithm for calculation of intersection of two convex polygons.
C and Java code is availaible on the page for book "Computational Geometry in C"
Algorithm traverses edges of polygons until it finds intersection (using
orientation test). After intersection it chooses "the most inner" edge from two possible next ones to build intersection core polygon (always convex).
When you have ordered list of vertices, you can calculate polygon area with shoelace formula.
To get area of union, we can calculate (thanks to Yves Daoust for hint)
Area(Union) = Area(P) + Area(Q) - Area(Intersection)
Alternatively to the very good solution of MBo/O'Rourke, you can use a sweep line approach.
For convenience, assume that one of the polygons is axis aligned. Then there are two "events" when the line hits the top and bottom sides of the aligned rectangle and four events when the line hits the vertices of the other (depending on the orientation, there are 8 possible permutations of the vertices).
The intersection of the rectangles occurs inside the vertical ranges defined by these events (you perform a merge of the two event sets) and there are up two six intersections to be computed between the oblique sides and the horizontals. For every event line, it is an easy matter to determine the X intervals spanned by both rectangles, and find their intersection or union. And the areas between two event lines are trapezoids.
To cope with rectangles in general position, you can
rotate both polygons to align one of them,
work with a counter-rotated coordinate system,
perform the sweep with an horizontal line anyway without moving the polygons; but then there are 8 events instead of 6 and up to 8 intersection computations.

Detecting line artifacts in noisy images

I'm doing image processing on computed tomography projection images. There's a specific type of artifact that results from the processing I'm doing which manifests as a vertical line going through the whole image:
I'm currently detecting it by comparing the mean of each column. If the mean is less than half the mean of both the left side and the right side column neighbours, then the column is deemed as a line artifact. It is then interpolated as the maximum of the left and right side neighbour pixels.
The interpolation works well (right side of the image), but the detection is too ad hoc. It also fails pretty often, as many of the columns containing only the black background can fulfill that condition due to the heavy Poisson noise apparent. This causes artifacts in filtering out the noise which is the next phase. I'm using BM3D with great results and do not wish to median filter the whole image.
Can you think of a better way to detect these 'line artifacts'? Note the strong borders of the objects in the images and the heavy noise included also in the artifact.
We want to find vertical lines in the image so first convolve the image with the filter [1 -2 1]. This will give high values for pixels that are lower than their vertical neighbors.
Sum all the columns of the image.
Find the index of column with the maximum value. This column is the problematic one.

An gridded area is divided into several sub-areas. How to track each grid's membership?

I have a square that is grid into many small cells. Each cell has a size of 0.1*0.1. Several zigzag lines are drawn to divided that square into sub-area. The zigzag lines only follow the cell edges.
The vertices that these zigzag lines cross are calculated and stored in a different matrix. In this case, there are 6 zigzag lines that meet at the center, thus there are 6 matrix that store the coordinated of the lines.
The center of each cell, as well as the four vertices, are calculated and store in a big matrix. Now, if I want to track to which sub-area each of the cell belongs, what looping algorithm should I use?
Say, we mark the top left area as 1, and count it clockwise. Then the cells in the top left area should be marked 1. Cells in the top right area should be marked 2, and so on.
From the given example, we can infer that the areas are "sectors" with a common center and the radial lines meeting the outline.
You can get the starting edges (on the outline) of the zigzag lines from their respective array. They each separate two cells belonging to different areas. By computing the angles and sorting them, you can number the areas increasingly and associate them a starting cell.
Now "draw" the zigzag lines in the big matrix (every cell having two flags that indicate the presence of an edge to the right or below it). Then, starting from the cell of every area, use a seed filling algorithm. You will need to modify the basic algorithm to account for the presence of the edges.

Matlab - Concatenation of overlapping blocks with weighted average

I'm looking for a quick way to combine overlapping blocks into one image. Assume the size of the full image and the coordinates of each block within the full image are known. Also assume the blocks are regularly spaced both horizontally and vertically.
The catch - in the overlapping region, a pixel in the output image should get a value according to a weighted average of the corresponding pixels in the overlapping blocks. The weights should be proportional to the distance from the block center.
So, for example, take a pixel location p (relative to the full image coordinates) in the overlapping region between block B1 and B2. Assume the overlap region is due to a horizontal shift only of size h. If B1(p) and B2(p) are the values at that location as they appear in blocks B1,B2, and d1,d2 are the respective distances of p from the center of blocks B1 and B2 then in the output image O the location p will get O(p) = (h-d1)/h*B1(p) + (h-d2)/h*B2(p).
Note that generally, there can be up to 4 overlapping blocks in any region.
I'm looking for the best way to do this in Matlab. Hopefully, for any choice of distance function.
blockproc and alike can help splitting an image into blocks but allow for very basic combination of results. imfuse comes close to what I need, but offers simple non-weighted alpha blending only. bwdist seems to be useful, but I haven't figured what the most efficient method to put it to use is.
You should use the command im2col.
Once you have all your patches in vectors aligned in one matrix you'll be able to work on the columns (Filtering per patch) and rows (Filtering between patches).
It will be trickier than the classic usage of im2col but it should work.

I have x points in a 2D space. How can I split them into the largest clusters possible with max radius r for each cluster and no overlaps?

(source: hiveworkshop.com)
In the above picture, I have 20 arbitrary points split into 5 clusters. On the right is a circle that defines the max cluster size. On the top right is the maximum points per cluster. I want to be able to take any arbitrary set of points k and split them into clusters of max sizes n with max radii r. I need the algorithm to retrieve the most full clusters possible (in the above example, as many clusters of 4 as possible). Any given point can only belong to 1 cluster and clusters may not overlap.
It would also be helpful if the algorithm could add/remove points to an existing set and update the clusters.
I am totally lost on how to accomplish this. My best idea so far was calculating centers for sets of points and then using those centers for binary space partitioning, but the best I could hope with using that approach would be evenly distributed clusters.
Any help would be appreciated :).
edit
Don't overlap as in the shapes that regions form don't intersect with the shapes other regions form and that regions are not located inside of other regions (circles in circles for example). In the above picture, each region has a shape to it. None of these shapes intersect and no region is inside of another region.