2014-03-02 27 views
2

我必須使用Sobel邊緣檢測來檢測圖像是如何被篡改的。我已經能夠實現邊緣過濾器,但一直未能弄清楚如何使用它來檢測篡改。我想通過突出顯示以另一種顏色篡改的區域來顯示篡改。通過在處理中使用索貝爾邊緣篡改檢測

有人可以幫忙嗎?

PImage img, edgeImg; 

int[][] sobelx = { { -1, -2, -1 }, { 0, 0, 0 }, { 1, 2, 1 } }; 

int[][] sobely = { { -1, 0, 1 }, { -2, 0, 2 }, { -1, 0, 1 } }; 

void setup() { 
    img = loadImage("face1.jpg"); 
    size(img.width, img.height); 
    edgeImg = createImage(img.width, img.height, RGB); 
} 

void draw() { 
    image(img, 0, 0); 
    int matrixsize = 3; 
    loadPixels(); 
    img.loadPixels(); 
    int loc = 0; 

    for (int x = 1; x < img.width - 1; x++) { 
     for (int y = 1; y < img.height - 1; y++) { 
      loc = x + y * img.width; 
      int sx = convolution(x, y, sobelx, matrixsize, img); 
      int sy = convolution(x, y, sobely, matrixsize, img); 
      int sum = abs(sy) + abs(sx); 
      sum = constrain(sum, 0, 255); 
      edgeImg.pixels[loc] = sum; 
     } 
    } 

    edgeImg.updatePixels(); 
    image(edgeImg, 0, 0); 
    filter(THRESHOLD, 0.8); 
} 

int convolution(int x, int y, int [][] mat, int matrixsize, PImage img) { 
    float rtotal = 0.0; 
    float gtotal = 0.0; 
    float btotal = 0.0; 
    int total = 0; 

    int offset = matrixsize/2; 

    for(int i=0; i<matrixsize; i++) { 
     for(int j=0; j<matrixsize; j++) { 

      int xloc = x + i - offset; 
      int yloc = y + j - offset; 

      int loc = xloc + img.width*yloc; 
      loc = constrain(loc,0,img.pixels.length - 1); 

      rtotal = rtotal + red(img.pixels[loc])*mat[i][j]; 
      gtotal = gtotal + green(img.pixels[loc])*mat[i][j]; 
      btotal = btotal + blue(img.pixels[loc])*mat[i][j]; 

      total = total + int(brightness(img.pixels[loc])*mat[i][j]); 
     } 
    } 

    rtotal = constrain(rtotal, 0, 255); 
    gtotal = constrain(gtotal, 0, 255); 
    btotal = constrain(btotal, 0, 255); 

    return total; 
} 
+0

僅使用Sobel邊緣檢測,實現精確的通用圖像篡改檢測算法將非常困難。對圖像可能被篡改的方式有任何限制嗎? –

回答

0

我不知道該算法可以如何使用您特定的目的,但我猜你需要運行相同的過濾器,以原始圖像和比較結果。

PImage original = loadImage("face1.jpg"); 
PImage edgeImg;        // previously created 

original.loadPixels(); 
edgeImg.loadPixels(); 

for (int i=0; i<original.pixels.length; i++) { 
    color origPx = original.pixels[i]; 
    color edgePx = edgeImg.pixels[i]; 

    // compare red values, since the edgeImg is B&W 
    if ((origPx >> 16 & 0xFF) != (edgePx >> 16 & 0xFF)) { 
    // don't match? do something! 
    } 
}