Welcome to Fractal Forums

Fractal Software => Help & Support => Topic started by: SeryZone on May 02, 2014, 10:53:51 AM




Title: Histogram/Sort equalization
Post by: SeryZone on May 02, 2014, 10:53:51 AM
Hello! About 2 months can't implement histogram coloring, like this: http://www.hpdz.net/TechInfo/Colorizing.htm#Histogram

I write programms on delphi. What's I doing wrong???

Code:
Histogram := nil;
        SetLength(Histogram, (max-min));
        NUM:=0;
          for x:=0 to (maxy*maxx-1) do                             //All data points
            if (IterDat[x]<max) then                                   //If less than max
              inc(Histogram[Iterdat[x]-min], IterDat[x]);         //Histogram+=IterationData[x]
...
             ColorIndex := sqrt( Iterdat[x,y] / Histogram[IterDat[x,y]-min]  );   //ColorIndex - between [0;1]
...
      end;

Help me, please!!!


Title: Re: Histogram/Sort equalization
Post by: phtolo on May 02, 2014, 02:42:11 PM
This is just a wild guess as you may have omitted the missing code.
Are you making the histogram distribution cumulative as mentioned in the linked description ?


Title: Re: Histogram/Sort equalization
Post by: SeryZone on May 02, 2014, 02:45:00 PM
This is just a wild guess as you may have omitted the missing code.
Are you making the histogram distribution cumulative as mentioned in the linked description ?

Code:
begin
        index := 0;
        Histogram := nil; Percentage:=nil;
        SetLength(Histogram, (max-min-1));
        SetLength(Percentage, (max-min-1));
        NUM:=0;
        for x:=0 to (maxx*maxy-1) do
          if (IterDat[x]<max) then
            inc(Histogram[IterDat[x]-min]);
        for x:=0 to max-min-1 do
          NUM:=NUM+Histogram[x];
        running_total:=0;
        for x:=0 to max-min-1 do
          begin
            running_total := running_total + histogram[x];
            percentage[x] := running_total / NUM;
          end;
          for y := 0 to maxy-1 do
            for x := 0 to maxx-1 do
          begin
            if (IterDat[x*maxy+y] >= max) then
            begin
              buffer[index] := 0; // red
              buffer[index + 1] := 0; // green
              buffer[index + 2] := 0; // blue
            end
            else if (IterDat[x*maxy+y] > 0) and (IterDat[x*Maxy+y]<max) then
              ColorIndex := round( 4096*(percentage[IterDat[x*maxy+y]-min]) ) mod 4096;
            begin
              buffer[index] := pal[ColorIndex].b; // red
              buffer[index + 1] := pal[ColorIndex].g; // green
              buffer[index + 2] := pal[ColorIndex].r; // blue
            end;
            inc(index, 4)
          end;
      end;
  end;

Here, I implemented it! But it too rough... How to make Rank-order???


Title: Re: Histogram/Sort equalization
Post by: phtolo on May 03, 2014, 08:11:37 AM
Glad it worked!

Btw, I think you have misplaced a begin in the code above.

Code:
            else if (IterDat[x*maxy+y] > 0) and (IterDat[x*Maxy+y]<max) then
              ColorIndex := round( 4096*(percentage[IterDat[x*maxy+y]-min]) ) mod 4096;
            begin
              buffer[index] := pal[ColorIndex].b; // red
              buffer[index + 1] := pal[ColorIndex].g; // green
              buffer[index + 2] := pal[ColorIndex].r; // blue
            end;

should read

Code:
            else if (IterDat[x*maxy+y] > 0) and (IterDat[x*Maxy+y]<max) then
            begin   //<-- moved up one row
              ColorIndex := round( 4096*(percentage[IterDat[x*maxy+y]-min]) ) mod 4096;
              buffer[index] := pal[ColorIndex].b; // red
              buffer[index + 1] := pal[ColorIndex].g; // green
              buffer[index + 2] := pal[ColorIndex].r; // blue
            end;

Only by looking at it, rank-ordered seems to make quite similar results.
Create an array with values from 0 to maxx*maxy-1 and sort IterDat while making the same changes to the new array.
The new array is used to keep track of the original index. Color according to the new index after sorting.
You probably need to implement your own sorting algorithm for this.


Title: Re: Histogram/Sort equalization
Post by: SeryZone on May 03, 2014, 05:16:10 PM
Glad it worked!

Btw, I think you have misplaced a begin in the code above.

Code:
            else if (IterDat[x*maxy+y] > 0) and (IterDat[x*Maxy+y]<max) then
              ColorIndex := round( 4096*(percentage[IterDat[x*maxy+y]-min]) ) mod 4096;
            begin
              buffer[index] := pal[ColorIndex].b; // red
              buffer[index + 1] := pal[ColorIndex].g; // green
              buffer[index + 2] := pal[ColorIndex].r; // blue
            end;

should read

Code:
            else if (IterDat[x*maxy+y] > 0) and (IterDat[x*Maxy+y]<max) then
            begin   //<-- moved up one row
              ColorIndex := round( 4096*(percentage[IterDat[x*maxy+y]-min]) ) mod 4096;
              buffer[index] := pal[ColorIndex].b; // red
              buffer[index + 1] := pal[ColorIndex].g; // green
              buffer[index + 2] := pal[ColorIndex].r; // blue
            end;

Only by looking at it, rank-ordered seems to make quite similar results.
Create an array with values from 0 to maxx*maxy-1 and sort IterDat while making the same changes to the new array.
The new array is used to keep track of the original index. Color according to the new index after sorting.
You probably need to implement your own sorting algorithm for this.

Thanks, my mistake))) One bug fixed)
But 2560x1440 or even 1920x1080 elements make stack overflow!!!

Code:
procedure QuickSort(first,last:integer;var m:array of integer);
var i,j,n:integer;
    c, x:integer;
begin
  i:=first;
  j:=last;
  x:=m[(first+last) div 2]; {выбираем серединный элемент массива и делим массив пополам}
  repeat
    while m[i]>x do i:=i+1; {считываем всю левую часть до этого элемента}
    while x>m[j] do j:=j-1; {считываем всю правую часть до этого элемента}
    if i<=j then
     begin
       c:=m[i]; {Сортируем элементы массива}
       m[i]:=m[j];
       m[j]:=c;
       i:=i+1;
       j:=i-1;
     end;
   until i>j;
   if first<j then QuickSort(first,j,m);
   if i<last then QuickSort(i,last,m);
  end;

Stack over...FLOWWWWW!!! I laugh about myself's stupidness...


Title: Re: Histogram/Sort equalization
Post by: phtolo on May 04, 2014, 09:54:24 AM
You probably need to find a sorting algorithm that isn't recursive.

Or increase the stack size (not sure how to do this in delphi).


Title: Re: Histogram/Sort equalization
Post by: SeryZone on May 04, 2014, 02:21:18 PM
You probably need to find a sorting algorithm that isn't recursive.

Or increase the stack size (not sure how to do this in delphi).


I didn't find it. What Can I Do?


Title: Re: Histogram/Sort equalization
Post by: phtolo on May 04, 2014, 02:57:40 PM
Try this one:
http://delphi.wikia.com/wiki/Bubble_sort (http://delphi.wikia.com/wiki/Bubble_sort)


Title: Re: Histogram/Sort equalization
Post by: knighty on May 04, 2014, 08:15:55 PM
In your quicksort procedure you did:
j:=i-1; {Shouldn't it be: j:=j-1; ?}

Anyway, There are a lot of examples for quicksort functions for Delphi out there. googling for "delphi quicksort example" and selecting the first match gives: http://delphi.about.com/od/objectpascalide/a/quicksort.htm

Good luck. :)


Title: Re: Histogram/Sort equalization
Post by: SeryZone on May 05, 2014, 03:57:19 PM
Thanks, sorting very fast.

So, how to make Rank-Ordered??? Sorting IterDat don't gives any changes!!!

Code:
begin
        index := 0;
        Histogram := nil; Percentage:=nil; DX:=nil;
        SetLength(Histogram, (max-min));
        SetLength(Percentage, (max-min));
        NUM:=0;
        Setlength(dx, maxx*maxy);
        for x:=0 to (maxx*maxy-1) do
          dx[x]:=IterDat[x];
        QuickSort(dx, low(dx), High(dx));   //<---------------------------
        for x:=0 to (maxx*maxy-1) do
          if dx[x]<max then
          inc(Histogram[dx[x]-min]);
        for x:=1 to max-min do
          NUM:=NUM+Histogram[x];
        running_total:=0;
        for x:=1 to max-min do
          begin
            running_total := running_total + histogram[x];
            percentage[x] := running_total / NUM;
          end;
          for y := 0 to maxy-1 do
            for x := 0 to maxx-1 do
          begin
            if (IterDat[x*maxy+y] >= max) then
            begin
              buffer[index] := 0; // red
              buffer[index + 1] := 0; // green
              buffer[index + 2] := 0; // blue
            end
            else if (IterDat[x*maxy+y] >= 0) and (IterDat[x*Maxy+y]<max) then
            begin
              ColorIndex := round( 4095*(percentage[IterDat[x*maxy+y]-min]) );
              buffer[index] := pal[ColorIndex].b; // red
              buffer[index + 1] := pal[ColorIndex].g; // green
              buffer[index + 2] := pal[ColorIndex].r; // blue
            end;
            inc(index, 4)
          end;
      end;


Title: Re: Histogram/Sort equalization
Post by: SeryZone on May 05, 2014, 05:12:06 PM
I did it!!! Smooth coloring + Rank-Ordered!!! Whhhhhhhhhooooooooooooooooooooooohhhhhhhhhhhhoooooooooooooooooooooooooooooooo!!!!!!!!!! I love all people!!!!! Who read: be happy!!! :worm:
 :cantor_dance: :mandel:


Code:
begin
        index := 0;
        Histogram := nil; Percentage:=nil;
        SetLength(Histogram, (max-min));
        SetLength(Percentage, (max-min));
        NUM:=0;
        {for x:=0 to (maxx*maxy-1) do
          if IterDat[x]<max then
            histogram[IterDat[x]-min]:=1;}
        for x:=0 to (maxx*maxy-1) do
          if IterDat[x]<max then
            inc(Histogram[IterDat[x]-min]);
        for x:=1 to max-min do
          Histogram[x]:=round(sqrt(Histogram[x]));
        for x:=1 to max-min do
          NUM:=NUM+Histogram[x];
        running_total:=0;
        for x:=1 to max-min do
          begin
            running_total := running_total + histogram[x];
            percentage[x] := running_total / NUM;
          end;
          for y := 0 to maxy-1 do
            for x := 0 to maxx-1 do
          begin
            if (IterDat[x*maxy+y] >= max) then
            begin
              buffer[index] := 0; // red
              buffer[index + 1] := 0; // green
              buffer[index + 2] := 0; // blue
            end
            else if (IterDat[x*maxy+y] >= 0) and (IterDat[x*Maxy+y]<max) then
            begin
              ColorIndex := round( 4095*(percentage[IterDat[x*maxy+y]-min]-((percentage[IterDat[x*maxy+y]-min]-percentage[IterDat[x*maxy+y]-min-1])*SmD[x*maxy+y])) );
              buffer[index] := pal[ColorIndex].b; // red
              buffer[index + 1] := pal[ColorIndex].g; // green
              buffer[index + 2] := pal[ColorIndex].r; // blue
            end;
            inc(index, 4)
          end;
      end;

I use sqrt() for equalization

Histogram:
(http://s7.hostingkartinok.com/uploads/images/2014/05/d91f3cb68edfa807333ed1be611bcdbd.jpg)
Sqrt:
(http://s7.hostingkartinok.com/uploads/images/2014/05/3acb21e223165215b59622d1b13fe54c.jpg)


Title: Re: Histogram/Sort equalization
Post by: phtolo on May 05, 2014, 05:22:04 PM
1. Create an array with values from 0 to maxx*maxy-1
Code:
       Setlength(dx, maxx*maxy);
        for x:=0 to (maxx*maxy-1) do
          dx[x]:=x;

2. and sort IterDat while making the same changes to the new array
Code:
procedure QuickSort(first,last:integer;var m,m2:array of integer);
...
       c:=m[i];
       m[i]:=m[j];
       m[j]:=c;
       c:=m2[i];
       m2[i]:=m2[j];
       m2[j]:=c;
...
   if first<j then QuickSort(first,j,m,m2);
   if i<last then QuickSort(i,last,m,m2);
...

3. Color according to the new index after sorting
Code:
        QuickSort(low(IterDat), High(IterDat), IterDat, dx);
          for y := 0 to maxy-1 do
            for x := 0 to maxx-1 do
          begin
index := 4 * dx[x*maxy+y];
            if (IterDat[x*maxy+y] >= max) then
            begin
              buffer[index] := 0; // red
              buffer[index + 1] := 0; // green
              buffer[index + 2] := 0; // blue
            end
            else if (IterDat[x*maxy+y] >= 0) and (IterDat[x*Maxy+y]<max) then
            begin
              ColorIndex := round( 4095*((x*maxy+y) / (maxx*maxy)) );
              buffer[index] := pal[ColorIndex].b; // red
              buffer[index + 1] := pal[ColorIndex].g; // green
              buffer[index + 2] := pal[ColorIndex].r; // blue
            end;
end;


Title: Re: Histogram/Sort equalization
Post by: phtolo on May 05, 2014, 05:22:54 PM
Doh, too slow..

Glad you solved it!!!


Title: Re: Histogram/Sort equalization
Post by: SeryZone on May 05, 2014, 07:02:31 PM
Doh, too slow..

Wait for 2 hours and gave up...