# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import pyvision as pv
from pyvision.edge.canny import canny  # An interface to the OpenCV Canny.
'''
This code is from part 1 of the PyVision Quick Start Guide.
'''
if __name__ == '__main__':
    # (1) Load an image from a file.
    im = pv.Image(pv.__path__[0] + "/data/nonface/NONFACE_16.jpg")

    # (2) Rescale the image
    im = pv.AffineScale(0.5, (320, 240)).transformImage(im)

    # (3) Run the canny function to locate the edges.
    edge_im1 = canny(im)

    # (4) Run the canny function with different defaults.
    edge_im2 = canny(im, threshold1=100, threshold2=250)

    # (5) Save the results to a log.
    ilog = pv.ImageLog("../..")
    ilog.log(im, label="Source")
    ilog.log(edge_im1, label="Canny1")
    ilog.log(edge_im2, label="Canny2")

    # (6) Display the results.
    ilog.show()
import sys

if __name__ == '__main__':
    ilog = ImageLog('../..')
    source_name = os.path.join(pyvision.__path__[0],'data','misc','p5240019.jpg')
    im = Image(source_name)
    im = AffineScale(0.25,(320,240)).transformImage(im)
    im.show()
    ilog.log(im)
    
    mat = im.asMatrix2D()
    high = mat > 180
    low = mat < 50
    mask = high#+low
    
    edges = canny(im,100,200)
    ilog.log(edges)
    
    ilog.log(Image(1.0*mask))
    
    e = edges.asPIL().convert('RGB')
    m = Image(1.0*mask).asPIL()
    i = im.asPIL()
    logo = Image(composite(i,e,m))
    ilog.log(logo)
    #sys.exit()
    
    sm = Image(im.asPIL().resize((320,240),LINEAR))
    detector = DetectorDOG()
    
    points = detector.detect(sm)
    # Rescale the image
    im = pv.AffineScale(0.5, (320, 240)).transformImage(im)
    ilog.log(im, label="Source")

    # Try a range of sigmas
    for sigma in arange(1.0, 5.1, 0.5):

        # Perform a Gaussian Blur
        mat = im.asMatrix2D()
        mat = gaussian_filter(mat, sigma)
        blur = pv.Image(mat)
        blur.annotateLabel(pv.Point(10, 10), "Sigma: " + str(sigma))
        ilog.log(blur, label="Blur")

        #Try a range of thresholds
        for thresh in arange(50, 150, 10):

            # Run the canny function with different defaults.
            edge = canny(blur, threshold1=thresh / 2, threshold2=thresh)

            # Annotate the edge image
            edge.annotateLabel(pv.Point(10, 10), "Sigma: " + str(sigma))
            edge.annotateLabel(pv.Point(10, 20), "Thresh: " + str(thresh))

            # Save the results to a log.
            ilog.log(edge, label="Canny")

    # Display the results.
    ilog.show()
    im = pv.AffineScale(0.5,(320,240)).transformImage(im)
    ilog.log(im,label="Source")    
    
    # Try a range of sigmas
    for sigma in arange(1.0,5.1,0.5):
        
        # Perform a Gaussian Blur
        mat = im.asMatrix2D()
        mat = gaussian_filter(mat,sigma)
        blur = pv.Image(mat)
        blur.annotateLabel(pv.Point(10,10),"Sigma: " + str(sigma))
        ilog.log(blur,label="Blur")    

        #Try a range of thresholds
        for thresh in arange(50,150,10):
    
            # Run the canny function with different defaults.
            edge = canny(blur,threshold1=thresh/2,threshold2=thresh)
            
            # Annotate the edge image
            edge.annotateLabel(pv.Point(10,10),"Sigma: " + str(sigma))
            edge.annotateLabel(pv.Point(10,20),"Thresh: " + str(thresh))
    
            # Save the results to a log.
            ilog.log(edge,label="Canny")
    
    # Display the results.
    ilog.show()
    
    
    
import pyvision as pv
from pyvision.edge.canny import canny  # An interface to the OpenCV Canny.

'''
This code is from part 1 of the PyVision Quick Start Guide.
'''
if __name__ == '__main__':
    # (1) Load an image from a file.
    im = pv.Image(pv.__path__[0]+"/data/nonface/NONFACE_16.jpg")
    
    # (2) Rescale the image
    im = pv.AffineScale(0.5,(320,240)).transformImage(im)
    
    # (3) Run the canny function to locate the edges.
    edge_im1 = canny(im)
    
    # (4) Run the canny function with different defaults.
    edge_im2 = canny(im,threshold1=100,threshold2=250)
    
    # (5) Save the results to a log.
    ilog = pv.ImageLog("../..")
    ilog.log(im,label="Source")    
    ilog.log(edge_im1,label="Canny1")
    ilog.log(edge_im2,label="Canny2")
    
    # (6) Display the results.
    ilog.show()