How does one calculate the integral image from original? - image

I tried separating the individual channels of the image and then calculate using the recursive function. At the end, I joined the three channels:
function [ii] = computeIntegralImage(image)
%function to compute integral from original image
iip=zeros(size(image,1)+1,size(image,2)+1);
jjp=zeros(size(image,1)+1,size(image,2)+1);
kkp=zeros(size(image,1)+1,size(image,2)+1);
for i=2:size(iip,1)
for j=2:size(iip,2)
iip(i,j)=image(i-1,j-1,1)+iip(i,j-1)+iip(i-1,j)-iip(i-1,j-1);
end
end
for i=2:size(jjp,1)
for j=2:size(jjp,2)
jjp(i,j)=image(i-1,j-1,2)+jjp(i,j-1)+jjp(i-1,j)-jjp(i-1,j-1);
end
end
for i=2:size(kkp,1)
for j=2:size(kkp,2)
kkp(i,j)=image(i-1,j-1,3)+kkp(i,j-1)+kkp(i-1,j)-kkp(i-1,j-1);
end
end
ii= cat(3,iip,jjp,kkp);
The matlab output for function integralImage is completely white:
My output is a colorful image:

The integral image can be easily computed by first integrating over one axis, then integrating the result over the other axis. This 1D integral is computed with cumsum:
out = cumsum(image,1);
out = cumsum(out,2);
Note that if image is of an integer type, this is likely going to lead to overflow. You should convert such an array to double first.
Finally, to display the result you need to use
imshow(out,[])
otherwise you don’t see the full range of the data, and anything above 1 becomes white, as you saw with MATLAB’s result.
Regarding your code:
The problem is overflow. Convert the value taken from input to double first. In MATLAB, uint8(150)+150 == uint8(255). This leads to alternating rows and columns like you see: one step you subtract some large value from the partial sums, leading to a small value, the next step you subtract a small value leading to a large value, etc.
At first I was confused by your first row and column in the output, which remain at 0. But then I noticed that the output is one larger than the input, and you use this first column to avoid special cases.
Consider cropping the first row and column from your output.
Regarding loop order: It is faster when the inner loop is over the first dimension, as then the data is accessed in storage order and therefore uses the cache better. This should not affect the result, just the timing.

Related

how to plot variables with possibly wild variable values?

I want to build an application that would do something equivalent to running lsof (maybe changing it to output differently, because string processing may mean it is not real time enough) in a loop and then associate each line (entries) with what iteration it was present in, what I will be referring further as frames, as later on it will be better for understanding. My intention with it is that showing the times in which files are open by applications can reveal something about their structure, while not having big impact on their execution, which is often a problem. One problem I have is on processing the output, which would be a table relating "frames X entry", for that I am already anticipating that I will have wildly variable entry lengths. Which can fall in that problem of representing on geometry when you have very different scales, the smaller get infinitely small, while the bigger gets giant and fragmentation makes it even worse; so my question is if plotting libraries deal with this problem and how they do it
The easiest and most well-established technique for showing both small and large values in reasonable detail is a logarithmic scale. Instead of plotting raw values, plot their logarithms. This is notoriously problematic if you can have zero or even negative values, but as I understand your situations all your lengths would be strictly positive so this should work.
Another statistical solution you could apply is to plot ranks instead of raw values. Take all the observed values, and put them in a sorted list. When plotting any single data point, instead of plotting the value itself you look up that value in the list of values (possibly using binary search since it's a sorted list) then plot the index at which you found the value.
This is a monotonous transformation, so small values map to small indices and big values to big indices. On the other hand it completely discards the actual magnitude, only the relative comparisons matter.
If this is too radical, you could consider using it as an ingredient for something more tuneable. You could experiment with a linear combination, i.e. plot
a*x + b*log(x) + c*rank(x)
then tweak a, b and c till the result looks pleasing.

SPSS: generate 'fake' survey data using rv.uniform without losing value labels

I have a pretty straightforward survey dataset. Each row is a respondent, and each column is a question. Responses have a value that is a whole number, and each number has a label.
Now, I need to replace all of those values with fake data to use in a training. I need something that looks and feels like the original dataset, but isn't actually client data.
I started by replacing my variables with random number values:
COMPUTE Q1=RV.UNIFORM(1,2).
EXECUTE.
COMPUTE Q2=RV.UNIFORM(1,36).
EXECUTE.
COMPUTE Q3=RV.NORMAL(50, 13).
EXECUTE.
(rv.normal/rv.uniform depending on what kind of data I'm trying to fake - age versus multiple-choice question, for example).
This works, but then when I try and generate crosstabs, export the dataset w value labels, etc., the labels aren't applied to the columns with fake data. As far as I can tell, my fake numbers are in the exact same format they were in before - numeric, no decimals, width of 2, nominal. The labels still appear in the variable view, but they aren't actually being applied.
I'd really prefer not to have to manually re-label every one of these columns, because there's quite a few of them. Any ideas for how to get around this issue? Or is there a smarter way to generate fake data?
Your problem is the RV.UNIFORM and the RV.NORMAL functions do not generate integers - they generate decimal numbers. You may have your display hide the decimal numbers by having 0 decimals in the variable view, but they are still there (you can check this by adding decimals in the variable view).
So you neen another step of turning your decimals into integers. For example, the following are two ways to get a random 1 or 2 (integers):
COMPUTE Q1=rnd(RV.UNIFORM(1,2)).
or
COMPUTE Q1=trunc(RV.UNIFORM(1,3)).
Once the numbers generated are integers corresponding to the value labels definition, you should be able to see the labels in the output.

Is there any option to do FOR loop in excel?

I have an excel that I'm calculating my Scrum Task's completed average. I have Story point item also in the excel. My calculation is:
Result= SP * percentage of completion --> This calculation is for each row and after that I sum up all result and taking the summary.
But sometimes I am adding new task and for each task I am adding the calculation to the average result.
Is there any way to use for loop in the excel?
for(int i=0;i<50;i++){ if(SP!=null && task!=null)(B+i)*(L+i)}
My calculation is like below:
AVERAGE((B4*L4+B5*L5+B6*L6+B7*L7+B8*L8+B9*L9+B10*L10)/SUM(B4:B10))
First of all, AVERAGE is not doing anything in your formula, since the argument you pass to it is just one single value. You already do an average calculation by dividing by the sum. That average is in fact a weighted average, and so you could not even achieve that with a plain AVERAGE function.
I see several ways to make this formula more generic, so it keeps working when you add rows:
1. Use SUMPRODUCT
=SUMPRODUCT(B4:B100,L4:L100)/SUM(B4:B100)
The row number 100 is chosen arbitrarily, but should evidently encompass all data rows. If you have no data occurring below your table, then it is safe to add a large margin. You'll want to avoid the situation where you think you add a line to the table, but actually get outside of the range of the formula. Using proper Excel tables can help to avoid this situation.
2. Use an array formula
This would be a second resort for when the formula becomes more complicated and cannot be executed with a "simple" SUMPRODUCT. But the above would translate to this array formula:
=SUM(B4:B100*L4:L100)/SUM(B4:B100)
Once you have typed this in the formula bar, make sure to press Ctrl+Shift+Enter to enter it. Only then will it act as an array formula.
Again, the same remark about row number 100.
3. Use an extra column
Things get easy when you use an extra column for storing the product of B & L values for each row. So you would put in cell N4 the following formula:
=B4*L4
...and then copy that relative formula to the other rows. You can hide that column if you want.
Then the overal formula can be:
=SUM(N4:N100)/SUM(B4:B100)
With this solution you must take care to always copy a row when inserting a new row, as you need the N column to have the intermediate product formula also for any new row.

Reading Matrix Simulink Robot Arm

I am building the trajectory of a robot arm and I have computed the position of the degrees of freedom in matrix format, i.e. the first position is row 1 of the matrix (x1,y1,z1), the 2nd position is row 2 (x2,y2,z2) etc.
I am trying to read a row at a time in Simulink and change to the following row when the first position has been reached. However it is not possible for me to solve this with a script. Any suggestions?
If I am understanding your question correctly you have a matrix of desired states and some sort of control loop inside of Simulink.
In brief, loops in Simulink are best represented by switches. And the easiest way to access individual rows of your matrix of desired states (x,y,z) is through the "Select Rows" block.
For example you can feed your matrix into the "Select Rows" simulink block "In1". Then you can take the difference (using sum block) between the output of the the "Select Rows" block and your current state (x,y,z) and feed the norm of this difference into a compare block set to "less than". Feed the result of the compare block into a switch so that the switch will be thrown on when the result of the compare block is less than some tolerance. The switch can be set to 0 when False and 1 + the previous output value of the switch when True. The output value of this switch can then be used to choose the index on the "row selector."
You might encounter a scenario in which the switch will stay true for too long in which case you can either reduce your tolerance or create your own transient switch by using an AND gate to the TRUE input to switch.

Determine offset where the most constructive interference occurs

I have two arrays of data:
I would like to align these similar graphs together (by adding an offset to either array):
Essentially what I want is the most constructive interference, as shown when two waves together produce the same wave but with larger amplitude:
This is also the same as finding the most destructive interference, but one of the arrays must be inverted as shown:
Notice that the second wave is inverted (peaks become troughs / vice-versa).
The actual data will not only consist of one major and one minor peak and trough, but of many, and there might not be any noticeable spikes. I have made the data in the diagram simpler to show how I would like the data aligned.
I was thinking about a few loops, such as:
biggest = 0
loop from -10 to 10 as offset
count = 0
loop through array1 as ar1
loop through array2 as ar2
count += array1[ar1] + array2[ar2 - offset]
replace biggest with count if count/sizeof(array1) > biggest
However, that requires looping through offset and looping through both arrays. My real array definitions are extremely large and this would would take too long.
How would I go about determining the offset required to match data1 with data2?
JSFiddle (note that this is language agnostic and I would like to understand the algorithm more-so than the actual code)
Look at Convolution and Cross-correlation an its computation using Fast Fourier Transformation. It's the way how it is done in real life applications.
If (and only if) you data has very recognizeable spikes, you could do, what a human being would do: Match the spikes: Fiddle
the importand part is function matchData()
An improved version would search for N max and min spikes, then calculate an average offset.

Resources