Then creating a full array as the lookup-data source, is very inefficient.
since, when you are given your K*K area, you must do a full scan of it. That is a very slow way to do it.
Btw, the speed of the algorithm isn't normally measured in seconds, but in the "big O" notation (
http://en.wikipedia.org/wiki/Big_O_notation)
Scanning an area of k*k pixels, in your manner is O(k^2) => It doesn't depend on N.
There are specific data structures you can use to locate/index 2 dimensional data: For example you can store each white picel in an R-Tree (
http://en.wikipedia.org/wiki/R-tree) then they can be retrieved at relative low cost.
Of course you have the cost of:
- look up (finding one or more pixels)
- insertion (insert each pixel during creation)
A maybe simpler algorithm is using sorted lists, of the following format:
- list 1: sorted list of all lines containing at least one white pixel
Doing a binary search on this you can O(Log linecount) find quickly all lines of interest.
- list 2 (one for each line): sorted x position of each white pixels.
Again using binary search to find the 1st pixel in the K*K block is quick
Since only 10% of the pixels are white, the lists should be small (smaller than the array you do at current)
If you want to trade memory against speed, you can do several, overlapping (overlapping at leas the maximum possible K amount of pixels) lists for lines.
Then you can choose one of the lsits, and it will have less lines, because it doesn't contain lines, that have white pixels outside the given x bounds.
(Again it takes more memory and more time to build / The higher memory isn't affecting the cache for the search, since in the search only one list is used)
if max k = 10 then you have
list 1: lines with white in x from 0 to 50
list 2: lines with white in x from 40 to 90
list 2: lines with white in x from 80 to 130
...
if you need to search the area from y=? and 60-70 => you sear4ch the 2nd list.