True pixel quantity - c#

I know that my question will be a littel strange but i need realy some help. I have an algorithme to calculate true pixel quantity in a binary image. Her is an example how it work:
Binary Image :
0 0 1 0 0 1
1 1 1 1 0 1
1 1 1 1 1 1
1 1 0 0 0 1
0 1 0 0 0 1
Her is the result :
18 15 11 8 6 5
16 13 9 7 5 4
11 9 6 5 4 3
5 4 2 2 2 2
2 2 1 1 1 1
And this is how it work :
Result (i,j) = result (i+1, j) + result (i, j+1) - result(i + 1, j + 1) + Image(i,j)
Her an example for the 18 value:
18 = 16 + 15 - 13 + 0
My question :
What is the name of this algorithm because I need to get some more information about it?
Thank you for help.

This is called an integral image, or summed area table. It is used to speedup box filtering, among others. It is a 2D generalization of a prefix sum.

Related

Logical AND operation with integers in C#

When I do the following logical AND operation with numbers in C# I'm getting the following results:
-3 & 3 = 1
-1 & 1 = 1
0 & 0 =0
but when I do 8 & -8 =8
Can someone please explain how we are getting the result as 8?
If you look at the numbers in hexadecimal format, it will help you understand how the calculation is performed.
Assuming you store the numbers as integers:
3 = 0x00000003
8 = 0x00000008
-3 = 0xFFFFFFFD
-8 = 0xFFFFFFF8
Then if we zoom in on the smallest nibble (4-bits, consider the following):
For 3 & -3
0 0 1 1
& 1 1 0 1
-------
0 0 0 1 = 1
For 8 & -8
1 0 0 0
& 1 0 0 0
-------
1 0 0 0 = 8
To see what is going on, you could use the following:
static void Main(string[] args)
{
show(8);
show(-8);
}
static void show(int i)
{
Console.WriteLine($"{i,3} = 0b{Convert.ToString(i, 2).PadLeft(32, '0')}");
}
Output:
8 = 0b00000000000000000000000000001000
-8 = 0b11111111111111111111111111111000

Lookup from a 3-column table and interpolate one of the columns?

I have a table with 3 columns: Temperature, Pressure, and Gain.
interface IEntry
{
public float Temperature {get;}
public int Pressure {get;}
public float Gain {get;}
}
Given a certain "input" pair (Temperature_input, Pressure_input), I would like to match this pair with the ones in the table and lookup the corresponding gains, interpolating in certain cases.
Example:
T P G
3 1 0
3 2 1
3 3 2
3 5 4
6 1 0
6 2 2
6 3 3
8 1 0
8 2 3
9 1 0
Input pair (T = 4, P = 2):
Step 1: As the exact temperature T = 4 is not in the table, the immediate lower and upper temperatures 3 and 6 are examined (8 table entries)
Step 2: The pairs for T = 3, T = 6 are sorted by Pressure (which is an int)
3 1 0
6 1 0
3 2 1
6 2 2
3 3 2
6 3 3
3 5 4
3 5 4
Step 3: Linear interpolation between temperatures of the same pressure:
3 2 1
6 2 2
Result is the interpolated Gain = 1 + (2-1)/(6-3) = 1.33
What is an efficient way of implementing the lookup part of the table, that will be accessed millions of times?

permutations of k objects from a set of n algorithm

I'm looking for an efficient way to achieve this:
you have a set of numbers, let's say that our set is equal to 4 (N = 4);
you have to generate all permutations of 3 elements (K = 3);
Output for N = 4 and K = 3:
1 2 3
1 2 4
1 3 2
1 3 4
1 4 2
1 4 3
2 1 3
2 1 4
2 3 1
2 3 4
2 4 1
2 4 3
3 1 2
3 1 4
3 2 1
3 2 4
3 4 1
3 4 2
4 1 2
4 1 3
4 2 1
4 2 3
4 3 1
4 3 2
Anyone have a great, nice'n'quick algorithm up their sleeve or web reference??
Thanks!
Something like this pseudocode:
permute(set, output, len) //output will hold all the permutations
for each number in the set do
choose number and store it at output[0]
if(!empty(set))
call permute(set{without the number}, output + (len - 1)!, len-1) //adjust the position
Invoke by permute(set, output, k)

Get max x rows plus all row info per group by in mysql to linq

A while ago I found a handy query for mysql to get the top X per group by.
This is what I mean:
if this is the table:
rid id1 id2 id3 value
1 1 1 1 10
2 1 1 2 11
3 1 1 3 9
4 1 2 1 20
5 1 2 2 18
6 1 2 3 23
7 1 3 1 30
8 1 3 2 34
9 1 3 3 31
10 1 3 4 27
11 1 3 5 32
12 1 4 1 41
13 1 4 2 40
14 1 4 3 43
15 1 5 1 53
16 1 5 2 51
17 1 5 3 50
18 2 1 1 11
19 2 1 2 9
20 2 1 3 12
I want to get this result:
rid id1 id2 id3 value
2 1 1 2 11
6 1 2 3 23
8 1 3 2 34
14 1 4 3 43
15 1 5 1 53
I can get this by running the following mysql query:
SELECT * FROM
(SELECT * FROM idsandvalue
WHERE id1=1 AND
(SELECT COUNT(*) FROM idsandvalue AS helper
WHERE helper.id1 = idsandvalue.id1
AND helper.id2= idsandvalue.id2
AND helper.value > idsandvalue.value
) < 1
)a;
if I change < 1 to lets say 2, 3 or x I can get the top x per id2 where id1=1 (so, two of the same id2's with different id3's) like this:
rid id1 id2 id3 value
1 1 1 1 10
2 1 1 2 11
4 1 2 1 20
6 1 2 3 23
8 1 3 2 34
11 1 3 5 32
12 1 4 1 41
14 1 4 3 43
15 1 5 1 53
16 1 5 2 51
two questions.
A) the query is not really fast in MySQL. Takes a while (runs a table with 3207394 rows). Can I get the same result with the use of a different query (I was not able to get it).
B) How can I translate this to linq? Due to the strange where statement, I have no clue how to translate this into linq.
(later I added this extra question as well)
in MySQL I use this query:
SELECT *,COUNT(*) AS Counter FROM idsandvalue GROUP BY id1,id2;
to get this result:
rid id1 id2 id3 value Counter
1 1 1 1 10 3
4 1 2 1 20 3
7 1 3 1 30 5
12 1 4 1 41 3
15 1 5 1 53 3
18 2 1 1 11 3
I'm also having difficulties translating this to Linq.
(extra info was too big for comment)
Hi John (thanks for the quick respond).
with this mysql query
SELECT * FROM
(SELECT * FROM idsandvalue
WHERE id1=1 AND
(SELECT COUNT(*) FROM idsandvalue AS helper
WHERE helper.id1 = idsandvalue.id1
AND helper.id2= idsandvalue.id2
AND helper.value > idsandvalue.value
) < 1
)a
I try to get the rows for each grouped id1 and id2 with it's biggest value. That's why in this case I get for instance row with id 2. 11 is the biggest of 10,11 and 9 where id1=1 and id2=1. and that's why I get the row with id 8, because where id1=1 and id2=3 the biggest value for column value is 34. If I change the query to < 2, I get the top two. for id2=1 and id2=3 this would give the rows with id 8 and 11. Is this better explained?
Recreated your table in SQL Server and ran your query against it, than converted the query via linqer:
from a in ((from idsandvalue in db.idsandvalue whereidsandvalue.id1 == 1 &&
(from helper in db.idsandvalue
where
helper.id1 == idsandvalue.id1 &&
helper.id2 == idsandvalue.id2 &&
helper.value > idsandvalue.value
select new {
helper
}).Count() < 1
select new {
idsandvalue
}))
select new {
a.idsandvalue.rid,
a.idsandvalue.id1,
a.idsandvalue.id2,
a.idsandvalue.id3,
a.idsandvalue.value
}
How's this:
var takeRows = 2; // or whatever value you want
var q = from i in idsandvalue
group i by new { i.ID1, i.ID2 } into g
orderby g.Key.ID1, g.Key.ID2
select new { g.Key.ID1, g.Key.ID2, TopValues = g.OrderByDescending(i => i.Value).Take(takeRows) };

Paging through an IEnumerable

I have an IEnumerable object (IEnumerable<Class>) and I would like to retrieve a specified line from the object. So if I'm on page two I would like to select row two from the IEnumerable object and then pass it on to another class etc.
I'm a bit stuck at the moment, any ideas?
Look at the functions .Take() and .Skip(). I normally do something like this:
IEnumerable<object> GetPage(IEnumerable<object> input, int page, int pagesize)
{
return input.Skip(page*pagesize).Take(pagesize);
}
If I understand your requirements correctly, something like this paging mechanism should work:
int pageSize = 10;
int pageCount = 2;
iEnumerable.Skip(pageSize*pageCount).Take(pageSize);
This example shows 10 rows per page and a page number of 2. So, it will skip to page 2 and take the first row on that page.
Assuming that pages and rows start at 1, and there is a fixed number of rows per page (say 10), you need to transform the page number and the row to an index as follows:
Page 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 ...
Row 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 ...
↓
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...
Code:
int page = 2;
int row = 2;
int rowsPerPage = 10;
IEnumerable<MyClass> source = ...
MyClass result = source.ElementAt((page - 1) * rowsPerPage + (row - 1));
So to get row 2 on page 2, you need to skip the first page (10 elements) and then take the second element (index 1 in that page).
I've implemented a dynamic solution in vb.net, i hope helpful:
<Runtime.CompilerServices.Extension()>
Public Function Paginate(Of T As {Class})(source As T, skip As Integer, take As Integer) As T
If source IsNot Nothing AndAlso TypeOf source Is IEnumerable Then
Dim chunk = (From c In DirectCast(source, IEnumerable)).Skip(skip).Take(take).ToList
If chunk.Count = 0 Then Return Nothing
Return AutoMapper.Mapper.Map(chunk, GetType(T), GetType(T))
End If
Return source
End Function

Categories

Resources