我一直在使用Hough變換的Rosetta Code Java implementation,它也很好地生成了累加器的可視化。在原始圖像中顯示與Rosetta Code Hough變換髮現的行
通過在輸入圖像是這樣的:
累加器是這樣的:
鑑於我cpompile並調用類是這樣的:
$ javac HoughTransform.java && java HoughTransform pentagram.png out.png 640 480 100
這是有道理的。現在我想用我發現的線條覆蓋原始圖像,但這給我帶來了嚴重的麻煩。
我只能找到一個例子我想要做什麼用C++編寫:
... int x1, y1, x2, y2; x1 = y1 = x2 = y2 = 0; if(t >= 45 && t <= 135) { //y = (r - x cos(t))/sin(t) x1 = 0; y1 = ((double)(r-(_accu_h/2)) - ((x1 - (_img_w/2)) * cos(t * DEG2RAD)))/sin(t * DEG2RAD) + (_img_h/2); x2 = _img_w - 0; y2 = ((double)(r-(_accu_h/2)) - ((x2 - (_img_w/2)) * cos(t * DEG2RAD)))/sin(t * DEG2RAD) + (_img_h/2); } else { //x = (r - y sin(t))/cos(t); y1 = 0; x1 = ((double)(r-(_accu_h/2)) - ((y1 - (_img_h/2)) * sin(t * DEG2RAD)))/cos(t * DEG2RAD) + (_img_w/2); y2 = _img_h - 0; x2 = ((double)(r-(_accu_h/2)) - ((y2 - (_img_h/2)) * sin(t * DEG2RAD)))/cos(t * DEG2RAD) + (_img_w/2); } ...
從https://github.com/brunokeymolen/hough/blob/master/hough.cpp#L125
我試圖適應的代碼至少要看到,如果我能得到一般想法,但C++版本和Rosetta代碼版本的實現看起來有些不同。
我實現:
public static void getLines(String filename, int thetaAxisSize, ArrayData arrayData) throws IOException
{
BufferedImage inputImage = ImageIO.read(new File(filename));
double[] sinTable = new double[thetaAxisSize];
double[] cosTable = new double[thetaAxisSize];
for (int theta = thetaAxisSize - 1; theta >= 0; theta--)
{
double thetaRadians = theta * Math.PI/thetaAxisSize;
sinTable[theta] = Math.sin(thetaRadians);
cosTable[theta] = Math.cos(thetaRadians);
}
java.awt.Color color = new java.awt.Color(255, 0, 0);
int max = arrayData.getMax();
System.out.println("Max value: " + max);
for (int r = 0; r < arrayData.height; r++)
{
for (int theta = 0; theta < arrayData.width; theta++)
{
int val = arrayData.get(theta, r);
if (val < max - 1) {
continue;
}
System.out.println("Found val: " + val + ", r: " + r + ", theta: " + theta);
int x = (int)(r * cosTable[theta]);
int y = (int)(r * sinTable[theta]);
System.out.println("Found val: " + val + ", r: " + r + ", theta: " + theta + ", x/y: " + x + "/" + y);
}
}
ImageIO.write(inputImage, "PNG", new File("/tmp/hough-overlay.png"));
}
但隨後就卡住,因爲結果已經是無意義的對我說:
Max value: 217 (this one still makes sense)
Found val: 216, r: 275, theta: 342
Found val: 216, r: 275, theta: 342, x/y: -29/273
Found val: 216, r: 276, theta: 340
Found val: 216, r: 276, theta: 340, x/y: -27/274
Found val: 217, r: 277, theta: 337
Found val: 217, r: 277, theta: 337, x/y: -23/276
Found val: 217, r: 277, theta: 339
Found val: 217, r: 277, theta: 339, x/y: -25/275
Found val: 217, r: 278, theta: 336
Found val: 217, r: 278, theta: 336, x/y: -21/277
Found val: 216, r: 279, theta: 334
Found val: 216, r: 279, theta: 334, x/y: -19/278
我的數學不夠好,找出我如何改變r
和theta
回到圖像空間,給出一條線被發現。我閱讀了很多有關霍夫變換的白皮書和文章,但我仍然不明白。我發現的幾個實現,如C++版本,似乎都與我的Java版本有點不同。
所以我想知道,有沒有人用過Hough變換的Rosetta Code Java實現,並設法將線條從極座標變換回原圖像?
你是否有五個簇的近r/theta對圍繞這個pentagon的最大值? – MBo
@MBo Hm你是什麼意思?根據霍夫空間圖像,線條被正確檢測。 – Max
是的,你的問題是要獲得這些明亮地方的位置,不是嗎? – MBo