BradLarson / GPUImage

An open source iOS framework for GPU-based image and video processing
http://www.sunsetlakesoftware.com/2012/02/12/introducing-gpuimage-framework
BSD 3-Clause "New" or "Revised" License
20.24k stars 4.61k forks source link

Extracting region of interest using Hough Transform Lines ? desperate for help. #2208

Open maitham opened 8 years ago

maitham commented 8 years ago

I'm trying to implement Hough Transform for road lane detection. Currently I have the hough transform filter, however I would like to extract the coordinates of the lines generated by this filter, to find the vanishing point of the road, then extract just the region of the road that meets the vanishing point. Therefore Is there anyway I can extract the line coordinates generated by the Hough Transform filter?

import UIKit
import GPUImage

class ViewController: UIViewController {

    var videoCamera:GPUImageVideoCamera?

    override func viewDidLoad() {
        super.viewDidLoad()
        videoCamera = GPUImageVideoCamera(sessionPreset: AVCaptureSessionPresetPhoto, cameraPosition: .Back)
        videoCamera!.outputImageOrientation = .LandscapeLeft;

        // set view as gpuImageView
        let filteredVideoView = (self.view as! GPUImageView)
        filteredVideoView.fillMode = kGPUImageFillModePreserveAspectRatioAndFill
        filteredVideoView.sizeInPixels

        let filter = GPUImageHoughTransformLineDetector()
        filter.lineDetectionThreshold = 0.35
        let lineGenerator = GPUImageLineGenerator ()
        lineGenerator.forceProcessingAtSize(filteredVideoView.sizeInPixels)
        lineGenerator.setLineColorRed(1.0, green:0.0, blue:0.0)
        filter.linesDetectedBlock = { (lineArray:UnsafeMutablePointer<GLfloat>, linesDetected:UInt, frameTime:CMTime) in
                    lineGenerator.renderLinesFromArray(lineArray, count:linesDetected, frameTime:frameTime)

                }

        videoCamera?.addTarget(filter)

        let blendFilter = GPUImageAlphaBlendFilter()
        blendFilter.forceProcessingAtSize(filteredVideoView.sizeInPixels)
        let gammaFilter = GPUImageGammaFilter()
        videoCamera?.addTarget(gammaFilter)
        gammaFilter.addTarget(blendFilter)
        lineGenerator.addTarget(blendFilter)
        blendFilter.addTarget(filteredVideoView)
        videoCamera?.startCameraCapture()
    }
}
endios-github commented 7 years ago

I need the position of the detected Line. It ist possible to get the position?

BradLarson commented 7 years ago

@endios-github - There isn't really such a thing as a position of a line. You get a slope and an intercept, which define the line.

zacharyblank commented 6 years ago

@BradLarson I have been working on this and while you do get a slope and an intercept back, it is not clear what scale or coordinate system it's on so translating it to a UIImage, for example, is very challenging. So far I am stumped.

BradLarson commented 6 years ago

@zacharyblank - A normalized coordinate system is used, with the image being in coordinates of 0,0 to 1.0, 1.0. This needs to be translated to your image dimensions, with the slope being adjusted by the ratio of width to height (or maybe the other way around, I forget) and the Y-intercept being scaled by the height of your image. I believe this is what I do in the FilterShowcase sample application.

johndpope commented 6 years ago

so I've spent last few days looking into this. here's my dump on this.

this repo contains original matlab code from whitepaper + YorkUrbanDB dataset https://github.com/johndpope/BMVC-Dubska (you can click on results file in folder to load answers)

for this image from original paper - the detection of vanishing points + raster space using cpp code - yields the following coordinates. (I don't know how to get the image lines)

N.B. their pipeline differs / there's canny image filtering / the matlab code is most instructive.

diamond_vanish.m

%find edge points
EdgeImg = edge(rgb2gray(InputImg),'canny'); 

%find lines
LinesData = mx_lines(int32(padarray(EdgeImg,[PatchSize(end),PatchSize(end)])), int32(PatchSize)); 
LinesData(3,:) = LinesData(3,:)*Normalization;

SubPixelRadius = 2;
Threshold = 0.05;

% run_on_dataset.m
%  default parameters are from training on a ECCV_TrainingAndTestImageNumbers -> trainingSetIndex   
%    Normalization = 0.4
%    SpaceSize = 321
%    PatchSize = [6:4:22]
    Results(i).Image = img;
    Results(i).Detected = img_point_to_world(Detect.CC_VanP, Camera.focal, Camera.pp, Camera.pixelSize)';
    Results(i).Orthogonal = find_ortho(Results(i).Detected', Camera, Normalization, SpaceSize, Detect.Space, size(I))';

cameraParameters.mat that correspoding to YorkUrban photographs.

screen shot 2018-06-15 at 11 20 52 screen shot 2018-06-15 at 11 09 25

screen shot 2018-06-15 at 10 08 36

when I run this against the FeatureExtractionAppDelegate - I get this image / albeit the threshold parameters are not consistent - it does seem a little off not picking up the line of the building.

screen shot 2018-06-15 at 09 57 19

I did rig the filter code for gpuimage to mac and it does show the diamond space ok - https://gist.github.com/johndpope/7b7747f361264066ee05554aa036998c

screen shot 2018-06-15 at 10 23 00

Digging around elsewhere on github - I found this code cpp / python

https://github.com/johndpope/optimization The class RasterSpacePure seems closest to white paper from @Kairat100 I had to update some of the code my branch - https://github.com/johndpope/optimization ran into this problem compiling / after finally getting all the dependencies with brew to install / took me a few hours with majove. https://github.com/Kairat100/optimization/issues/8

from what I can understand from the optimization repo - is that this is a real time detection of raster space using diamond space technique. albeit couldn't get it to work. might keep persisting.

@Kairat100 has separated two files - RasterSpacePure.py /RasterSpace.py one leverages cython / c code. this is just python code - I had to fix a bug with indexing / a / b / c d. (I actually still getting my head around what this is and how this correlates to matlab code) https://gist.github.com/johndpope/46c17fbab58a1318c6ef9ba36dc267fc

Not having looked into the code of GPUImageHoughTransformLineDetector / not sure of the overlap. It would seem like getting the RasterSpacePure.py ported to swift may help unlock things. will keep you posted.

johndpope commented 6 years ago

this code is working (slowly) there's a c version but I couldn't get it to compile - https://github.com/johndpope/optimization/blob/master/puremain.py

screen shot 2018-06-15 at 11 46 16