簡體   English   中英

如何在iphone中找到帶有顏色的圖像邊緣?

[英]How to find edges of image with its colors in iphone?

我正在開發一個與更改圖像顏色效果相關的應用程序。 我做了幾乎所有事情。 現在的問題是,在其中一個效果我必須像photoshop中的發光egdes濾鏡一樣生效。 此濾鏡使圖像邊緣的顏色和其余圖像顏色為黑色。 通過使用BradLarson GPU Image GPUImageSobelEdgeDetectionFilter或GPUImageCannyEdgeDetectionFilter我可以找到邊緣但是有白色邊緣,我需要找到顏色邊緣。 是他們通過使用GPUImage或openCV找到顏色邊緣的任何其他方法。 任何幫助對我都很有幫助。 謝謝

你真的應該自己去編寫自定義着色器。 它非常平易近人,如果您投入精力,它可以很快變得強大。

也就是說,我認為你正在嘗試這樣的結果: 在一堆樂高的邊緣檢測

你可以在這里找到許多可接受的方法,但是為GPUImageTwoInputFilter的子類編寫自定義着色器,然后使用原始圖像和edgeDetection圖像來定位它是我如何完成你在這里看到的圖片。

子類看起來像這樣:

#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

正如我寫的那樣,我們期望edgeDetection過濾器的輸出成為此自定義過濾器的第二個輸入。

我在edgeDetection圖像上任意選擇閾值0.3為強度,以顯示原始顏色。 通過將它綁定到從應用程序中的UISlider提供的GLint中,可以很容易地將其動態化(Brad的示例代碼中有很多示例)

為了清楚剛開始使用GPUImage的人,使用您編寫的自定義過濾器非常簡單。 我是這樣做的:

[self configureCamera];

edgeDetection = [[GPUImageSobelEdgeDetectionFilter alloc] init];
edgeMixer = [[OriginalColorEdgeMixer alloc] init];

[camera addTarget:edgeDetection];
[camera addTarget:edgeMixer];
[edgeDetection addTarget:edgeMixer];
[edgeMixer addTarget:_previewLayer];

[camera startCameraCapture];

總之,不要害怕開始編寫一些自定義着色器! 學習曲線很簡短,調試器拋出的錯誤非常有助於您准確了解語法的位置。

最后, 這是記錄OpenGL特定函數的語法和用法的好地方

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM