Optimization: minimize painting errors - math

You are given an m*n grid, where each cell is marked either "b" or "w". You are also given black and white paints. You are allowed to use k strokes, each of any color (black OR white), a stroke is defined as coloring of contiguous uncolored cells from the same row (which means a stroke can not go beyond the length of the row, also if you pick up your brush before the end of the row thats the end of that stroke). The aim is to minimize the number of errors, an error occurs if you paint a cell with wrong color OR a cell remains unpainted. What is the optimal strategy?

Knowing solution for one row problem (what is a minimal number of errors with k strokes on a given BW row) can be used to solve problem.
For each row make list of number of errors for given number strokes k_i = [0, 1, ..., min k needed to cover i-th row]. Now we have n lists (of different sizes). To find in which rows to use 'k' strokes, it is enough to iteratvely pop k elements from begining of list which stroke cover most cells.
So, main task is to solve one row problem, and I'm not sure how :-)
Let C be number of colour changes in a row. Than minimal number of strokes to cover row is a ceil( (C+1)/2 ). That can be done by alternate stroke colour with first stroke to cover whole row and next strokes between most distant change in a last stroke. First stroke has colour of one (or both) end(s).
I think , with similar approach it is possible to find number of errors when there isn't enough strokes to cover whole row. Some ranges of one colour have to be omitted. That is be done by:
starting with colour that is not on a boundary (omitting first stroke),
some strokes are not between most distant change in last stroke, but
between closer changes.
I'm not sure, but it seems like it is enough to find few smallest same colour parts and that is what will stay as an error. Probably it is important how far these parts are from ends.
That is for now ...

Related

How can I scale a glyph down vertically while keeping the vertical stroke widths the same (and not altering any of the horizontal dimensions)?

I'm using FontForge. I'm modifying the lower case q to make a straight-stalked 9. The q has 2 logical parts, the stalk, and the 'c'. The 'c' is too big vertically. How can I scale it down vertically while keeping the vertical stroke widths the same (and not altering any of the horizontal dimensions)?
I'm a novice with FontForge, so please spell out your explanation and provide step-by-step instructions. Thanks for your help.
It sounds like you want to decrease the x-height of the 'q' without changing stroke widths.
Font-forge provides a built-in tool to achieve this: Element > Styles > Change X-Height. You might like to experiment with this, but in practice it gives you very little control over the results and I would rarely use it.
Instead I would achieve this by directly modifying the nodes of the paths.
First, I would ensure that InterpolateCPsOnMotion is enabled. Double-click the pointer tool in your toolbox to access this setting.
This will help ensure that curves scale correctly (rather than distort) as you move control points. Now, I would select the nodes at the top and sides of the bowl of the q:
and use the down arrow key to move them down about half the distance you wish to decrease the height by. Then I would deselect the nodes at the side of the bowl:
and lower the remaining nodes the rest of the distance:
You will need to check the resulting appearance and possibly make tweaks to get it perfect. Note that this or any scaling technique can subtly distort the axis of modulated strokes, which you may wish to correct.
This technique presupposes that nodes are sensibly placed at the vertical and horizontal extrema of the bowl, and that you don't have extra nodes between these extrema. If you are not in this happy situation, you can add extrema by ctrl-shift-x and you can remove surplus nodes by selecting them and ctrl-m. If you can't remove extra nodes without significantly changing the shape of the bowl, you'll just have to modify these nodes by eye.
Another point: you say you're working from a "c". I'm not sure whether you just mean the C-shape of the bowl of the q, or whether you mean you are copying the actual glyph 'c'. Note that it is rare that the bowl of a 'q' will have exactly the same shape and weight as a 'c'. Typically the stroke will be somewhat lighter to achieve the right visual grey, and especial care will be taken where it intersects the stem. Often the two shapes will differ substantially.

R/Shiny: How to detect overlapping circle markers?

I'm using Leaflet and Shiny with circle markers. Trying to figure out how to detect if a circle marker overlaps with one or more markers. I need to set the color of each marker based on whether they overlap or not. Have any of you done something like this before? Thankful for any suggestions :)
You can use an accumulator. Represent the empty space as a n by m matrix of 0s, so each cell of the matrix represents a single point of the lowest granularity (like a pixel). Now mark the position of your marking in the accumulator with a 1. If you take the sum of the matrix at this point is should be 1. Now for all points that would be included in your circle mark with a 1 on the same accumulator. Now check the sum of the accumulator, if it is equal to the number of points in the circle + 1 the circle does not cover the point, but if is only equals the number of points the circle the marker is located within the circle.
Edit: If you want to look for overlaps: instead of just setting points to 1, increment the points by 1 for every object including it. So an over laps would have the value of 2 and a triple overlap would be a 3, etc. You could then find these by searching for local or global maximum or minimums.

Fading effect with QCPItemTracer

I have been able to use QCPItemTracer to trace a specific point on my data when plotting. How do I achieve a fade out effect? That is, as the next point is plotted, the last n points fade out slowly. Does Qt provide such a feature?
I'm not familiar with this class of QCustomPlot but it should be easy to implement what you are asking for your self. You just need to keep track of the last n points. When it comes to plotting this is often referred to as oscilloscope-type persistence.
Fade out effect is usually achieved by gradually changing either the alpha channel or the color value of the item you want to affect. The first is relatively easy but requires alpha support (QCustomPlot does support it) and decreases performance of your plotting tool. The second requires you to calculate a gradient starting with the color the point was originally plotted with and going all the way up/down to whatever background color you have selected for your plot. The gradient step can directly be derived from n.
For every n+1 point just iterate through the n points before that
For each of those points reduce the alpha or change the color
I'm presuming that fade out effect you want also needs to be distributed unequally among all points based on their age with point n (the youngest) being the least affected and point 0 (the oldest) being the most affected by the fade out effect like this (from left to right age of a point increase):

How to deal with arbitrary size for Laplacian Pyramid?

Recently I had much fun with the Laplacian Pyramid algorithm (http://persci.mit.edu/pub_pdfs/pyramid83.pdf). But one big problem is that the original paper is limited to 2^m+1*2^n+1 images. My question is: What is the best way to deal with arbitrary w*h instead? I can think of a couple of options:
Up sample the input to the next 2^m+1,2^n+1 up front
Pad even lines. How exactly? Wouldn't it shift the signal?
Shift even lines by half a sample? Wouldn't it loose half a sample?
Does anybody have experience with this? What is the most practical and efficient approach? Also any pointers to papers dealing with this would be very welcome.
One approach is to create an image with a width and height equal to the next 2^m+1,2^n+1, but instead of up-sampling the image to fill the expanded dimensions, just place it in the top-left corner and fill the empty space to the right and below with a constant value (the average value for the image is a good choice for this). Then encode in the normal way, storing the original image dimensions along with the pyramid. When decoding, decode and then crop to the original size.
This won't introduce any visual artifacts or degradation because you aren't stretching or offsetting the image in any way.
Because the empty space to the right and below the original image is a constant value, the high-pass bands at each level in the image pyramid will be all zero in this area. So if you are using a compression scheme like run length encoding to store each level this will be automatically taken care off and these areas will be compressed to almost nothing. If not then you can simply store the top-left (potentially non-zero) area of each level and then fill out the rest with zeros when decoding.
You could find the min and max x and y bounding rectangle of the non-zero values for each level and store this along with the level, cropped to include only non-zero values. The decoder could also be optimized so that areas of the image that are going to be cropped away are not actually decoded in the first place, by only processing the top-left of each level.
Here's an illustration of the technique:
Instead of just filling the lower-right area with a flat color, you could fill it with horizontally and vertically mirrored copies of the image to the right and below, and a copy mirrored in both directions to the bottom-right, like this:
This will avoid the discontinuities of the first technique, although there will be a discontinuity in dx (e.g. if the value was gradually increasing from left to right it will suddenly be decreasing). Choosing a mirror that keeps dx constant and ddx zero will avoid this second-order discontinuity by linearly extrapolating the values.
Another technique, which is similar to what some JPEG encoders do to pad out an image to a whole number of MCU blocks, is to take the last pixel value of each row and repeat it, and likewise for columns, with the bottom-right-most pixel of the image used to fill the bottom-right area:
This last technique could easily be modified to extrapolate the gradient of values or even the gradient of gradients instead of just repeating the same value for the remainder of the row or column.

measure correlation of colored pixels

I need a way to measure color correlation of pixels. For example, it's obviously that color correlation between those two pixels chains is higher
comparing to two chains below
.
Ok, I can:
correlate R,G,B values separately but what to do next ? I need to obtain only one figure, not three.
I also can transform RGB to HUE representation, but it looks that for all "grey"-colors (from black to white) H-component = 0, so, correlating different luminance grey-pixels will give same value...
So, I need your suggestions and help :)
thanks
In order to compare the R, G and B composents between two pixels, cosine similarity http://en.wikipedia.org/wiki/Cosine_similarity is certainly the best solution.
After that, if you want to compare the arrays only by comparing couples of pixels that are in the same index, you can just sum the values. You can also sum a rapidly decreasing function of the values as they decrease (for example the square) if you want to discard cases where nearly all the pixels are the same but with one or two big differences. You can take the square root if you want the opposite (privilegiate the number of nearly identical pixels rather than the fewest differences).

Resources