Skip to content

Instantly share code, notes, and snippets.

@Lait-au-Cafe
Last active December 30, 2017 16:12
Show Gist options
  • Save Lait-au-Cafe/1ced935488ec1fc5602c3de8189d682e to your computer and use it in GitHub Desktop.
Save Lait-au-Cafe/1ced935488ec1fc5602c3de8189d682e to your computer and use it in GitHub Desktop.
大津の手法を用いた二値化
// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel CSMain
// Create a RenderTexture with enableRandomWrite flag and set it
// with cs.SetTexture
RWStructuredBuffer<float2> data;
[numthreads(8,8,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
float2 w = float2(1, 0);
w = lerp(data[id.x*2], data[id.x*2+1], step(data[id.x*2].y, data[id.x*2+1].y));
AllMemoryBarrierWithGroupSync();
data[id.x] = w;
}
// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel CSMain
// Create a RenderTexture with enableRandomWrite flag and set it
// with cs.SetTexture
StructuredBuffer<int> histogram;
RWStructuredBuffer<float2> result; //x:thresh, y:variable
float num1, num2; //画素数
float sum1, sum2; //和
float m1, m2; //平均
[numthreads(8,8,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
float w = 0;
uint i=0;
num1=0;
num2=0;
sum1=0;
sum2=0;
m1=0;
m2=0;
for(i=0; i<id.x; i++){
num1 += histogram[i];
sum1 += histogram[i]*i;
}
m1 = sum1/num1;
for(i=id.x; i<256; i++){
num2 += histogram[i];
sum2 += histogram[i]*i;
}
m2 = sum2/num2;
w = num1 * num2 * (m1 - m2) * (m1 - m2);
if(isfinite(w)){
result[id.x] = float2( (float)id.x, w);
}
else{
result[id.x] = float2((float)id.x, 0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment