2020 08 02 Merge regions back together. - syntaxmonkey/Thesis GitHub Wiki
Need to merge the regions to resemble the original image.
Determining relative X, Y of each region
We know the region index before we call processMask.
From the region information, we can we determine the delta X, delta Y?
We have the regionMap variable hashmap? Yes. We can obtain the coordinates in the region. Then, we can find the top-left coordinate.
regionCoordinates = regionMap.get(region)
Let's test this. When drawing the raster, we can mark the topLeft pixel.
As it turns out, the "topLeft" is actually the intersection of the top pixel and the left most pixel. It is NOT the topLeft active pixel.
topLeft = (np.max(regionCoordinates), np.max(regionCoordinates))
When generating the coordinates for the mask, we further pad the x,y coordinates by 5. When we account for this padding, we finally find the actual top left corner.
shiftx, shifty = topLeft[0]-5, topLeft[1]-5
So, to place the contour lines back onto the original image, we need to perform the following transformation:
x, y = x + shiftx + 5, y + shifty + 5
Recreating canvas of original image.
Should be the same size as the original image.
We have the region merging working. Here is a screenshot of the original image regions along with the merged imaged of the first 10 regions.
There are still some outstanding issues:
- Since we utilized the opening operation on the original mask, the contour lines will be larger than the original region size. The contour lines take up more space than the original regions and somewhat overlap.
- The density of the lines is quite high. We need to perform some level of line culling.
Fixing the overlap
- Scale the generated contour lines by some fixed scale.
- Draw the lines such that the line ends diminish.
If we change the scale to 85%, we produce the following:
TODO:
- Crop the contour lines before merging. done
- Try other images.
- Overlay the contour lines on the SLIC regions. done
- Colour the SLIC regions based on average intensity of each region. The intensity should be the greyscale intensity. done
- Cull the contour lines based on line density. Determine the distance between the contour lines based on an average distance between multiple points along each line. done
Crop the contour lines
We'll obtain the pixels for each region. We will perform the cropping based on the raster. We will shift the coordinates of the contour lines to the coordinate system of the raster. If the corresponding pixel in the raster is enabled, then we keep the line point. Otherwise, we discard the line point.
Here are the results for region 11. The first image are the generated contours.
The cropped contour lines.
This raster was used for cropping.
The resulting merged regions after cropping.
Convert the region raster to greyscale
The raster for the SLIC regions is already being processed. It is false colour based on the the regionlabel.
raster[i][j] = (regionLabel * 234867)%256
We can convert the image to greyscale and calculate the average intensity for each region. We can now display the average intensity of each region and display the boundaries of each region.
Plot the contour lines onto the SLIC Regions
- Plot the regions onto the finished image.
- Plot the contour lines onto the finished image. Can now draw contour lines onto SLIC regions for diagnostic purposes.
Plotting all the regions back, we get the following:
Cull contour lines based on intensity of the region.
- Sort the exterior points when they are generated.
We can now cull the lines by only drawing every X lines. Here is a sample where we only draw every third line.
- We can also cull based on the average distance between the lines. We average out the distance between the respective end points and the center point. If this average distance is greater than the radius * factor, then we draw the line. Otherwise we skip it. We continue to the next line and check the average distance against the last line drawn.
Here we use a factor of 3.
This uses a factor of 2.
Finding the "Distance" between lines
We were using the average distance between the lines. However, if we check the distance of the middle points of the lines, the resulting distances between the lines is more consistent. Comparing the two images below, 3 point distance average vs middle point distance, the middle point distance check seems to produce more consistent line spacing most of the time. In some cases, where the lines have significantly differing number of points or where the middle point in the lines are not aligned, the middle point distance check doesn't work as well.
However, for the 3 point distance average, curved lines that diverge at the ends are actually placed too close together.
3 point distance average between lines
Middle point distance between lines
Could we implement the closest pair of points algorithm?
The problem with using the middle point distance is that we cannot reliably determine the middle of the line based on the line point indeces. Instead, we could potentially implement "Closest pair of points" algorithm to determine the minimum distance between the lines: https://en.wikipedia.org/wiki/Closest_pair_of_points_problem.
- May be able to use scipy.spatial package to calculate the distances: https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.cdist.html
- Can then find the points that are closest: https://thispointer.com/numpy-amin-find-minimum-value-in-numpy-array-and-its-index/
Implemented Closest Point pairs. However, the spacing is unexpected. Here is a screen shot of some lines when factor is 3. The green dots indicate the starting line point while the red dot is the second line dot. The distance between the green dot and the red dot is the shortest distance between the two lines. In some cases where the points along one side of the line are closer together while the other end of the lines are further apart, we have a lot of culling and the lines are actually quite far apart.
However, most of the regions that have relatively regular lines, the spacing is quite consistent.
TODO:
- Draw lines that are not perfectly vertical.
- Draw cross hatching.
Can now draw lines at an angle
Here are some images of different angles and their respective transformation.
15 degree angle
40 degree angle
90 degree angle
125 degree angle
Create cross hatch
We are able to draw the cross hatch on the flatted mesh. However, we overwrite the line information each time we call drawLine. Need to change the code so we merge the lines.
Cross hatching now works.
Example of cross hatch using 0 degrees and 90 degrees
- Are there any other concerns? For example, determining which lines to cull.