#lang racket (require vigracket) (require (rename-in 2htdp/image (save-image save-plt-image) (image-width plt-image-width) (image-height plt-image-height))) (require lang/posn) ;; HELPERS ;; ----------------------------------------------------------------------------- ;; Bilder prozentual skalieren (define (resizeimage-percent img p) (resizeimage img (inexact->exact (round (* (image-width img) (/ p 100)))) (inexact->exact (round (* (image-height img) (/ p 100)))) 2)) ;; Bilder zuschneiden (cropping) (define (cropimage img ul_x ul_y lr_x lr_y) (let ((w (- lr_x ul_x)) (h (- lr_y ul_y))) (plt-image->image (crop ul_x ul_y w h (image->plt-image img))))) ;; BBox: 0 - left, 1 - upper, 2 - right, 3 - lower (define (findBBox x y pixel bbox) (when (> (car pixel) 0.0) (begin (when (> x (vector-ref bbox 2)) (vector-set! bbox 2 x)) (when (< x (vector-ref bbox 0)) (vector-set! bbox 0 x)) (when (> y (vector-ref bbox 3)) (vector-set! bbox 3 y)) (when (< y (vector-ref bbox 1)) (vector-set! bbox 1 y))))) ;; Main program ;; ----------------------------------------------------------------------------- (define image_dir "/Users/seppke/teaching/sose13/bv-praktikum/Bilder/Mastermind") (define img (load-image (build-path image_dir "IMG_8553.JPG"))) (define resized_img (resizeimage-percent img 10)) (define work_img (image->green resized_img)) ;(show-image (cannyedgeimage work_img 1.0 0.0 255.0)) ;(show-image (cannyedgeimage work_img 2.0 0.0 255.0)) ;(show-image (cannyedgeimage work_img 3.0 0.0 255.0)) ;(show-image (cannyedgeimage work_img 1.0 10.0 255.0)) ;(show-image (cannyedgeimage work_img 1.0 20.0 255.0)) (define canny_img (cannyedgeimage work_img 1.0 20.0 255.0)) ;(show-image (image-map (lambda (x) (if (> x 100.0) 255.0 0.0)) work_img)) ;(show-image (image-map (lambda (x) (if (> x 150.0) 255.0 0.0)) work_img)) ;(show-image (image-map (lambda (x) (if (> x 200.0) 255.0 0.0)) work_img)) (define threshold_img (image-map (lambda (x) (if (> x 160.0) 255.0 0.0)) work_img)) (define bbox1 (vector (image-width resized_img) (image-height resized_img) 0 0)) (define bbox2 (vector (image-width resized_img) (image-height resized_img) 0 0)) (define foo (image-for-each-pixel (curryr findBBox bbox1) canny_img)) (define bar (image-for-each-pixel (curryr findBBox bbox2) threshold_img)) (define (overlay-bboxes img bboxes colors) (if (empty? bboxes) (image->plt-image img) (let ((bbox (car bboxes)) (color (car colors))) (underlay/xy (overlay-bboxes img (cdr bboxes) (cdr colors)) (vector-ref bbox 0) (vector-ref bbox 1) (rectangle (- (vector-ref bbox 2) (vector-ref bbox 0)) (- (vector-ref bbox 3) (vector-ref bbox 1)) 'outline color))))) (define result (plt-image->image (overlay-bboxes resized_img (list bbox1 bbox2) '(green red)))) (define crop1_img (cropimage resized_img (vector-ref bbox1 0)(vector-ref bbox1 1)(vector-ref bbox1 2)(vector-ref bbox1 3))) (define crop2_img (cropimage resized_img (vector-ref bbox2 0)(vector-ref bbox2 1)(vector-ref bbox2 2)(vector-ref bbox2 3)))