Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You really owe it to yourself to play around with writing custom shaders. It's extremely approachable, and can very quickly become powerful if you invest the effort. </p> <p>That said, I think you're trying for something like this result: <img src="https://i.stack.imgur.com/OQN1U.png" alt="Edge Detection on a pile of Lego&#39;s"></p> <p>There are many acceptable ways you could get here, but writing a custom shader for a subclass of GPUImageTwoInputFilter then targeting it with both the original image AND the edgeDetection image is how I accomplished the picture you see here.</p> <p>The subclass would look something like this:</p> <pre><code>#import "OriginalColorEdgeMixer.h" //Assumes you have targeted this filter with the original image first, then with an edge detection filter that returns white pixels on edges //We are setting the threshold manually here, but could just as easily be a GLint which is dynamically fed at runtime #if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE NSString *const kOriginalColorEdgeMixer = SHADER_STRING ( varying highp vec2 textureCoordinate; varying highp vec2 textureCoordinate2; uniform sampler2D inputImageTexture; uniform sampler2D inputImageTexture2; lowp float threshold; mediump float resultingRed; mediump float resultingGreen; mediump float resultingBlue; void main() { mediump vec4 textureColor = texture2D(inputImageTexture, textureCoordinate); mediump vec4 textureColor2 = texture2D(inputImageTexture2, textureCoordinate2); threshold = step(0.3, textureColor2.r); resultingRed = threshold * textureColor.r; resultingGreen = threshold * textureColor.g; resultingBlue = threshold *textureColor.b; gl_FragColor = vec4(resultingRed, resultingGreen, resultingBlue, textureColor.a); } ); #else NSString *const kGPUImageDifferenceBlendFragmentShaderString = SHADER_STRING ( varying vec2 textureCoordinate; varying vec2 textureCoordinate2; uniform sampler2D inputImageTexture; uniform sampler2D inputImageTexture2; float threshold; float resultingRed; float resultingGreen; float resultingBlue; void main() { vec4 textureColor = texture2D(inputImageTexture, textureCoordinate); vec4 textureColor2 = texture2D(inputImageTexture2, textureCoordinate2); threshold = step(0.3,textureColor2.r); resultingRed = threshold * textureColor.r; resultingGreen = threshold * textureColor.g; resultingBlue = threshold *textureColor.b; gl_FragColor = vec4(resultingRed, resultingGreen, resultingBlue, textureColor.a); } ); #endif @implementation OriginalColorEdgeMixer - (id)init; { if (!(self = [super initWithFragmentShaderFromString:kOriginalColorEdgeMixer])) { return nil; } return self; } @end </code></pre> <p>As I've written this, we're expecting the edgeDetection filter's output to be the second input of this custom filter.</p> <p>I arbitrarily chose a threshold value of 0.3 for intensities on the edgeDetection image to enable the original color to show through. This could easily be made dynamic by tying it to a GLint fed from a UISlider in your app (many examples of this in Brad's sample code)</p> <p>For the sake of clarity for people just starting out with GPUImage, using that custom filter you wrote is really easy. I did it like this:</p> <pre><code>[self configureCamera]; edgeDetection = [[GPUImageSobelEdgeDetectionFilter alloc] init]; edgeMixer = [[OriginalColorEdgeMixer alloc] init]; [camera addTarget:edgeDetection]; [camera addTarget:edgeMixer]; [edgeDetection addTarget:edgeMixer]; [edgeMixer addTarget:_previewLayer]; [camera startCameraCapture]; </code></pre> <p>In summary, don't be scared to start writing some custom shaders! The learning curve is brief, and the errors thrown by the debugger are extremely helpful in letting you know exactly where you f**d up the syntax.</p> <p>Lastly, <a href="http://www.opengl.org/sdk/docs/" rel="noreferrer">this is a great place for documentation of the syntax and usage of OpenGL specific functions</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload