set's membership seems not to work (python) - set

I am working on a image search algorithm that finds certain shapes of certain colors; to save time I only register half of the shape's perimeter in 2 distinct sets, one for the rows and one for the columns used by the shape. The idea is that whenever I find a point which has the target color, I then check if this point's row and column are in a master set (which have both the previous sets); if they are I skip it, if they are not then I initialize 2 recursive fuctions that register the first row and the first column of the shape.
Since it's for a school project, my images are specially tailored
and the code would be
for y in range(height):
for x in range(width):
if img[y][x] == target:
if y in master_set and x in master_set:
continue
else:
row = set()
column = set()
flood_fillv2_y(img,x,y,target,column)
flood_fillv2_x(img,x,y,target,row)
row=frozenset(row)
column=frozenset(column)
master_set.add(row)
master_set.add(column)
The idea then is to check the len of master_set to see how many shapes I have, but as I said what I get is that y and x are never in the master set so it keeps doing it for all points of the shape, resulting in a wrong number.

It's hard to give a good answer without seeing the whole code, but I can give a guess:
master_set.add(row) literally adds the frozenset row to the master_set, but you probably want all elements from the set to be added to master_set. Take a look at the update() method of sets.
Does this help?

Related

D3 sort() results in weird unexpected behaviour

I'm working on a motion chart showing the development of certain data points through time by updating the position and size of the corresponding circle on a chart (similar to https://bost.ocks.org/mike/nations/ ).
Since circles can overlap I need the smallest one to be drawn on top.
In the example this is accomplished by calling selection.sort(), but if I do the same (with the newer D3v4) I get an unexpected behaviour. The sort seems to switch the data objects associated to the visual circle object.
Check this fiddle to see. Run it once as it is (without calling sort(order) in line 45) - this is the expected behaviour. The y value of each object doesn't change so the circles should move on a horizontal line. Now uncomment line 45 to call the sort on each update and run it again. This time the paths of the circles suddenly cross (because the underlying object is switched).
https://jsfiddle.net/orj1rcy8/1/
The API states selection.sort() Returns a new selection that contains a copy .... I assume this is the problem, however I don't understand at this point how the correct approach would look like.
Short answer
You need a key function when you bind your data:
.data(dataForKey(keyIndex), function(d){ return d.name})
Here is your updated fiddle: https://jsfiddle.net/7kyvzkwe/
Long answer
What happened here is that you were victim of what we call object constancy, or, more precisely, not properly setting an object constancy (here is a good reading about it, written by Mike Bostock).
The problem is that, in D3, the data is bound to the elements in their order:
If a key function is not specified, then the first datum in data is assigned to the first selected element, the second datum to the second selected element, and so on.
So, let's see what happened. Your code has three circles, in this order:
The smallest circle, r = 3;
The medium circle, r = 10;
The largest circle, r = 15.
The data is appended in that above-mentioned order. But then, when you do sort(order):
function order(a, b){
return radius(b) - radius(a);
}
You sort the elements, and now you have:
The largest circle, r = 15;
The medium circle, r = 10;
The smaller circle, r = 3.
And then comes the problem: next time you run the next function and bind the new data, you're binding the data to DOM elements in that new order. That is, the data regarding the smallest circle is being bound to the largest circle in the DOM. Using the names of your elements, you're binding the data regarding item1 to the item3 in the DOM (given you have 3 elements, the only one always receiving the correct data is item2).
You can understand this better in the next fiddle. In this next fiddle, I'm using exactly your code, uncommenting the sort function. But, unlike your original code, here I'm changing the order of the data, so we have the largest circle first, then the medium, then the smallest:
[{name:"item1", x:1, y:2, z:15},
{name:"item2", x:1, y:4, z:10},
{name:"item3", x:1, y:6, z:3}];
You can see that, even calling sort, the circles stay in their positions. Here is the fiddle: https://jsfiddle.net/9p1tL43j/
That happens because the next time the function next runs, the data for the smallest circle is bound to the smallest circle (in the DOM), and so on...
Thus, in a nutshell, you need a key function if you want to keep the object constancy. A key function:
... may be specified to control which datum is assigned to which element, replacing the default join-by-index.

Creating a scatter plot in visual basic with irregular x-axis values

I've written a program which reads measurements from an impedance analyzer as it sweeps over a range of frequencies or voltages, saves the data to a text file, and also creates a scatter plot. In one type of measurement, I obtain x and y values for complex impedance, neither of which are the independent parameter. Now when plotting this graph, it appears that it simply puts each x value to the right of the previous one at regular spacings resulting in x axis labels looking like, from left to right, [45000, 43000, 40000,... etc.].
I've tried forcing the x-axis to start from zero which did not change anything and haven't been able to find much else on this. Is there a way to make sure the plot reflects the actual x values of each point?
Here's my current method of creating the chart,which pulls the data from the already created table:
For Each row In table.Rows
Chart1.Series("series1").Points.AddXY(row(0), row(1))
Next

What's the name of the algorithm concerning daily planner rendering?

Is there a well-known algorithm that is able to take as input a collection of time-bound items (defined by a start time and an end time) and produce a "graphical" layout? By graphical I mean a bi-dimensional projection of those events (2d matrix, 2d space boundaries, whatever).
The output has to be bi-dimensional because the input may contain overlapping events (events beginning at the same time etc.). One dimension would be the time, of course, and the other one is an artificial one.
If we associate a vertical axis y with the time dimension and a horizontal one, x, with the artificial dimension, then I am thinking about an algorithm playing with X and Y tokens, about token requirements and tokens availability.
E.g. the algorithm used by Outlook to render the daily view of the calendar etc.
Thank you!
PS: I believe the term "projection" is not correct, because we are adding an artificial dimension :)
PPS: Maybe what I want is one of these?
These slides: http://www.cs.illinois.edu/class/fa07/cs473ug/Lectures/lecture2.pdf call that "interval partitioning" (second part of the slides - haven't found another reference to that term elsewhere) and give a proof that a greedy algorithm works: sort the items by start time; when processing an item, if you can put it in one of the "bins" already there, put it there, otherwise start a new bin and put the item there.

How to print comments at the right side of a matlab graph?

I want to plot only one simple set of data. For example, my plot command could be :
x = (1:10);
y = ones[1,10];
plot(x,y);
In fact, the y data set could have been generated by a previous code, depending on several parameters. I want to print the name of every parameters and there values outside the graph, at the right of it, as if it were a legend. My problem is that I have several parameters to print, but only one set of data.
I tried to do this by the text or legend functions, but it never fit completly my needs.
Could you help me please ?
I think this code should help you out. Its probably easiest to split your figure into two axes, the right one just to hold text:
x = rand(1,10);
y = rand(1,10);
figure % makes your figure
axes('Position', [0.05,0.05,0.45,.9]) % makes axes on left side of your figure
scatter(x,y)
axes('Position', [0.55,0,1,1],'ytick',[],'xtick',[]) %make axes on left side of your figure, turns of ticks
text(0.05,0.85,{'Parameter 1: blah blah';'Parameter 2: bloop bloop';'Parameter 3: ....'},'Interpreter','Latex')
Play around with the numbers in the brackets to resize things as you like.

How to detect boundaries of a pattern [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Detecting thin lines in blurry image
So as the title says, I am trying to detect boundaries of patterns. In the images attached, you can basically see three different patterns.
Close stripe lines
One thick L shaped line
The area between 1 & 2
I am trying to separate these three, in say 3 separate images. Depend on where the answers go, I will upload more images if needed. Both idea or code will be helpful.
You can solve (for some values of "solve") this problem using morphology. First, to make the image more uniform, remove irrelevant minima. One way to do this is using the h-dome transform for regional minima, which suppresses minima of height < h. Now, we want to join the thin lines. That is accomplished by a morphological opening with a horizontal line of length l. If the lines were merged, then the regional minima of the current image is the background. So we can fill holes to obtain the relevant components. The following code summarizes these tasks:
f = rgb2gray(imread('http://i.stack.imgur.com/02X9Z.jpg'));
hm = imhmin(f, h);
o = imopen(hm, strel('line', l, 0));
result = imfill(~imregionalmin(o), 'holes');
Now, you need to determine h and l. The parameter h is expected to be easier since it is not related to the scale of the input, and in your example, values in the range [10, 30] work fine. To determine l maybe a granulometry analysis could help. Another way is to check if the result contains two significant connected components, corresponding to the bigger L shape and the region of the thin lines. There is no need to increase l one by one, you could perform something that resembles a binary search.
Here are the hm, o and result images with h = 30 and l = 15 (l in [13, 19] works equally good here). This approach gives flexibility on parameter choosing, making it easier to pick/find good values.
To calculate the area in the space between the two largest components, we could merge them and simply count the black pixels inside the new connected component.
You can pass a window (10x10 pixels?) and collect features for that window. The features could be something as simple as the cumulative gradients (edges) within that window. This would distinguish the various areas as long as the window is big enough.
Then using each window as a data point, you can do some clustering, or if the patterns don't vary that much you can do some simple thresholds to determine which data points belong to which patterns (the larger gradient sums belong to the small lines: more edges, while the smallest gradient sums belong to the thickest lines: only one edge, and those in between belong to the other "in-between" pattern .
Once you have this classification, you can create separate images if need be.
Just throwing out ideas. You can binarize the image and do connected component labelling. Then perform some analysis on the connected components such as width to discriminate between the regions.

Resources